1

I need to have my Java program run on a linux box once a day. So I created a simple file with just one line:

java -jar /opt/location/my_jar.jar

and put it in etc/cron.daily, assuming it would run once a day. But it doesn't run at all. I tried both to have the .sh extension, or simply the file name with no extension. Still, no luck.

I googled it and I'm getting quite a bit of conflicting info. Can someone please help?

EDIT:

I'm summarizing the place it is right now, based on the answers given by Satish and Mithrandir.

1.I created the run_conversions script using vi, to get over the problem with the end of line character on windows. Now the script is

#!/bin/sh
/usr/bin/java -jar /opt/location/my_jar.jar

2.I put it in /etc/cron.hourly.

3.Checking the log at /var/log/cron I'm seeing the it's starting run_conversions and finishing run_conversions every hour. So far so good.

4.But it doesn't seem like my jar file is running. I know this because when it's running properly it should update a database -- and the database is not updating.

5.Here's the strange thing: when I'm running cron.hourly manually, but calling

run-parts /etc/cron.hourly

the jar file is hit propertly, and the database is updating.

To sum it up: when running it through run-parts, it works. When leaving it to run hourly by itself, it doesn't.

Any ideas?

EDIT 2:

Following advice from Satish, Mithrandir and vahid I changed my run_conversions_loc to look like this:

#!/bin/bash
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bexport SHELL=/bin/bash
/usr/bin/java -jar /opt/tf/conversions/aff_networks2.jar > /opt/tf/conversions/runconversions.txt

I removed the script from cron.hourly and added this line to crontab:

*/10 * * * * /opt/tf/conversions/run_conversions_loc

The script now runs every 10 minutes, and is registered in the cron log like this:

Feb 24 09:30:01 backsome CROND[7933]: (root) CMD (/opt/tf/conversions/run_conversions_loc)

So far it looks good. But the database -- which it should update -- isn't updated.

Looking deeper into it, the jar file, aff_networks2.jar is looking for an ftr.properties file in the local directory -- the same directory where it's at. The file exists in this directory. But it's not read properly. I know this because in the output file, runconversions.txt, the value that should be read from the properties file is null.

Two things to complete the picture:

  1. Everything in the conversions directory has 777 permissions. I know it's not recommended to give such extended permissions but I wanted to make sure (at least try) it's not the issue.

  2. When I run the script from the shell by calling ./run_conversions_loc it runs, finds the properties file, and updates the database. I am logged into the shell as root, and I also created all the relevant files as root, and installed as root the line for calling the script in crontab.

Any ideas why isn't cron reading the properties file?

14
  • 1
    did you check /var/log/cron ?
    – Satish
    Feb 21, 2013 at 17:19
  • Thanks for the tip, Satish. I checked it now and my script isn't mentioned there, although now I put it both in cron.daily and cron.hourly, and followed Mithrandir advice below. Any idea what's going on?
    – Eddy
    Feb 22, 2013 at 11:16
  • 1
    create a shell script like @Mithrandir told and run cron manually on command like: run-parts /etc/cron.daily and check your log
    – Satish
    Feb 22, 2013 at 14:37
  • 1
    what is your OS? and shell use are using? try #!/bin/bash one more thing also use full path instead of java. use /usr/bin/java wherever your java located.
    – Satish
    Feb 22, 2013 at 16:19
  • 1
    Ah! got it is your editor issue, try this command: dos2unix scriptname.sh also check more detail here stackoverflow.com/questions/2920416/…
    – Satish
    Feb 22, 2013 at 16:38

3 Answers 3

3

Solution with example:

Run command manually on command line:

[root@04 cron.daily]# /usr/bin/java -jar /root/HelloWorld.jar
Hello World #1

Let create a script in /etc/cron.daily/test.sh and give execute permission:

#!/bin/bash
/usr/bin/java -jar /root/HelloWorld.jar

Notes: run dos2unix in case you have dos character issue or error /bin/bash^M: bad interpreter: No such file or directory

[root@04 cron.daily]# unix2dos test.sh
unix2dos: converting file test.sh to DOS format ...

Test it, voila!!

[root@04 cron.daily]# run-parts /etc/cron.daily
/etc/cron.daily/ldiscan:

