Having problems running shell script from crontab

Yuri Pankov yuri.pankov at gmail.com
Sat Jan 7 13:05:14 UTC 2012


On Sat, Jan 07, 2012 at 02:21:51PM +0200, Kaya Saman wrote:
> Hi,
> 
> I wrote a shell script to detect if the port of tomcat was in use or not 
> then restart if the port wasn't online; due to tomcat segfaulting as my 
> system hasn't got enough memory for it.
> 
> 
> This is the shell script:
> 
> 
> #!/usr/local/bin/bash
> ntstat=`netstat -ap tcp | grep 8180 | sed -n '1p'`

sockstat would be more useful here.

> port="8180"
> 
> #echo $ntstat
> #echo $port
> 
> if [[ $ntstat =~ $port ]]; then
>     echo "Output of Netstat command $ntstat port number $port" > 
> /root/java_restart/java_restart.log;
> else
>     wait 60; /usr/local/etc/rc.d/tomcat6 restart;

Are you sure you want 'wait' here (or should it be 'sleep')?

> fi
> 
> 
> 
> Here in /var/log/cron - it can be seen that the script has been executed:
> 
> Jan  7 10:30:00 wiki /usr/sbin/cron[19509]: (root) CMD 
> (/root/java_restart/java_restart.sh)
> Jan  7 11:00:00 wiki /usr/sbin/cron[20418]: (root) CMD 
> (/root/java_restart/java_restart.sh)
> Jan  7 11:30:00 wiki /usr/sbin/cron[21356]: (root) CMD 
> (/root/java_restart/java_restart.sh)
> Jan  7 12:00:00 wiki /usr/sbin/cron[22455]: (root) CMD 
> (/root/java_restart/java_restart.sh)
> 
> 
> 
> 
> The strange thing is that if I run this script manually 
> /root/java_restart/java_restart.sh it works fine and does what it's 
> supposed to do. Cron however seems to execute the IF statement but not 
> get as far as else??? - it seems as tomcat doesn't restart.
> 
> Here is my little log file that tells the port is active:
> 
> -rw-r--r--  1 root  wheel  116 Jan  7 11:30 java_restart.log
> 
> 
> If I stop tomcat just before the xx:00 or xx:30 time designations tomcat 
> will not be restarted by the script and I don't understand why?
> 
> 
> This is the crontab: 0,30 * * * * /root/java_restart/java_restart.sh

Try changing it to /usr/local/bin/bash /root/java_restart/java_restart.sh.
 
> which is being run as root user.
> 
> 
> Can anyone suggest anything that might be a possible cause for tomcat 
> not getting restarted automatically when the proper conditions are met?


Yuri


More information about the freebsd-questions mailing list