Sponsored Content
Full Discussion: Monitor a script
Top Forums UNIX for Dummies Questions & Answers Monitor a script Post 302765261 by gary_w on Friday 1st of February 2013 05:18:37 PM
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.
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

[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

7. 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

8. 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

9. 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
msvc(8) 						      System Manager's Manual							   msvc(8)

NAME
msvc - control minit SYNOPSIS
msvc [ -[uodpchaitko] ] [ -P pid ] service [...] DESCRIPTION
msvc is the management interface to minit. service is the service directory name relative to /etc/minit. Starting with minit 0.9 you can also include /etc/minit/ in the service name. OPTIONS
If no option is given, msvc will just print a small diagnostic message to stdout, saying if the service is up, down or finished, which PID it has if it is up, and for how long it has been in this state. -u Up. If the service is not running, start it. If the service stops, restart it. -o Once. If the service is not running, start it. If the service stops, do not restart it. -d Down. If the service is running, send it a TERM signal and then a CONT signal. After it stops, do not restart it. -p Pause. Send the service a STOP signal. -c Continue. Send the service a CONT signal. -h Hangup. Send the service a HUP signal. -a Alarm. Send the service an ALRM signal. -i Interrupt. Send the service an INT signal. -t Terminate. Send the service a TERM signal. -k Terminate. Send the service a KILL signal. -P pid Set PID. Tell minit the PID of the service is really pid. This is useful for services that fork themselves in the background but put their real PID in a file, typically called /var/run/service.pid. Used by pidfilehack. -D service Print dependencies. This will print all the names of all the services that were started because this services depended on them. Please note that this is not done recursively (i.e. if default depends on qmail and qmail depends on log, this will print qmail, not qmail/log. But msvc -D qmail will print qmail/log). -H Print history. This will print the names of the ten least recently spawned processes. This is useful if you see a process looping (initialization fails and minit is restarting it all the time). RETURN CODES
Generally, msvc return zero if everything is OK or 1 on error (could not open /lib/minit/in or /lib/minit/out or there is no process with the given name). In diagnostic mode, it will exit 0 if the service is up, 2 if it is down or 3 if it is finished. SEE ALSO
pidfilehack(8), svc(8) msvc(8)
All times are GMT -4. The time now is 06:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy