How to make CRON call the correct PATH

I am trying to get cron to call the correct PATH. When I run the Python script from the shell, the script works fine because it uses the PATHs installed in bashrc, but when I use cron, all PATHs are not used from bashrc. Is there a file where I can enter PATHs for cron, for example bashrc, or a way to call PATH from bashrc?

Sorry, I donโ€™t think I correctly formulated this, I can run the correct script (which means that PATH for the script in crontab is not a problem), itโ€™s just when this script I run the build, and it uses the PATH installed in .bashrc . When I run the script, when I logged in, PATH gets pulled into it. .bashrc . Since cron does not start in the shell, let's say it does not pull .bashrc . Is there a way to pull this without having to write a bash wrapper script?

+100
linux path cron
Mar 05 '10 at 16:05
source share
15 answers

I used /etc/crontab . I used vi and went into the PATH that I needed in this file and ran it as root. Normal crontab overwrites the PATH that you configured. A good tutorial on how to do this .

The cron system file looks like this:

 This has the username field, as used by /etc/crontab. # /etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the 'crontab' # command to install the new version when you edit this file. # This file also has a username field, that none of the other crontabs do. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # mh dom mon dow user command 42 6 * * * root run-parts --report /etc/cron.daily 47 6 * * 7 root run-parts --report /etc/cron.weekly 52 6 1 * * root run-parts --report /etc/cron.monthly 01 01 * * 1-5 root python /path/to/file.py 
+145
Mar 09 '10 at 13:40
source share

Most likely, cron works in a very rare environment. Check the cron environment variables using the addition of a dummy job that unloads env into the file as follows:

 * * * * * env > env_dump.txt 

Contrast this with the env output in a regular shell session.

You can add your own environment variables to your local crontab by specifying them at the top of your crontab.

Here's a quick fix for adding $PATH to the current crontab:

 # echo PATH=$PATH > tmp.cron # echo >> tmp.cron # crontab -l >> tmp.cron # crontab tmp.cron 

The resulting crontab will be similar to chrissygormley's answer, with PATH defined before the crontab rules.

+45
Jun 22 2018-12-12T00:
source share

You must put the full paths in your crontab . This is the safest option.
If you do not want to do this, you can put a script wrapper around your programs and set PATH there.

eg.

 01 01 * * * command 

becomes:

 01 01 * * * /full/path/to/command 

Also, everything that is called from cron should be very careful about running programs and probably set its own choice for the PATH variable.

EDIT:

If you donโ€™t know where the command you want to execute which <command> from your shell, and it will 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 ) for the values โ€‹โ€‹needed to run the script.
Basically, instead of thinking about how to change the cron environment to make it more suitable for your program / script, make your script a processed environment specified by the appropriate parameter at startup.

+19
Mar 05 '10 at 16:08
source share

Make your variables work for you, this will allow access t

Define your PATH in the file /etc/profile.d/*. sh

System-wide environment variables

Files with the .sh extension in the /etc/profile.d directory are executed whenever the bash login shell is entered (for example, when entering the system from the console or via ssh), as well as using the DisplayManager when the desktop boots.

You can, for example, create the file /etc/profile.d/myenvvars.sh and set such variables:

 export JAVA_HOME=/usr/lib/jvm/jdk1.7.0 export PATH=$PATH:$JAVA_HOME/bin 

Run crontab with the login parameter!

CRONTAB run a script or command with environment variables

 0 9 * * * cd /var/www/vhosts/foo/crons/; bash -l -c 'php -f ./download.php' 0 9 * * * cd /var/www/vhosts/foo/crons/; bash -l -c download.sh 
+14
03 Oct '14 at 11:37
source share

Installing PATH right before the command line in my crontab worked for me:

 * * * * * PATH=$PATH:/usr/local/bin:/path/to/some/thing 
+13
Apr 15 '14 at 13:22
source share

Problem

Your script works when you run it from the console, but does not work in cron.

Cause

Your crontab does not have the correct path variables (and possibly shell)

Decision

Add current shell and crontab path

Script to do it for you

 #!/bin/bash # # Date: August 22, 2013 # Author: Steve Stonebraker # File: add_current_shell_and_path_to_crontab.sh # Description: Add current user shell and path to crontab # Source: http://brakertech.com/add-current-path-to-crontab # Github: hhttps://github.com/ssstonebraker/braker-scripts/blob/master/working-scripts/add_current_shell_and_path_to_crontab.sh # function that is called when the script exits (cleans up our tmp.cron file) function finish { [ -e "tmp.cron" ] && rm tmp.cron; } #whenver the script exits call the function "finish" trap finish EXIT ######################################## # pretty printing functions function print_status { echo -e "\x1B[01;34m[*]\x1B[0m $1"; } function print_good { echo -e "\x1B[01;32m[*]\x1B[0m $1"; } function print_error { echo -e "\x1B[01;31m[*]\x1B[0m $1"; } function print_notification { echo -e "\x1B[01;33m[*]\x1B[0m $1"; } function printline { hr=------------------------------------------------------------------------------------------------------------------------------- printf '%s\n' "${hr:0:${COLUMNS:-$(tput cols)}}" } #################################### # print message and exit program function die { print_error "$1"; exit 1; } #################################### # user must have at least one job in their crontab function require_gt1_user_crontab_job { crontab -l &> /dev/null [ $? -ne 0 ] && die "Script requires you have at least one user crontab job!" } #################################### # Add current shell and path to user crontab function add_shell_path_to_crontab { #print info about what being added print_notification "Current SHELL: ${SHELL}" print_notification "Current PATH: ${PATH}" #Add current shell and path to crontab print_status "Adding current SHELL and PATH to crontab \nold crontab:" printline; crontab -l; printline #keep old comments but start new crontab file crontab -l | grep "^#" > tmp.cron #Add our current shell and path to the new crontab file echo -e "SHELL=${SHELL}\nPATH=${PATH}\n" >> tmp.cron #Add old crontab entries but ignore comments or any shell or path statements crontab -l | grep -v "^#" | grep -v "SHELL" | grep -v "PATH" >> tmp.cron #load up the new crontab we just created crontab tmp.cron #Display new crontab print_good "New crontab:" printline; crontab -l; printline } require_gt1_user_crontab_job add_shell_path_to_crontab 

Source

https://github.com/ssstonebraker/braker-scripts/blob/master/working-scripts/add_current_shell_and_path_to_crontab.sh

Sample output

add_curent_shell_and_path_to_crontab.sh example output

+9
Aug 23 '13 at 15:39
source share

Adding a PATH definition to a custom crontab with the correct values โ€‹โ€‹will help ... I filled me up simply:

 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 

And that's enough for all my scripts to work ... Include any custom path there if you need to.

+9
Nov 30 '13 at 15:53
source share

The standard environment for cron jobs is very sparse and can be very different from the environment in which you develop python scripts. For a script that can be run in cron, any environment you depend on must be explicitly defined. In the cron file itself, include the full paths to the python executables and your python scripts.

+2
Mar 05 '10 at 16:12
source share

On my AIX, cron will select environment variables from / etc / environment, ignoring what is set in .profile.

Edit: I also checked several Linux boxes of different ages, and they seem to have this file too, so this is most likely not AIX.

I checked this with the joemaller cron clause and checked the output before and after editing the PATH variable in / etc / environment.

+2
Mar 18 '13 at 16:58
source share

I know that an answer has already been given, but I thought it would be useful to some. I had a similar problem that I recently solved ( here) , and here are the main points of the steps I took to answer this question:

  • make sure you have the variables you need in PYTHONPATH (here and here and for more information here) inside .profile or .bash_profile for any shell that you want to test for your script to make sure it works.

  • edit your crontab to include the directories needed to run the script in the cron job (found here and here).

    a) be sure to include the root directory in the PATH (.) variable, as described here (basically, if you are executing the executable with your command, it should be able to find the root or directory in which the executable is stored) and probably they (/ sbin: / bin: / usr / sbin: / usr / bin)

  • in your crontab file, create a cronjob that will change the directory to the directory where you successfully ran the script before (e.g. users / user / documents / foo)

    a) It will look as follows:

     * * * * cd /Users/user/Documents/foo; bar -l doSomething -v 
+2
Jul 16 '13 at 22:27
source share

If you do not want to make the same changes in different places, do something like this:

 * * * * * . /home/username/.bashrc && yourcommand all of your args 

. and then the path to .bashrc and the && team is the magic to change your environment in the bash shell. Also, if you really want the shell to be bash, it's nice to have a line in your crontab:

 SHELL=/bin/bash 

Hope this helps someone!

+1
Jul 22 '13 at 16:41
source share

@Trevino: your answer helped me solve my problem. However, for a beginner trying to give a step-by-step approach.

  • Get current java installation via $ echo $JAVA_HOME
  • $ crontab -e
  • * * * * * echo $PATH - this makes it clear that the PATH value is currently in use by crontab. Run crontab and take the $ PATH value used by crontab.
  • Now edit crontab again to set the desired path in java bin: a) crontab -e ; b) PATH=<value of $JAVA_HOME>/bin:/usr/bin:/bin (its fetch path); c) now your scheduled task / script as */10 * * * * sh runMyJob.sh & ; d) remove echo $PATH from crontab as it is not needed now.
+1
Jun 21 '17 at 9:16 on
source share

The simplest workaround I found is as follows:

 * * * * * root su -l -c command 

This example calls su as the root user and launches the shell with the full user environment, including $ PATH, as if they were logged in. It works the same on different distributions, is more reliable than searching .bashrc (which did not work for me), and avoids hard-coding specific paths, which can be a problem if you provide an example or installation tool and donโ€™t know which distribution or file layout in user system.

You can also specify the username after su if you want the other user to be non-root, but you should probably leave the root parameter in front of the su command as this ensures that su has sufficient privileges to switch to any user that you specify.

+1
Sep 14 '18 at 18:37
source share

Set the required PATH in your cron

 crontab -e 

Change: press i

 PATH=/usr/local/bin:/usr/local/:or_whatever 10 * * * * your_command 

Save and exit :wq

0
Jan 02 '18 at 19:46
source share

If you use webmin then these are the steps to set the PATH value:

 System -> Scheduled Cron Jobs -> Create a new environment variable -> For user: <Select the user name> -> Variable name: PATH -> Value: /usr/bin:/bin:<your personal path> -> Add environment variable: Before all Cron jobs for user 
-2
Oct 11 '17 at 11:09 on
source share



All Articles