Crontab Source File

I recently created a bash script that should run in cron.

After preparing the bash script and working normally, I put it in Cron and found that it was failing. As a second step, I removed all environment dependencies, instead of just file.txt , I specified /home/blah-blah/file.txt

I still found that the script is not working in one step. This step was a data processing tool. The command executed was /bin/blah-blah/processing_tool -parameter $INDEX , where $INDEX is the variable computed in the bash script.

The third step was to add the bash profile as a source at the beginning of the bash script. Voila !!!! The script started working fine from cron.

My question is why this happens even after I removed all the environment dependencies from my script. I also heard that finding a cron job in a bash profile is not recommended. If so, is there any other way I can avoid this.

+4
source share
2 answers

Basically: everything that started with cron starts with a completely blank slate. You cannot make any assumptions about the contents of environment variables or whatever folder the folder is at the beginning of any script run from cron.

The easiest solution: cd to the correct directory to make sure your path is in the right place. source / etc / profile to make sure you set the system environment variables. source ~ myuserid / .profile to read your personal environment settings. (~ / .profile will not work, as this points to the cron user.) Then start the actual script. Of course, the above approach requires the cron process to have read access to your home-dir, and it probably did a lot more work that is really needed.

A bit more complicated: specify which environment variables the script requires and everything that the script calls. Explicitly export them at the beginning of the cron script.

(Ps replace / etc / profile and ~ myuserid / .profile with any appropriate files for your selection shell.)

+2
source

A cron can be seen as a separate user. Thus, this β€œuser” may not β€œsee” or β€œread” the same files as you. Therefore, it is important that all path names, etc. They were defined in absolute terms.

Each script works in its own process. So, when you run the script, you can change $SHELL and any other variable inside, but it will be lost as soon as you exit it. I assume that the calculation of the $INDEX variables may have been calculated in the script successfully, but its use outside the script may have failed. Without additional information about what kind of work was or what you wanted to do, it is difficult to say.

There are two ways to run a cron job:

  • As root, you can run su -user -c < job > in root crontab .
  • The distortion of your profile is clearly how you did it.
  • You can also set environment variables inside crontab.
  • As a user in a custom crontab, you can run it like this: "/home/blah/.profile && myScript"

However, there should be something in your environment variables (other than file extensions) that are not present when running the cron job. You will need to execute this script using -x flag (in bash) and then process the result. Using diff between your environment variables and the root/cron parameter may be a pointer. Also, check to see if there are any utilities that are used in your scripts whose locations are not part of the $PATH variable for cron / root.

0
source

All Articles