kcore: Value too large for defined data type
/etc/cron.daily/test.sh:

Hello World #1
6
  • Thank you, Satish. You got me closer, it seams, but still not entirely there. I edited my question with more info.
    – Eddy
    Feb 23, 2013 at 11:18
  • Lets put that script in crontab with * * * * * <script name>
    – Satish
    Feb 23, 2013 at 12:10
  • Satish can you elaborate? I'm not sure what that means.
    – Eddy
    Feb 23, 2013 at 12:20
  • run "crontab -e" command and add line * * * * * /path/of/your/run_conversions check help here thegeekstuff.com/2009/06/15-practical-crontab-examples
    – Satish
    Feb 23, 2013 at 13:57
  • I acted on your advice, and edited the question with some more info (EDIT 2.) Would be great if you could take a look.
    – Eddy
    Feb 24, 2013 at 10:11
2

its probably your environment variables

does it work as the current user logged in when executing the script ?

if so

run:

env|egrep "(^PATH=|^SHELL=)"|awk '{print "export "$1}'

then take the output and put it on the top of your script and try another cron in 2 minutes from now to see if it worked

Updated answer in response to Eddy's Comment 24th Feb 2013.

I want to give you a crash course on crontab.

  1. setting up crontab can be done via global /etc/crontab or under user as crontab -e (to edit specific user's cron) or crontab -l (lists - stored in /var/spool/cron for each user)

I see you are trying to attempt a run ever 10 minutes which is fine in /etc/crontab

The reason why I suggested giving the entire class path of your current shell is because most of the time the script is trying to use a unix command that is not available as part of the crontab's PATH (which resides right at the very top of /etc/crontab file itself)

To debug path issues its usually a good idea to watch the mailbox of the user that crontab is executing the task as : so tail -100 /var/spool/mail/root and looking out for any messages related to that cron task as well as the cron logs itself as someone has suggested -

I do not think your problem is paths here though..

You are trying to run a java jar file and it may be that your jar file needs other files in that conversion folder and that when you are running it you are already in that folder....

so in your script you could run

cd /opt/tf/conversions/;
 /usr/bin/java -jar aff_networks2.jar > /opt/tf/conversions/runconversions.txt

But since this is such a small script you could get away with placing the entire thing as a cron entry and bypassing a shell script altogether something like this

*/10 *    * * *   root cd /opt/tf/conversions/; /usr/bin/java -jar aff_networks2.jar > /opt/tf/conversions/runconversions.txt

Hope this helps solve this issue

4
  • This is the output: export SHELL=/bin/bash export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin But what does it mean? And should I put it BEFORE or AFTER the line that says #!/bin/sh ?
    – Eddy
    Feb 22, 2013 at 9:37
  • this is your current path and your current shell as the user you are logged in as. Put it after #!/bin/sh. This will then export out the same paths within your script running as part of cron. If this works you could look at the top few lines of your crontab file and ensure the path matches or has the missing path that is listed in your current path - which makes the script work whilst you are logged in but not whilst running in crontab
    – V H
    Feb 22, 2013 at 10:43
  • I acted on your advice, and edited the question with some more info (EDIT 2.) Would be great if you could take a look.
    – Eddy
    Feb 24, 2013 at 10:11
  • Thank you very much, vahid. You got it!
    – Eddy
    Feb 24, 2013 at 20:18
0

Change your file to:

#!/bin/sh
/usr/bin/java -jar /opt/location/my_jar.jar

Check if /usr/bin/java is actually where the command is istalled (which java). Change the permissions of you file to be executable:

chmod +x /opt/location/my_jar.jar
4
  • Thanks, Mithrandir. I'll try that. Does it matter anything if the extension if .sh, or no extension?
    – Eddy
    Feb 21, 2013 at 16:53
  • 1
    the first line defines the script and not .sh or .whatever i.e. you could call it script.something and so long as the shell is set on the top line of the script it will work
    – V H
    Feb 21, 2013 at 16:56
  • 1
    It actually matters -- at least on debian, cron.daily is run by run-parts(8), which will skip filenames containing a dot
    – loreb
    Feb 21, 2013 at 17:14
  • I edited the question with some more info (EDIT 2.) Would be great if you could take a look.
    – Eddy
    Feb 24, 2013 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.