0

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?

Jean-François Fabre
141k24 gold badges179 silver badges246 bronze badges
asked Jan 5, 2017 at 10:32
9
  • don't you need admin rights to alter the crontable? Commented Jan 5, 2017 at 10:36
  • I can edit crontab using sudo crontab -e command, and i used same user above in my python code. Commented Jan 5, 2017 at 11:01
  • so you ran your pyhton script using sudo ? Commented Jan 5, 2017 at 11:46
  • This python script is being triggered by another cron job. code of cron job is: 0 7 * * * /usr/bin/python3 /home/(user)/Desktop/Schedular/ScheduleInvoicingUtility.py >> /home/(user)/Desktop/Schedular/Logs.txt Commented Jan 5, 2017 at 12:17
  • I mean: run the python script corresponding to the snippet using sudo, no the Invoicing thingy. Commented Jan 5, 2017 at 12:21

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.