Scheduling a python 3.6 script in using crontab/cron

后端 未结 2 636
逝去的感伤
逝去的感伤 2021-01-22 05:22

I\'m just setting up a cron tab/job on my Cent OS developement server.

Within my crontab I have the following. (Ignore the time setting, this was added about 15:32 UTC s

2条回答
  •  一生所求
    2021-01-22 06:03

    Looks like you're trying to change directory in crontab, just like omu_negru said you'll need to use full path instead. That's because crontab even if running under your name will not inherit your environmental variables such as $PATH.

    Try this. First, go to the directory where your script is... and turn your main.py to an executable file so you don't have to call python main.py anymore. The simplest way to do so is...

    $ chmod u+x main.py
    

    Now if you do ls -l you'll see that you have "x" in the user section of permissions which will allow you to run it directly.

    -rwxr--r-- 1 user user 0 Aug 17 17:55 main.py
    

    Now you're ready to simplify crontab syntax to something like this...

    34 15 * * * /foo/bar/welcomeclient-0.0.5/main.py 
    

    I also like to capture the output from the script to a log file so it's easier to troubleshoot when things don't work our as planned, as follows:

    34 15 * * * /foo/bar/welcomeclient-0.0.5/main.py & >> /foo/bar/main.log 
    

    The log should be added to log rotation, otherwise it will keep filling up and eventually make your system run out of space, but that's another topic already addressed on this site.

提交回复
热议问题