script working manually but not via crontab?

Tag: bash , shell , scripting , crontab Author: dfdf2l Date: 2011-09-18

Wondered if anyone had any idea why the following problem is occurring, or had any tips where to look…I can run the shell script manually in ssh, but if I set it up to run in crontab i get the problems below.

Server is: FreeBSD 8, and I have access to all root permissions

I have a shell script (Bourne) that runs under the “root” permissions using crontab with the following command:

* * * * * /data/backups/scripts/ > /data/backups/logs/cron_logs/server_log_check.sh_cron.log

The “” script checks to see if “the report server” is running with this command:

if ps -xauww | grep -v grep | grep java | grep www > /dev/null
                            #“reports are running, no need to try to restart it”
                            /usr/local/etc/rc.d/tomcat55 start     #start report server because it is not running

The problem is occurring on this line: “/usr/local/etc/rc.d/tomcat55 start”, when the script is run using crontab, but if I run the script manually via ssh this line of code runs without a problem, but all the rest of the code in the script executes fine, just not this line. Allternatively, if I paste this line /usr/local/etc/rc.d/tomcat55 start into the ssh command prompt, it runs just fine too.

I changed the “” ownership to be “root”, but that didn’t make a difference, and the script "tomcat55" ownership is "www". The crontab entry is being made under the "Root" profile, so, I assumed there is no problem running a file that is owned by a lessor permission such as "www" has

Do you have any ideas why cron is doing this?

Thanks in advance

What error message, if any, are you getting?
You have a capital E in the else.. is that a copy and paste error or the actual script?
no error that I was aware of...the capital "E" is my typo.

Best Answer

Try adding the following which will add the error to the log file as well:

* * * * * /data/backups/scripts/ > /data/backups/logs/cron_logs/server_log_check.sh_cron.log 2>&1

Also change this:

/usr/local/etc/rc.d/tomcat55 start  


cd /home/root

nohup /usr/local/etc/rc.d/tomcat55 start &

This should create a nohup.out in /home/root.


Kevin, thanks for your tip to send the error into the log file. I found that inside the "tomcat55" script the "chown" command needed to have the full path "/usr/sbin/chown" instead of just "chown" in order for the crontab to run it. Everything works great! 4 hours yesterday spent...a one minute change you suggested was all I needed!. P.S, I couldn't get the "nohup" to write anything out, but I guess it doesn't matter now. Thanks again!
Glad it worked ! The nohup should create a nohup.out in the directory where it was invoked. I guess it does not matter now, but you might need it in the future.