Monitor a script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Monitor a script
# 1  
Old 01-29-2013
Monitor a script

Hi All,

In a script I would like to check whether the current running command is completed in 1 hour or not. This i want to achieve inside the same script. I don't want to use separate script to monitor my current running script.

eg.
pseudocode;

Command1
if command1>60 mins
then terminate
else
continue

If anyone has any idea. Please share with me.
# 2  
Old 01-29-2013
If your requirement is to determine how long it takes the command to run then decide to exit whether it exceed 1 hr or not then:
Code:
starttime=`date "+%s"`
Command1
endtime=`date "+%s"`
interval=`expr $endtime - $starttime`

if [ $interval -ge 3600 ]; then
  exit
fi

# subsequent commands

You may have to explain further what it is you want to do if this is not what you are looking for
# 3  
Old 01-29-2013
Hi dexdex200,

Thanks for the reply.
Suppose COMMAND1 is started at 10:00 then command has to finish at 11:00 . If not then it should exit.

If COMMAND1 is taking 2 hours to complete then the code provided by u will exit after 2 hours. I want to exit immediately. Means at 11:00 o'clock i should check whether Command1 is completed or not.
# 4  
Old 01-29-2013
Hi Vicky5,
You can use an expect script to achieve this. Here is a simplified one:

Code:
#!/bin/bash

/usr/bin/expect <<EOD

spawn Command1
set timeout 3600
expect {
         eof { exit 0 } 
	 timeout { exit 1 }
       }
EOD

status="$?"

if [ "$status" -eq 1 ]; then
    exit
else
   #continue
fi


Last edited by dexdex200; 01-29-2013 at 12:50 PM..
# 5  
Old 01-29-2013
This example should give you some ideas:

Code:
$ cat timeout
#!/bin/ksh

##
##  timeout example
##

BACKGROUND_TIME=30  # Seconds the background process will run to simulate
                    #  a long-running process.
WAIT_TIME=20        # Seconds the main program will wait for the background process.
                    #  For an hour, use 3600.

##
##  Function to start the background process.
##
f_background_command() {
  print "  START - background_command, sleeping $BACKGROUND_TIME seconds..."
  ##  Simulate a long-running process.
  sleep $BACKGROUND_TIME
  print "  END - background_command"
}

##
##  Main
##
print "START"

## Start the background process...
f_background_command &

## And capture its PID.
background_PID=$!

print "background process PID is $background_PID"

## wait for the background command.
print "Waiting for $WAIT_TIME seconds"
sleep $WAIT_TIME

print "TIMEOUT reached"

##  If background command is still running, kill it.
if [[ -d "/proc/$background_PID" ]]; then
  kill -9 $background_PID 2>/dev/null  ## kill -9 could be dangerous!
  print "kill signal sent to PID $background_PID"
else
  print "Background process has already finished."
fi

print "END"

exit 0
$

# 6  
Old 02-01-2013
thank u @gary_w
Its working fine.
if f_background_command() function takes just 5 sec to complete In that case script still waits for another 25 sec.

1. So is there any solution where if f_background_command() completes lessthan 30 sec then continue execution with the rest of the lines after sleep command.
2. If the f_background_command() takes more than 30 sec then the process needs to be terminated,

#2 is achieved with gary_w's code, but #1 is not working.
# 7  
Old 02-01-2013
Here ya go. Checks every CHECK_TIME seconds to see if the background process has finished or if the WAIT_TIME value has been reached. Test by changing the BACKGROUND_TIME and WAIT_TIME values:
Code:
$ cat timeout

#!/bin/ksh

##
##  timeout example
##

integer BACKGROUND_TIME=10  # Seconds the background process will run to simulate
                            #  a long-running process.
integer WAIT_TIME=30        # Max seconds the main program will wait for the background process.
                            #  For an hour, use 3600.
integer CHECK_TIME=2        # Seconds between checking to see if the process finished.
integer elapsed_time=0          # Total time we have waited.

##
##  Function to start the background process.
##
f_background_command() {
  print "  START - background_command, sleeping $BACKGROUND_TIME seconds..."
  sleep $BACKGROUND_TIME
  print "  \nEND - background_command"
}

##
##  Main
##
print "START"

## Start the background process...
f_background_command &

## And capture its PID.
background_PID=$!

print "background process PID is $background_PID"

print "Waiting for $WAIT_TIME seconds or until the process completes..."

