使用python\u crontab计划的作业不工作
我使用以下python代码在ubuntu中安排作业使用python\u crontab计划的作业不工作,python,python-3.x,ubuntu,cron,crontab,Python,Python 3.x,Ubuntu,Cron,Crontab,我使用以下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('
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())
代码成功运行,其渲染函数打印输出如下:
*/2 * * * * /usr/bin/python3 /home/(user)/Desktop/Schedular/ScheduleInvoicingUtility.py >> /home/(user)/Desktop/Schedular/Logs.txt
但不知道该作业在ubuntu中保存在何处,并且该作业在指定时间后未运行/工作
知道我做错了什么吗?最后,我做了一些小改动来解决这个问题。下面是从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
您不需要管理员权限来更改crontable吗?我可以使用sudo crontab-e命令编辑crontab,我在python代码中使用了上面的同一个用户。因此,您使用
sudo
运行pyhton脚本?此python脚本由另一个cron作业触发。cron作业的代码是:0 7***/usr/bin/python3/home/(用户)/Desktop/Schedular/scheduleInvoicingulity.py>/home/(用户)/Desktop/Schedular/Logs.txtI意味着:使用sudo
运行对应于代码段的python脚本,而不是发票内容。