1

I am trying to run bash script as a cron job every 15 minutes.

The bash script simple calls a Java program. I made an entry in crontab to run the file every 15 minutes and saved it but it would not do anything ?

crontab -e

*/15 * * * * /home/marshell/graphite_cronjobs/Run_Cron_Read_Send_CapacityData_To_Graphite.sh

Run_Cron_Read_Send_CapacityData_To_Graphite.sh

java Cron_Read_Send_CapacityData_To_Graphite  >> Cron_Read_Send_CapacityData_To_Graphite.log
4
  • Are you sure "java" is in the PATH? (and I do not mean the users PATH ;-) )
    – Rinzwind
    Sep 17, 2014 at 14:10
  • I suggest you redirect std output and std error in order to debug your problem. */15 * * * * /home/marshell/graphite_cronjobs/Run_Cron_Read_Send_CapacityData_To_Graphite.sh >> /home/marshell/graphite_cronjobs/debug.log 2>&1
    – Lety
    Sep 17, 2014 at 14:17
  • Hi i tried this ' /usr/lib/jvm/java-1.7.0-openjdk-amd64/jre/bin/java Cron_Read_Send_CapacityData_To_Graphite ' in the bash script now i get a n error Could not find or load main class ?
    – Marsh
    Sep 17, 2014 at 14:52
  • Try to put your cron instructions in /etc/crontab' instead of crontab -e`
    – Harris
    Sep 17, 2014 at 15:23

1 Answer 1

1

There are two possible issues here. First of all, cron runs jobs in a special shell which does not read your personal $PATH. If java is not in /usr/bin or /bin, it will not be available to cron. From man 5 crontab:

Several environment variables are set up automatically by the cron(8) daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner.
PATH is set to "/usr/bin:/bin". HOME, SHELL, and PATH may be overridden by settings in the crontab; LOGNAME is the user that the job is running from, and may not be changed.

So, use the full path to java instead. The next issue is that your bash script calls java on a file with a relative path. It assumes that the script will be launched from the same directory as the java file. Again, this is not the case. So, always use absolute paths for your scripts if you want them to run as expected by cron:

/path/to/java /path/to/Cron_Read_Send_CapacityData_To_Graphite  >> /path/to/Cron_Read_Send_CapacityData_To_Graphite.log

Alternatively, you can set the $PATH in the crontab itself:

PATH=$PATH:/opt/bin

*/15 * * * * /home/marshell/graphite_cronjobs/Run_Cron_Read_Send_CapacityData_To_Graphite.sh
2
  • It is also possible to put a PATH in crontab ;)
    – Rinzwind
    Sep 17, 2014 at 16:10
  • @Rinzwind yes but personally I find using full paths better. In any case they're needed for the input and output files here. Still, I added an example of setting the path in the crontab.
    – terdon
    Sep 17, 2014 at 16:14

You must log in to answer this question.

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