Script not working in crontab

Hi

I have created a script. Which i have configured in cron to make it run in every 2 min. But script is not running. If I checl the cron log at /var/cron/log .it shows its running in every 2 min. Below is the command which i configured in crontab.

But I am not able to find the way so that this script can run in every two mins.

0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /appl/ESA/sigcheck.sh

Please suggest how to run this script in the cron.

Thanks in advance

Nandan

I don't think your crontab entry is the problem. You need to be sure that ALL the environment variables that make the script run from the command line exist in cron.

Example: you might need to source /etc/profile inside your script

. /etc/profile

You could also modify your cron entry to get errors into a log file

0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * *  /appl/ESA/sigcheck.sh 2>> /tmp/sigcheck.log

to see what is going on.

Hi Jim .Thanks for the reply. I did the same as u suggested.

content of /tmp/sigcheck.log are as follows.

resize:  can't open terminal /dev/tty
/appl/ESA/sigcheck.sh: C: not found
resize:  can't open terminal /dev/tty
/appl/ESA/sigcheck.sh: C: not found

Please suggest what need to do now. I am not able to understand about this error.

Thanks in advance..

and you might want to check if your cron allows for step values like */2 to execute a job every other minute...

Something in that script is demanding an interactive terminal.

Without seeing your code we can't possibly tell why.

@RudiC , It not allowing me to write */2 to ececute a job every 2 min.

Hi Corona

Please find below the script code..

#!/bin/sh
#set -x

checkMS()

{

 MSAlive=`ps -ef |grep java |grep -i "ms1@ipx.com" |grep -v grep |wc -l`

    if [ "${MSAlive}" -eq 0 ]; then
      echo "MS is down.. Please check "
      
      `$ESAHOMEINSTALL/ESA/bin/reportEvent raise APP 1 .1.1.1.3 "Messaging Server Down"`

      else
            MSCheck=`$ESAHOMEINSTALL/ESA/bin/printActiveAlarms | grep "Messaging Server Down" | wc -l`
          if [ "${MSCheck}" -eq 1  ]; then

	`$ESAHOMEINSTALL/ESA/bin/reportEvent clear APP 1 .1.1.1.3 "Messaging Server Down"`

	else
               echo " "

	fi

    fi

}

checkTS()

{

 TSAlive=`ps -ef |grep java |grep -i "Name=traf_svr" |grep -v grep |wc -l`

    if [ "${TSAlive}" -eq 0 ]; then
      echo "TS is down.. Please check "

      `$ESAHOMEINSTALL/ESA/bin/reportEvent raise APP 2 .1.1.1.3 "Traffic Server Down"`

      else
            TSCheck=`$ESAHOMEINSTALL/ESA/bin/printActiveAlarms | grep "Traffic Server Down" | wc -l`
          if [ "${TSCheck}" -eq 1 ]; then

        `$ESAHOMEINSTALL/ESA/bin/reportEvent clear APP 2 .1.1.1.3 "Traffic Server Down"`

            else

               echo " "
        fi

    fi

}



checkSIGNode()
{
checkMS
checkTS

}


#*************************************************************************#
# MAIN
#*************************************************************************#

checkSIGNode

Why is this in backticks?

`$ESAHOMEINSTALL/ESA/bin/reportEvent raise APP 1 .1.1.1.3 "Messaging Server Down"`

What is it, anyway? It may be the thing that's demanding a terminal, since I don't see much else that would.

There's some worryingly long pipe chains in there. You can reduce that in lots of ways.

1) You don't need wc -l to check if grep found anything. grep returns a code like any other program.

2) If you have grep | sed | grep | kitchen | sink, you can reduce that by putting them all in one awk call.

3) You're using functions in /bin/sh. /bin/sh is not guaranteed to have functions. Use ksh or bash if you really need functions.

4) You're not using functions in a way that really organizes your program anyway.

5) You're not using if/elif/else properly, nesting more if's inside instead of using elif.

if ps -ef | awk '/java/ && /[mM][sS]1@[iI][pP][xX][.][cC][oO][mM]/ { exit 0 } END { exit 1 }'
then
        echo "MS is down, please check"
        # What application is this?  Is it supposed to be interactive?
        $ESAHOMEINSTALL/ESA/bin/reportEvent raise APP 1 .1.1.1.3 "Messaging Server Down"
