shell script not running via crontab, runs fine ma

2019-06-19 02:06发布

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?

6条回答
放荡不羁爱自由
2楼-- · 2019-06-19 02:21

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?

查看更多
够拽才男人
3楼-- · 2019-06-19 02:25

Try specifying the full path to the jar file:

/usr/bin/java -jar /path/to/Pharmagistics_auto.jar -o
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-06-19 02:32

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?

查看更多
啃猪蹄的小仙女
5楼-- · 2019-06-19 02:37

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.

查看更多
Evening l夕情丶
6楼-- · 2019-06-19 02:37

"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

查看更多
爷的心禁止访问
7楼-- · 2019-06-19 02:39

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
查看更多
登录 后发表回答