I'm trying to get cron to call in the correct PATHs. When I run a Python script from shell the script runs fine as it uses the PATHs set in bashrc but when I use cron all the PATHs are not used from bashrc. Is there a file I can enter the PATHs into for cron like bashrc or a way to call the PATHs from bashrc?
Sorry I don't think I worded this correctly, I can get the correct script to run (meaning the PATH to the script in crontab is not the problem here), it's just when that script is running I run a build and this uses the PATHs set in .bashrc
. When I run the script when I'm logged in, the .bashrc
PATHs are pulled in. Since cron doesn't run in a shell per say it does not pull in .bashrc
. Is there a way of pulling this in without having to write a bash script wrapper?
Setting PATH right before the command line in my crontab worked for me:
I used
/etc/crontab
. I usedvi
and entered in the PATHs I needed into this file and ran it as root. The normal crontab overwrites PATHs that you have set up. A good tutorial on how to do this.The systemwide cron file looks like this:
Problem
Your script works when you run it from the console but fails in cron.
Cause
Your crontab doesn't have the right path variables (and possibly shell)
Solution
Add your current shell and path the crontab
Script to do it for you
Source
https://github.com/ssstonebraker/braker-scripts/blob/master/working-scripts/add_current_shell_and_path_to_crontab.sh
Sample Output
I know this has been answered already, but I thought that his would be useful to some. I had a similar issue that I recently solved (found here) and here are the highlights of the steps I took to answer this question:
make sure that you have the variables you need in PYTHONPATH (found here and here and for more info here) inside the .profile or .bash_profile for any shell you want to test your script in to make sure it works.
edit your crontab to include the directories needed to run your script in a cron job (found here and here)
a) be sure to include the root directory in the PATH variable (.) as explained here (basically if you are running an executable with your command it needs to be able to find root or the directory where the executable is stored) and probably these (/sbin:/bin:/usr/sbin:/usr/bin)
in your crontab file, create a cronjob that will change directory to the directory where you have successfully ran the script before (i.e. Users/user/Documents/foo)
a) This will look like the following:
Most likely, cron is running in a very sparse environment. Check the environment variables cron is using by appending a dummy job which dumps
env
to a file like this:Compare that with the output of
env
in a normal shell session.You can prepend your own environment variables to the local crontab by defining them at the top of your crontab.
Here's a quick fix to prepend
$PATH
to the current crontab:The resulting crontab will look similar to chrissygormley's answer, with PATH defined before the crontab rules.
Adding a PATH definition into the user crontab with correct values will help... I've filled mine with just:
And it's enough to get all my scripts working... Include any custom path there if you need to.