0

I've been running a shell script every minute between Monday and Friday between business hours, and thought it was running fine, as I can see it when I use the "crontab -l" command. The script calls a piece of Java code which checks certain conditions in a postgresql table, and if those conditions are met, it writes out to another table. The script is in the following directory on our Linux server.

/var/lib/tomcat7/webapps/sh

So lets say it is called "test.sh", should my cronjob look like this this:

* 08-18 * * 1-5 /var/lib/tomcat7/webapps/sh/test.sh

ie, it should run every minute of every day from Monday to Friday between 8 am and 18:59 pm. I just tried testing it by adding dummy data to the postgresql table so that the other table should have been written to and nothing happened. However when i ran ./test.sh manually from the command line it worked fine. The issue seems to be with my cron. If I type:

grep CRON /var/log/syslog

I can see that it has run every minute today. The output looks like this

Nov 25 12:38:01 webserver CRON[11868]: (ubuntu) CMD (/var/lib/tomcat7/webapps/sh/test.sh)
Nov 25 12:39:01 webserver CRON[11888]: (ubuntu) CMD (/var/lib/tomcat7/webapps/sh/test.sh)
Nov 25 12:40:01 webserver CRON[11953]: (ubuntu) CMD (/var/lib/tomcat7/webapps/sh/test.sh)
Nov 25 12:41:01 webserver CRON[11953]: (ubuntu) CMD (/var/lib/tomcat7/webapps/sh/test.sh)
Nov 25 12:42:01 webserver CRON[11985]: (ubuntu) CMD (/var/lib/tomcat7/webapps/sh/test.sh)

etc.. However after I altered the data in the postgresql table this did not seem to have any effect. It's as if the script isn't actually running. Although it runs perfectly when I run it manually from the command line. Has anyone ever experienced this sort of thing with cron jobs, or have any advice as to why this might be happening? Any assistance would be appreciated. Thanks in advance!

tasslebear
  • 199
  • 1
  • 4
  • 12
  • 1
    Maybe there are relative path in your script, If you try `/var/lib/tomcat7/webapps/sh/test.sh` Is it working ? – Duffydake Nov 26 '14 at 11:17
  • Hi Duffydake. Your right, there is a relative path in the script i'm trying to execute. The script starts off by moving into a directory where my java code is located – tasslebear Nov 26 '14 at 11:54
  • Apologies, I didn't mean to hit return so soon. The first line reads "cd ../java/JProject/src" and then proceeds to compile and run the java in the rest of the script. Could this be the reason it isn't running from cron? – tasslebear Nov 26 '14 at 11:56
  • Yes, your working directory is where you launch the script, so you need to change your path or get the directory where the script are and then : `cd $myScriptlocation` – Duffydake Nov 26 '14 at 12:11
  • Thats super. I just changed the relative path in the script to the absolute path, and hey presto!! I wouldn't have come to that conclusion on my own... I just upvoted your answer. Many thanks again for the input. – tasslebear Nov 26 '14 at 12:18
  • no problem. I added an answer (comments are not an answer), so you can accept it. – Duffydake Nov 26 '14 at 12:30

1 Answers1

0

Your working directory is not /var/lib/tomcat7/webapps/sh but / when you launch your script with /var/lib/tomcat7/webapps/sh/test.sh

You can get the current working directory with pwd command or get the script location (see this post and then :

cd $SCRIPTPATH
cd ../java/JProject/sr

OR

change relative path ../java/JProject/src with the absolute path

Community
  • 1
  • 1
Duffydake
  • 917
  • 7
  • 18