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?
You should put full paths in your
crontab
. That's the safest option.If you don't want to do that you can put a wrapper script around your programs, and set the PATH in there.
e.g.
becomes:
Also anything called from
cron
should be be very careful about the programs it runs, and probably set its own choice for thePATH
variable.EDIT:
If you don't know where the command is that you want execute
which <command>
from your shell and it'll tell you the path.EDIT2:
So once your program is running, the first thing it should do is set
PATH
and any other required variable (e.g.LD_LIBRARY_PATH
) to the values that are required for the script to run.Basically instead of thinking how to modify the cron environment to make it more suitable for your program/script - make your script handle the environment it's given, by setting an appropriate one when it starts.
Make your variables work for you, this will allow access t
Define your PATH in /etc/profile.d/*.sh
System-wide environment variables
Files with the .sh extension in the /etc/profile.d directory get executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well as by the DisplayManager when the desktop session loads.
You can for instance create the file /etc/profile.d/myenvvars.sh and set variables like this:
Execute crontab with login option!
CRONTAB run script or command with Environment Variables
@Trevino: your answer helped me solve my problem. However, for a beginner, trying to give a step by step approach.
$ echo $JAVA_HOME
$ crontab -e
* * * * * echo $PATH
- this lets you understand whats the PATH value being used by crontab at present. Run crontab and grab $PATH value used by crontab.crontab -e
; b)PATH=<value of $JAVA_HOME>/bin:/usr/bin:/bin
(its a sample path); c) now your scheduled job/script like*/10 * * * * sh runMyJob.sh &
; d) removeecho $PATH
from crontab as its not needed now.The simplest workaround I've found looks like this:
This example invokes
su
as root user and starts the shell with the user's full environment, including $PATH, set as if they were logged in. It works the same on different distros, is more reliable than sourcing .bashrc (which hasn't worked for me) and avoids hardcoding specific paths which can be a problem if you're providing an example or setup tool and don't know what distro or file layout on the user's system.You can also specify the username after
su
if you want a different user than root, but you should probably leave theroot
parameter beforesu
command since this ensuressu
has sufficient privileges to switch to any user you specify.Set the required PATH in your cron
Edit: Press
i
Save and exit
:wq
Should you use
webmin
then these are the steps how to set thePATH
value: