How to get CRON to call in the correct PATHs

前端 未结 15 967
时光取名叫无心
时光取名叫无心 2020-11-22 06:07

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 t

相关标签:
15条回答
  • 2020-11-22 06:37

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

    1. Get your current installation of java via $ echo $JAVA_HOME
    2. $ crontab -e
    3. * * * * * 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.
    4. Now edit crontab again to set your desired java bin path: a) 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) remove echo $PATH from crontab as its not needed now.
    0 讨论(0)
  • 2020-11-22 06:39

    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:

    * * * * * env > env_dump.txt
    

    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:

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

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

    0 讨论(0)
  • 2020-11-22 06:39

    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:

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

    Execute crontab with login option!

    CRONTAB run 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
    
    0 讨论(0)
  • 2020-11-22 06:42

    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.

    01 01 * * * command
    

    becomes:

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

    Also anything called from cron should be be very careful about the programs it runs, and probably set its own choice for the PATH 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.

    0 讨论(0)
  • 2020-11-22 06:44

    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:

    1. 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.

    2. 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)

    3. 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:

      * * * * cd /Users/user/Documents/foo; bar -l doSomething -v 
      
    0 讨论(0)
  • 2020-11-22 06:47

    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

    #!/bin/bash
    #
    # Date: August 22, 2013
    # Author: Steve Stonebraker
    # File: add_current_shell_and_path_to_crontab.sh
    # Description: Add current user's 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's crontab
    function add_shell_path_to_crontab {
        #print info about what's 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

    0 讨论(0)
提交回复
热议问题