使用 python_crontab 安排的作业不工作

Jobs scheduled using python_crontab not working

我正在使用以下 python 代码来安排 ubuntu 中的作业。

from crontab import CronTab
cron = CronTab(user='username')
job  = cron.new(command='/usr/bin/python3 /home/(user)/Desktop/Schedular/ScheduleInvoicingUtility.py >> /home/(user)/Desktop/Schedular/Logs.txt')
job.setall('*/2 * * * *')        
cron.write()
print(cron.render())

代码运行成功,render函数打印输出如下:

*/2 * * * * /usr/bin/python3 /home/(user)/Desktop/Schedular/ScheduleInvoicingUtility.py >> /home/(user)/Desktop/Schedular/Logs.txt

但不知道这个作业保存在 ubuntu 的什么地方,而且作业在指定时间后 running/working 也没有。

知道我做错了什么吗?

最后我通过一些小改动解决了这个问题。这是从 python:

正确创建 cron 作业的代码
cron = CronTab(user=True)
job  = cron.new(comment='My_Unique_Job', command='/usr/bin/python3 /home/(user)/Desktop/Schedular/ScheduleInvoicingUtility.py >> /home/(user)/Desktop/Schedular/Logs.txt')
job.setall('*/2 * * * *')
cron.write()

使用它来删除以前具有相同 ID 的作业。

cron.remove_all(comment='My_Unique_Job')

完整代码为:

cron = CronTab(user=True)
cron.remove_all(comment='My_Unique_Job')
job  = cron.new(comment='My_Unique_Job', command='/usr/bin/python3 /home/(user)/Desktop/Schedular/ScheduleInvoicingUtility.py >> /home/(user)/Desktop/Schedular/Logs.txt')
job.setall('*/2 * * * *')
cron.write()

别忘了导入 CronTab:

from crontab import CronTab

使用 pip 安装 python_crontab。

pip install python_crontab