Kill long running script, if it crosses the threshold time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Kill long running script, if it crosses the threshold time
# 1  
Old 11-12-2013
Kill long running script, if it crosses the threshold time

Hi,

I need a script to kill the process if it running for long time.

Inputs for the scripts:
1.test.sh (will be running fron cron scheduler)
2.1 hr (ie threshold_time - if the test.sh is running for more than 1 hr test.sh has to kill)

Thanks,
Divya
# 2  
Old 11-12-2013
Here's a basic concept that I wrote:

Code:
#!/bin/bash
#
# procWatch.sh
#
# -- script to check the elapsed time of a given
# -- process and if running longer than an hour
# -- kill the process
#

# check command-line for process name
if [ $# -ne 1 ]
then
    echo "Usage: ${0##*/} <process name>"
    exit 1
fi

# store the process name
proc=$1

# use ps command to list the processes and parse
# out our process
read etime pid prog <<<$(ps -eo etime,pid,args | grep $proc | grep -v grep)

# scrub the inputs
etime=$(echo $etime | awk -F'-' '{print $2}')
prog=$(echo $prog|awk '{print $1}')
prog=${prog##*/}

# convert hour time to seconds
hour=$(date --date="1:00:00" +%s)

# convert elapsed time to seconds
elapsed=$(date --date="$etime" +%s)

# compare the times
if [ $elapsed -gt $hour ]
then
    echo "[+] The $prog process has been running for over an hour."
    echo "[+] Killing process: $pid."
    kill $pid
else
    echo "[+] The $prog process has been running for $etime."
fi

# done
exit 0

d6adeb2db50f32cecbc8fc313292e797

Last edited by in2nix4life; 11-12-2013 at 01:40 PM..
This User Gave Thanks to in2nix4life For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

First script in a long time

I was wondering if I could get some feedback on my script to grab time from our MDM... I blocked out all of the important stuff. I really appreciate any guidance, since I am long out of practice. #!/bin/bash serial=$1 # get last seen value of ipad lastseen=$(curl -s -X "GET"... (11 Replies)
Discussion started by: andysensible
11 Replies

2. Shell Programming and Scripting

Killing the process if running for long time in script

I am running a script which will read the data from fail line by line and call the Java program by providing the arguments from the each line. The Java code is working fast for few records and for some records its getting hanged not providing response for morethan one hour. Currently am... (4 Replies)
Discussion started by: dineshaila
4 Replies

3. Shell Programming and Scripting

Script to kill a process running at a certain port

Hello, I have multiple scripts (vlc1, vlc2,...vlc5) and as I do not know how to run them as upstart processes, I entered my script links into rc.local file. Here is the sample one for process vlc1: $ nano /etc/rc.local added below line into rc.local /var/bin/./vlc1 & Port nr of vlc1... (7 Replies)
Discussion started by: baris35
7 Replies

4. Shell Programming and Scripting

Help with kill a specific process after certain running time

Hi, Do anybody experience to write a bash script in order to kill a specific process (java) after certain time of running? eg. java java.jar task_run.txt I will run a java program (java.jar) which will run a long list of process (task_run.txt) one by one. I plan to terminate the java... (5 Replies)
Discussion started by: perl_beginner
5 Replies

5. Shell Programming and Scripting

Running script automatically when threshold limit met in one of the field in oracle database

Hi Guys, Need you help in one point! I am working on one shell script which takes following steps : 1. Taking one query result from oracle database 2. Exporting that result to Xls file 3. Mailing that file to my own mail ID Now, I want to give a threshold limit to one of the column... (0 Replies)
Discussion started by: Agupte
0 Replies

6. Shell Programming and Scripting

determine the active processes on the system which are running since long time

Hi , Please help me shell script to determine the active processes on the system which are running since long time (2 Replies)
Discussion started by: itian2010
2 Replies

7. Shell Programming and Scripting

Alert for long running commands in script

Hi All, Is there a way in which a long running command can terminate by itself inside a script? I need something like below: echo Start <command> exit If the <command> is taking more than say 100 seconds to complete, the script should exit without manual intervention. Thanks, Deepak (2 Replies)
Discussion started by: deepakgang
2 Replies

8. Shell Programming and Scripting

Script for long running processes....

I searched the forums but didn't see anything related to what I'm looking for. I need a script that would give me a listing of jobs running longer than, for example, 12 hours or so. Thanks in advance for your assistance!! (2 Replies)
Discussion started by: CyberOptiq
2 Replies

9. UNIX for Dummies Questions & Answers

Script to kill rsh processes running for more than 10 mins

Hi Friends, I need to write a script to kill some processes running for more than 10 minutes. Can I get some pointers on that. Thanks for ur help in Advance. Thanks&Regards, Amit (3 Replies)
Discussion started by: amitsayshii
3 Replies

10. UNIX for Advanced & Expert Users

script to kill rsh processes running for more than 10 minutes

Hi Friends, I need to write a script to kill some processes running for more than 10 minutes. Can I get some pointers on that. Thanks for ur help in Advance. Thanks&Regards, Amit (1 Reply)
Discussion started by: amitsayshii
1 Replies
Login or Register to Ask a Question