Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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.

share|improve this question
How are you testing this? Are you running the script by hand, or are you running it through cron or something else? Also if this is bash, you can debug the script by running it as bash -x /path/to/script to see exactly what its doing. – Patrick Feb 17 '12 at 5:50
its being run by a cronjob. I'll try your suggestion of debugging and see if I can find anything. – Ronedog Feb 17 '12 at 15:34
Ok, here's what I learned. If I run the shell script at the command prmpt using: bash -x /data/backups/scripts/ then the tomcat7 start command runs just fine inside the script. But if I let the cronjob run this script for me that tomcat7 start command does not get executed...the debugging output didn't really help to figure out why it doesn't work in cron...any ideas? – Ronedog Feb 17 '12 at 16:08
up vote 2 down vote accepted

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.

share|improve this answer
you were right...i found another thread over here: and tried their suggestion of the $PATH...I put that in the top of my crontab file and everything works now. Thanks for helping out. – Ronedog Feb 17 '12 at 16:26

Why do you try to assign this to a variable?


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

in the script should do the trick. If you should need the textual output of this command, you can redirect it into a file with the > operator and you can use the $? special variable to check the return code of the command.

share|improve this answer
This is true, but storing output in a file is often messy. Because now you have to worry creating a unique file name, and then cleaning up that file when youre done. var="$(foobar)" is often much simpler. The only time I would use files is if the command is going to dump out a massive amount of text. – Patrick Feb 17 '12 at 5:48
that's what I thought as well. I've tried with and without the doesn't seem to matter if I run the command directly, or with backticks, etc...I get the same result. but when at command prompt it runs fine...i'm confused. – Ronedog Feb 17 '12 at 15:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.