shell script not running via crontab, runs fine manually

42,830

Solution 1

Try specifying the full path to the jar file:

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

Solution 2

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.

Solution 3

"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

Solution 4

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
Share:
42,830
astro
Author by

astro

Updated on March 01, 2020

Comments

  • astro
    astro about 4 years

    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?