## Wait until the background process finishes, or the timeout
##  has been reached.
while :
do
  sleep $CHECK_TIME

  if [[ ! -d "/proc/$background_PID" ]]; then
    print "Background process completed before WAIT_TIME reached."
    break
  fi
  if (( $elapsed_time > $WAIT_TIME )); then
    print "\nTimeout reached."
    kill $background_PID 2>/dev/null  ## kill -9 could be dangerous!
    print "kill signal sent to PID $background_PID"
    break
  fi

  (( elapsed_time = $elapsed_time + $CHECK_TIME ))

  printf "\rElapsed seconds: %d " $elapsed_time
done

print "\nEND"

exit 0

Sample run with BACKGROUND_TIME < WAIT_TIME (Background process finishes before TIMEOUT):
Code:
$  timeout
START
background process PID is 8048
Waiting for 30 seconds or until the process completes...
  START - background_command, sleeping 10 seconds...
Elapsed seconds: 8
   END - background_command
Background process completed before WAIT_TIME reached.

END
$

Sample run with BACKGROUND_TIME > WAIT_TIME (force a timeout):
Code:
$ timeout
START
background process PID is 8186
Waiting for 10 seconds or until the process completes...
  START - background_command, sleeping 30 seconds...
Elapsed seconds: 12
Timeout reached.
kill signal sent to PID 8186

END
$

Make sure your background process properly handles being killed so you don't end up with zombie processes, corrupted files, etc.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Script monitor website wth default tomcat script

Hi all, on our application server we have the following script that monitor the status of the website, my problem here is that i have edite the retries from 3 to 5, and the timewait to 120 second, so the script should check 5 times every 2 minutes, and if the fifth check fails it must restart... (0 Replies)
Discussion started by: charli1
0 Replies

2. Infrastructure Monitoring

Searching for Saas Monitor service which monitor my servers which are sitting in different providers

Sorry if this is the wrong forum Searching for Saas Monitor service which monitor my servers which are sitting in different providers . This monitor tool will take as less CPU as possible , and will send info about the server to main Dashboard. The info I need is CPU / RAM / my servers status (... (1 Reply)
Discussion started by: umen
1 Replies

3. Shell Programming and Scripting

Monitor the services by script

I developed for monitoring the network connections among the branch servers as I given below as script.But I don't know how to monitor the services through network script whether the services is running or not. eg : I want to check the postgres service for all the branch servers through network... (0 Replies)
Discussion started by: kannansoft1985
0 Replies

4. Shell Programming and Scripting

[Help] Script to monitor logs

Hello friends, as they are? First of all sorry for my poor English. I tell them what is my problem. I have the following script, which is basically what makes error search for a pattern within a folder containing logs. The script works fine, the problem is that whenever I find a pattern of new... (2 Replies)
Discussion started by: romanrsr
2 Replies

5. Shell Programming and Scripting

How to monitor a shell script called within a script?

HIi Guys... I am in a fix.... 1st the code : Script 123.sh looks like this : ./abc # a script which is getting called in this script while true do count=`ps -ef | grep abc | wc -l` if echo "abc is running sleep 10 fi done but the process is getting checked... (5 Replies)
Discussion started by: chatwithsaurav
5 Replies

6. Shell Programming and Scripting

Script to monitor Values

Hi All, I want a scrip to monitor values which is the out put of a certain command. Example is $ for (( c=1; c<15; c++ )); do cmu -O HTA -d HTTP-PROXY.tswebpxmp5.$c | grep -i active; done HTA_STATS_htaStatsDef.ifw_stats.streamStat.activeStreams : 2 ... (1 Reply)
Discussion started by: Siddheshk
1 Replies

7. Shell Programming and Scripting

Monitor script

Does anyone have a monitoring script in solaris that monitors the drives in an exclosure? The script should be in /bin/bash or /bin/sh thnks again This should be for solaris 10/11 looking for something that tells me a drive is down or offline.:confused: (0 Replies)
Discussion started by: walnutpony123
0 Replies

8. Shell Programming and Scripting

Help with HD monitor script

Hi, I'm new to linux and I'm trying to compile a hard drive monitoring script. I've seen a few on the internet and I've attempted to stumble through but I'm stuck at my while/do scenario. I assigned the variable NUM then took the percentage from my output and cut the % so it would be just a... (6 Replies)
Discussion started by: crocyson
6 Replies

9. Shell Programming and Scripting

need help doing a script to monitor if files are go through

I am trying to do a shell script to check a folder and see if files are passing through. Now if a file did not pass through in the last 1 hour send an email. ftp----------> folder to monitor ----------->ftp Now the script that moves the file runs every sec in cron, so i do not know if i... (0 Replies)
Discussion started by: jonathan184
0 Replies
Login or Register to Ask a Question