7

I have tried exporting my paths and variables and crontab still will not run my script. I'm sure I am doing something wrong.

I have a shell script which runs a jar file. This is not working correctly.

After reading around I have read this is commonly due to incorrect paths due to cron running via its own shell instance and therefore does not have the same preferences setup as my profile does.

Here is what my script looks like today after several modifications:

#!/bin/bash --

. /root/.bash_profile

/usr/bin/java -jar Pharmagistics_auto.jar -o

...

those are the most important pieces of the script, the rest are straightforward shell based.

Can someone tell me what I am doing wrong?

4

6 回答 6

14

Try specifying the full path to the jar file:

/usr/bin/java -jar /path/to/Pharmagistics_auto.jar -o
于 2010-02-08T23:40:34.600 回答
3

I would just tell you what you have already ruled out: Check your path and environment.

Since you have alredy done this, start debugging. Like write checkpoints into a logfile to see how far your script gets (if even started at all), check the cronjob log file for errors, check your mail (cron sends mails on errors) and so on ...

Not very specific, sorry.

于 2010-02-08T21:27:25.950 回答
2

"exporting my paths and variables" won't work since crontab runs in a different shell by a different user.

Also, not sure if this is a typo in how you entered the question, but I see:

usr/bin/java

...and I can't help but notice you're not specifying the fully qualified path. It's looking for a directory named "usr" in the current working directory. Oft times for crontab, the cwd is undefined, hence your reference goes nowhere.

Try specifying the full path from root, like so:

/usr/bin/java

Or, if you want to see an example of relative pathing in action, you could also try:

cd /

usr/bin/java

于 2010-02-08T21:34:27.170 回答
1

A few thoughts.

  1. Remove the -- after the #!/bin/bash
  2. Make sure to direct script output seen by cron to mail or somewhere else where you can view it (e.g. MAILTO=desiredUser)
  3. Confirm that your script is running and not blocked by a different long-running script (e.g. on the second line, add touch /tmp/MY_SCRIPT_RAN && exit)
  4. Debug the script using set -x and set -v once you know it's actually running
于 2010-02-08T21:30:04.673 回答
0

Do you define necessary paths and env vars in your personal .profile (or other script)? Have you tried sourcing that particular file (or is that what you're doing already with /root/.bash_profile?)

Another way of asking this is: are you certain that whatever necessary paths and env vars you expect are actually available?

If nothing else, have you tried echo'ing individual values or just using the "env" command in your script and then reviewing the stdout?

于 2010-02-08T21:53:05.837 回答
0

provide full paths to your jar file, and what user are you running the crontab in? If you set it up for a normal user, do you think that user has permission to source the root's profile?

于 2010-02-09T00:00:48.577 回答