I am using following python code to schedule job in 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())
Code run successfully, and it's render function print output as follow:
*/2 * * * * /usr/bin/python3 /home/(user)/Desktop/Schedular/ScheduleInvoicingUtility.py >> /home/(user)/Desktop/Schedular/Logs.txt
But do not know where this job is saved in ubuntu, and also job is not running/working after specified time.
Any idea what I am doing wrong?
asked Jan 5, 2017 at 10:32
Waqas Ali
1,5392 gold badges20 silver badges26 bronze badges
1 Answer 1
Finally i fix the issue with minor changes. Here is code which is properly creating the cron job from python:
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()
Use this to remove previous jobs with same id.
cron.remove_all(comment='My_Unique_Job')
Complete code will be:
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()
Don't forgot to import CronTab:
from crontab import CronTab
Install python_crontab using pip.
pip install python_crontab
answered Jan 6, 2017 at 10:56
Waqas Ali
1,5392 gold badges20 silver badges26 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
sudo?sudo, no the Invoicing thingy.