开发者

Crontab Source File

Recently I created a bash script which I am supposed to run in cron.

After preparing the bash script and its normal working, I put it in Cron and found that i开发者_开发问答t was failing. As as second step , I removed all the environment dependencies i.e instead of just file.txt, I specified /home/blah-blah/file.txt

I still found the script to be failing still at one step. The step was a data processing tool. The command i executed was /bin/blah-blah/processing_tool -parameter $INDEX where $INDEX is a variable calculated within the bash script.

Third step was to add the bash profile as source at the beginning of the bash script. Voila!!!! The script started executing perfectly from cron.

My question is why is this happening even after I removed all the environment dependencies from my script. Also I have heard that sourcing a cron job to a bash profile is not recommended. If so, Is there any other way in which I can avoid doing this.


Basicly: Anything started from cron starts with a totally clean slate. You can make no assumptions whatsoever about the content of environment variables or whichever folder is the current folder at the start of any script run from cron.

Easiest solution: cd to the desired directory to make sure your path is in the desired location. source /etc/profile to mak sure you get the system wide environment variables setup. source ~myuserid/.profile to read your personal environment settings. (~/.profile won't work as that would indicate the cron user.) Then start executing the actual script. Of course the approach above requires the cron process to have read access to your home-dir adn it's probably doing a lot more work thatn is actually required.

Slightly more complicated: Figure out which environment variables are required by the script and anything that gets called by the script. Explicitly export these at the beginning of the cron script.

(P.s. replace /etc/profile and ~myuserid/.profile with whatever are the corresponding files for your shell of choice.)


A cron can be thought of as a separate user. So, this "user" may not "see" or "read" the same files as you do. It is thus essential that all path names etc. be defined in the absolute.

Every script runs within its own process. So, when you run a script, you can change the $SHELL and any other variable within but it will be lost once you get out of it. My guess is that the $INDEX variable computation may have had been computed within the script successfully but its use outside of the script may have failed. Without more information about what job it was, or what you wanted to do, it is hard to tell.

There are two ways to run a cron job:

  1. As root, you can run su -user -c < job > in root crontab.
  2. Sourcing your profile explicitly, as you have done.
  3. You can also set environment variables within the crontab.
  4. As user in the user crontab, you can run it like so: "/home/blah/.profile && myScript"

That said, there HAS to be something in your environment variables (apart from file extensions) that is not present when you run the cron job. You will have to execute that script with -x flag (in bash) and then pore over the output. Using a diff between your environment variables and that of root/cron might be a pointer. Also, check if there are some utilities that are being used in your scripts whose locations are not part of the $PATH variable for cron/root.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