elif $ESAHOMEINSTALL/ESA/bin/printActiveAlarms | grep "Messaging Server Down" >/dev/null
then
	$ESAHOMEINSTALL/ESA/bin/reportEvent clear APP 1 .1.1.1.3 "Messaging Server Down"
else
        # Why?
        echo " "
fi


if ps -ef | awk '/java/ && /[nN][aA][mM][eE]=[tT][rR][aA][fF]_[sS][vV][rR]/ { exit 0 } END { exit 1 }'
then
        echo "TS is down.. Please check "

      $ESAHOMEINSTALL/ESA/bin/reportEvent raise APP 2 .1.1.1.3 "Traffic Server Down"

elif $ESAHOMEINSTALL/ESA/bin/printActiveAlarms | grep "Traffic Server Down" >/dev/null
then
        $ESAHOMEINSTALL/ESA/bin/reportEvent clear APP 2 .1.1.1.3 "Traffic Server Down"
else
        # why?
        echo " "
fi
2 Likes

Further to Corona688.

Please post precisely what Operating System and Shell on this and every subsequent thread. Please also post the output from set when you are running in a normal terminal session so we can work out what /bin/sh means when you are running from cron:

uname -a   # Blotting anything confidential like machine names with Xs
echo $SHELL
set

There are many areas of your script which need attention:

1) Corona688 has already pointed out that the backticks are neither required nor advisible in this context.

2) The Environment Variable $ESAHOMEINSTALL is not set anywhere in the script. Please post the output from the set command when logged in interactively so we can see your interactive environment (which will be quite radically different from the environment when running from cron).

3) The script includes dodgy numeric comparisons:

if [ "${MSAlive}" -eq 0 ]; then
if [ "${TSAlive}" -eq 0 ]; then
if [ "${TSCheck}" -eq 1 ]; then

In all of these cases, lose the double quotes. You are comparing numbers not strings.

4) Is it safe to assume that the weird output in /tmp/sigcheck.log did not come from the script as posted but only happens if you include . /etc/profile in the script? Executing . /etc/profile in this context is inadvisible because there is no terminal context.
If true, please post the actual output seen in /tmp/sigcheck.log which matces the script posted.

5) The whole script design depends on processing ps -ef every two-minutes. This is madness. Eventually you will hit a moment when the kernel is busy and ps -ef returns a null or incomplete response.
Ideally you would record the PID of the process in a file when it starts. Then you only need to query the one PID. ps -fp<pid> .
At a minimum consider confining to ps -fu<username> where username is the name of the user who owns the crontab.
Within your script, consider code to only react to say three consecutive failures rather than just the one.

Further to Corona688.

Please post precisely what Operating System and Shell on this and every subsequent thread. Please also post the output from set when you are running in a normal terminal session so we can work out what #!/bin/sh means when you are running from cron:

uname -a   # Blotting anything confidential like machine names with Xs
echo $SHELL
set

There are many areas of your script which need attention:

1) Corona688 has already pointed out that the backticks are neither required nor advisible in this context.

2) The Environment Variable $ESAHOMEINSTALL is not set anywhere in the script. Please post the output from the set command when logged in interactively so we can see your interactive environment (which will be quite radically different from the environment when running from cron).

3) The script includes dodgy numeric comparisons:

if [ "${MSAlive}" -eq 0 ]; then
if [ "${TSAlive}" -eq 0 ]; then
if [ "${TSCheck}" -eq 1 ]; then

In all of these cases, lose the double quotes. You are comparing numbers not strings.

4) Is it safe to assume that the weird output in /tmp/sigcheck.log did not come from the script as posted but only happens if you include . /etc/profile in the script? Executing . /etc/profile in this context is inadvisible because there is no terminal context.
If true, please post the actual output seen in /tmp/sigcheck.log which matces the script posted.

5) The whole script design depends on processing ps -ef every two-minutes. This is madness. Eventually you will hit a moment when the kernel is busy and

ps -ef returns a null  or incomplete response.
Ideally you would record the PID of the process in a file when it starts. Then you only need to query the one PID. ps -fp<pid>

.
At a minimum consider confining to ps -fu<username> where username is the name of the user who owns the crontab.
Within your script, consider code to only react to say three consecutive failures rather than just the one.