1

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.

3
  • 1
    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.
    – phemmer
    Feb 17, 2012 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, 2012 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/server_log_check.sh 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, 2012 at 16:08

2 Answers 2

3

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.

1
0

Why do you try to assign this to a variable?

Just

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

2
  • 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.
    – phemmer
    Feb 17, 2012 at 5:48
  • that's what I thought as well. I've tried with and without the variable...it 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, 2012 at 15:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .