简体   繁体   中英

Crontab, python script fails to run

I have a bash script to automate few things I do. The bash calls 2 python scripts, If I run the bash script normally, everything runs, no errors what so ever. I set up a cron job to Automate this and when I checked the logs I noticed the python scripts don't run at all. It gives me the following error.

python: can't open file 'movefiles.py': [Errno 2] No such file or directory

python: can't open file 'create_log_shimmer.py': [Errno 2] No such file or directory

Both these files exist and runs when calling the bash script directly.

The working directory of the cron is different from the directory you run the script directly.

  • Make your bash script to use absolute path for python script files.

  • Or make the bash script to change directory to where you run the script directly.

Problems: 1) Crontab default path is somewhere else

2) IN python .py file you haven't mentioned the complete file names with in the code [in case you used]

**3) Specify the complete path in the cron file while running **

BELOW SHOWS THE BEST TO CREATE THE CRON (NOT THE BEST BUT FOR ME ITS EASY AS"

Best way to have a cronjob (crontab is )

1) create a file in that directory by vi FILENAME.cron

2) in the filename type in the following (This sets the current path to be the cron path)

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

* * * * * python ___________.py

3) Now keep in mind that save it and to start the cron type crontab FILENAME.cron. ( In you python .py code, if you are accessing any file or writtin to a file or database provide the full path in that place)

4) You would be able to run the cron with ease.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM