0

I am trying to add a line to my system user's crontab, from a Python script which uses the package python-crontab. My crontab file does not exist yet, and when I run this code, nothing happens (no errors, no results, no creation of crontab file):

from crontab import CronTab

cron = CronTab(user=True)
# cron = CronTab(user='my_user') I tried this line too without any results

job = cron.new(command='python3 /opt/my_script.py')

job.minute.on(2)
job.hour.on(12)

True == job.is_valid()

Am I missing anything?

2
  • I think getting user cron should be: user_cron = CronTab('user_name'), rest seems correct. Commented Jun 6, 2014 at 10:21
  • True == job.is_valid() does nothing. You should use assert job.is_valid() Commented Jun 6, 2014 at 10:37

1 Answer 1

3

You need to save the cronjob, that's all that's missing:

#!/bin/python 

from crontab import CronTab

cron = CronTab(user=True)

job = cron.new(command='python3 /opt/my_script.py')
job.minute.on(2)
job.hour.on(12)

cron.write()
Sign up to request clarification or add additional context in comments.

1 Comment

I have realized I had not read that part on the tutorial just before reading your post. Thank you of course.

Your Answer

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.