views:

1489

answers:

6

Hey guys,

Before you guys jump on me for asking this question which is asked a million times over and over.

Yes 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?

If you need more info please let me know and I will make sure to include them. Thanks.

Thank's to all that helped me on this. Ultimately Dennis' comment got it going thankfully. I truly appreciate all of your time and effort.

+1  A: 

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.

ZeissS
I have tried MAILTO=myemailbut for some reason I have been receiving no emails. I've also checked cron logs /var/log/cronand it does seem to be executing the script, just not the java portion of it.
astro
+1  A: 

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
Kaleb Pederson
1. I have removed the --, to note I have also tried adding -l to no avail.2. MAILTO for some reason is not sending me any emails, I am not sure why this would be the case. I have added the [email protected] right before my actual cronjob text and when i crun crontab cron.txt it is digested just fine but still no emails.3. my script is running and it is executing shell commands just fine, it is only the java portion that is not executing, yet not throwing me any errors.4. what do you mean here? set something before and after the portions of code i am questionable about?
astro
@astro - RE #2 - set it to a user account on the local machine, RE #3 - Thanks for clarifying. RE #4 - By adding the `set -x` command to the script, bash will show you what it's executing. `set -v` provides further information on command evaluation and expansion.
Kaleb Pederson
2. not sure if emails have been setup for this box unfortunately. 4. I added the set -v to the script and still no luck. I get absolutely nothing. I have changed the script to the following /usr/bin/java -jar Pharmigistics.jar -o > javalog.txt and the javalog is always empty.I don't see any error messages anywhere, the /var/log/cron tells me it is executing my script yet, that one portion of my script simply does nothing.
astro
+1  A: 

"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

robmandu
that was a typo thanks' for pointing that out.
astro
A: 

Do you define necessary paths and env vars in your personal .profile (or other script)? Have you tried sourcing that particular file (or is that what you're doing already with /root/.bash_profile?)

Another way of asking this is: are you certain that whatever necessary paths and env vars you expect are actually available?

If nothing else, have you tried echo'ing individual values or just using the "env" command in your script and then reviewing the stdout?

robmandu
I believe I am sourcing the file with the '. /root/.bash_profile' command.I am not certain, but I have ran a cronjob and have executed a env > conEnv.txt file and then compared the results to my own env command results. The results were obviously very different export paths etc. I then tried adding the following instead of the '. /root/.bash_profile' part: PATH=...[my environments here copied/pasted from the results of running env with my user profile]..; export PATH; and that did not work either.
astro
A: 

Try specifying the full path to the jar file:

/usr/bin/java -jar /path/to/Pharmagistics_auto.jar -o
Dennis Williamson
I just did a forehead slap on that one.Good catch, thank's for the help Dennis that fixed the problem. Truly appreciate it!
astro
A: 

provide full paths to your jar file, and what user are you running the crontab in? If you set it up for a normal user, do you think that user has permission to source the root's profile?

ghostdog74