简体   繁体   中英

crontab in mac is not working properly for python script

crontab -l:

(base) yan@0053 notebooks % crontab -l
* * * * * /opt/anaconda3/bin/python /Users/yan/Documents/pol/coop/analysis/test.py
* * * * * echo "hello" >>/Users/yan/Documents/pol/coop/analysis/hello.txt

Does NOT work in crobtab: 
* * * * * /opt/anaconda3/bin/python /Users/yan/Documents/pol/coop/analysis/test.py

Does work in crontab:
* * * * * echo "hello" >>/Users/yan/Documents/pol/coop/analysis/hello.txt


BUT this python script works in Terminal, and i have the output file saved in the folder: 
(base) yan@0053 notebooks % /opt/anaconda3/bin/python /Users/yan/Documents/pol/coop/analysis/test.py

I am wondering how to debug python.py in crontab. I can't manually run it in terminal...

My test.py:

import pandas as pd
df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
                   'mask': ['red', 'purple'],
                   'weapon': ['sai', 'bo staff']})
df.to_csv('ouoput.csv', index=False)

Basically, shouldn't be a test.py issue, cuz I simply modified the test.py and only include one line print('y'). It works in manually, but doesn't work in crontab

print('y')

This issue was resolved by https://blog.bejarano.io/fixing-cron-jobs-in-mojave/

On MacOS CronTab has been deprecated since 10.4. It's not supported. I recommend shifting away from Crontab to launchd. In short, you create.plist files with your instructions. There's this excellent little app called LaunchControl that's super helpful for running background system scripts and processes. Highly recommend since the syntax and permissions / security settings can be tricky. Also, there's this great little article "How to Use launchd to Run Services in macOS."

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