0

I am trying to create a monitoring script in Shell script. When a particular process/thread is not available, i have to kick a background process which will be never ending. This works fine when i manually run my monitoring script. However, this monitoring script has to be run every 10 minutes(i tried crontab for scheduling). Now, the problem is the background process which there in the monitoring script is not running if the script is called through the crontab.

Any help is appreciated! I am open to use any other Linux schedulers too if it available for free.

The monitoring script pseudo code as follows:

#!/bin/sh
backgroundProcessCount=`ps -ef | grep backgroundProcess | grep -v grep | wc -l`
    echo $backgroundProcessCount
    #### Start if not already running
    if [ $backgroundProcessCount-eq 0 ]
    then
            echo Background Process is not running!!!
            sh /usr/share/bin/backgroundProcess.sh
            echo Background Process....
    else
            echo Background Process NOT Started!!!!
    fi

Thanks!

invisible
  • 53
  • 1
  • 8
  • Any error? Does it even run? – Aleks-Daniel Jakimenko-A. Aug 27 '13 at 21:32
  • Any code you could show us please? – konsolebox Aug 27 '13 at 21:32
  • @Aleks no error. It silently comes out :( – invisible Aug 27 '13 at 21:34
  • @konsolebox updated my qn with the code snippet. Thanks! – invisible Aug 27 '13 at 21:41
  • @user2187477 Try specifying a value for PATH since it would be different, or try running your script in crontab with -l option to Bash to imitate login process. Also use absolute paths for executing binaries like sh when possible. – konsolebox Aug 27 '13 at 21:45
  • @konsolebox Am a new bee.. Unable to get few things.. Which PATH u refer to ?? Can u give examples for running crontab with -l ?? And lastly, i am already using absolute path only in exec my shell script.. – invisible Aug 27 '13 at 21:53
  • @user2187477 You could try launching your script with `X Y * * * /path/to/sh -l /path/to/script` in crontab. It seems that most shells understands the `-l` option to act like login when running. If that doesn't work, try solutions in these threads: http://stackoverflow.com/questions/2135478/how-to-simulate-the-environment-cron-executes-a-script-with and here: http://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables. – konsolebox Aug 27 '13 at 22:15

1 Answers1

1

You might also consider logging the possible errors that might occur on the script like this:

#!/bin/sh
{
    backgroundProcessCount=`ps -ef | grep backgroundProcess | grep -v grep | wc -l`
    echo $backgroundProcessCount
    #### Start if not already running
    if [ $backgroundProcessCount-eq 0 ]
    then
            echo Background Process is not running!!!
            sh /usr/share/bin/backgroundProcess.sh
            echo Background Process....
    else
            echo Background Process NOT Started!!!!
    fi
} >/var/log/something 2>&1

That might help you find what's preventing the background process to run.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • Thanks! The error logging helped me finding the issue. The issue was with the classpath. I fixed it.. Thanks again!! One more quick qn.. What is "2>&1" means ?? – invisible Aug 28 '13 at 16:16
  • It duplicates fd 1 to 2 so anything sent to 2 is actually sent to 1. So in the case above since 1 is already redirected to /var/log/something, anything that sends 2 would be sent to the file as well. – konsolebox Aug 28 '13 at 16:43