Bash Shell – Command Runs Fine at Prompt but Not in .sh Script

bashshell

I'm trying to write a script that restarts tomcat, but can't figure out what my problem is.

if I execute the following command at the command prompt it runs fine:

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

However, inside myshell.sh script I tried to run the same command with and without backticks as follows:

rc=`/usr/local/etc/rc.d/tomcat7 start`
rc=/usr/local/etc/rc.d/tomcat7 start
rc=$(/usr/local/etc/rc.d/tomcat7 start)

but nothing happens and I don't get any errors output.

What can it run at the command prompt, but not inside my shell script?

Thanks in advance.

Best Answer

Since you said you were experiencing this issue when running the script through cron, here are a couple ideas.

99% of the problems that arise when running scripts through cron are because of $PATH. The path used by cron is very minimal. You do specify the full path to the init script, so I wouldnt expect this to be the issue. Your script doesnt do anything else does it?

Also, in your script, if you are using bash, you can change the first line (the shebang) and add a second line as follows

#!/bin/bash -x
exec &>/tmp/script.log

This will enable debugging when cron runs the script. It will also log all output from the script to /tmp/script.log. Once you solve the issue you can revert this back.