为什么Crontab无法运行我的python脚本?

时间:2019-07-30 18:00:52

标签: python linux cron

我正在尝试创建一个cron作业,该作业每分钟都会添加文本,并且我的代码似乎正在运行。但是,我的脚本无法由cron运行。

我已经尝试在终端中键入此env -i /bin/bash --noprofile --norc来模拟执行我的cron脚本的cron

* * * * * cd /Users/alainpierrerayosparonda/Documents/cronTest &&  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /Users/alainpierrerayosparonda/Documents/cronTest/pythonCron.py # CronTest

它有效!但是,如果我这样执行它

from crontab import CronTab

cron = CronTab(tabfile='filename.tab')
job1 = cron.new(command='cd /Users/alainpierrerayosparonda/Documents/cronTest &&  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /Users/alainpierrerayosparonda/Documents/cronTest/pythonCron.py', comment='CronTest')
job1.minute.every(1)

for item in cron:
    print(item)

cron.write()

它不起作用。

如果有问题,我正在使用Mac。

这是我的代码:

serverr.py

from crontab import CronTab

cron = CronTab(tabfile='filename.tab')
job1 = cron.new(command='cd /Users/alainpierrerayosparonda/Documents/cronTest &&  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /Users/alainpierrerayosparonda/Documents/cronTest/pythonCron.py', comment='CronTest')
job1.minute.every(1)

for item in cron:
    print(item)

cron.write()

pythonChron.py

from datetime import datetime

myFile = open('/Users/alainpierrerayosparonda/Documents/cronTest/append.txt', 'a') 
myFile.write('\nAccessed on ' + str(datetime.now()))

append.txt


Accessed on 2019-07-30 22:56:13.057246
Accessed on 2019-07-30 22:56:29.712203
Accessed on 2019-07-30 23:19:15.792065
Accessed on 2019-07-30 23:22:41.850271
Accessed on 2019-07-30 23:34:51.832505
Accessed on 2019-07-31 00:23:24.417114
Accessed on 2019-07-31 00:30:30.473127
Accessed on 2019-07-31 00:33:37.833229
Accessed on 2019-07-31 00:34:27.308541
Accessed on 2019-07-31 00:37:39.640581
Accessed on 2019-07-31 00:38:17.532211
Accessed on 2019-07-31 00:40:36.648651

filename.tab


* * * * * cd /Users/alainpierrerayosparonda/Documents/cronTest &&  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /Users/alainpierrerayosparonda/Documents/cronTest/pythonCron.py # CronTest
* * * * * cd /Users/alainpierrerayosparonda/Documents/cronTest &&  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /Users/alainpierrerayosparonda/Documents/cronTest/pythonCron.py # CronTest
*/5 * * * * cd /Users/alainpierrerayosparonda/Documents/cronTest &&  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /Users/alainpierrerayosparonda/Documents/cronTest/pythonCron.py # CronTest
*/2 * * * * cd /Users/alainpierrerayosparonda/Documents/cronTest &&  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /Users/alainpierrerayosparonda/Documents/cronTest/pythonCron.py # CronTest
* * * * * cd /Users/alainpierrerayosparonda/Documents/cronTest &&  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /Users/alainpierrerayosparonda/Documents/cronTest/pythonCron.py # CronTest
* * * * * cd /Users/alainpierrerayosparonda/Documents/cronTest &&  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /Users/alainpierrerayosparonda/Documents/cronTest/pythonCron.py # CronTest

我的预期结果是将当前访问的时间添加到appended.txt中。

如果有人可以帮助我,那真的很棒。另外,这也是我第一次在Stack Overflow上发帖。

0 个答案:

没有答案