Shell script does not work through crontab, works fine manually

I tried to export my paths and variables, and crontab still does not run my script. I am sure that I am doing something wrong.

I have a shell script that runs a jar file. This is not working properly.

After reading it, I read this, usually due to incorrect paths due to the fact that cron works through its own shell instance and therefore does not have the same settings as my profile.

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

#!/bin/bash -- . /root/.bash_profile /usr/bin/java -jar Pharmagistics_auto.jar -o ... 

these are the most important parts of the script, the rest are straightforward shells.

Can someone tell me what I'm doing wrong?

+6
linux shell cron crontab
source share
6 answers

Try specifying the full path to the jar file:

 /usr/bin/java -jar /path/to/Pharmagistics_auto.jar -o 
+13
source share

I'll just tell you what you have already ruled out: check your path and environment.

Since you did this, start debugging. Like writing breakpoints to a log file, to find out how far your script gets (even if it is running at all), check the cronjob log file for errors, check your mail (cron sends error messages) and so on ...

Not very specific, sorry.

+3
source share

"exporting my paths and variables" will not work, as crontab is launched in another shell by another user.

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

usr/bin/java

... and I cannot help but notice that you did not indicate the full path. It searches for a directory named "usr" in the current working directory. Often for crontab, cwd is undefined, so your link goes nowhere.

Try specifying the full path from root, for example:

/usr/bin/java

Or, if you want to see an example of a relative path in action, you can also try:

cd /

usr/bin/java

+2
source share

A few thoughts.

  • Remove -- after #!/bin/bash
  • Be sure to redirect the script output visible to cron to mail or somewhere else where you can view it (e.g. MAILTO = wishUser)
  • Confirm that your script is running and not blocked by another long running script (for example, on the second line, add touch /tmp/MY_SCRIPT_RAN && exit )
  • Debugging the script with set -x and set -v after you know that it really works
+1
source share

Do you define the necessary paths and env vars in your personal .profile (or another script)? Have you tried to find this particular file (or is this what you are already doing with /root/.bash_profile?)

Another way to ask the question: are you sure that all the necessary paths and env vars that you expect are really accessible?

If nothing else, did you try echoing the individual values ​​or just use the "env" command in your script and then look at stdout?

0
source share

provide the full path to your jar file and to which user are you using crontab? If you configured it for a regular user, do you think that the user has permission to source the root profile?

0
source share

All Articles