Create crontab with python-crontab in Python?
Question: 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?


Answer: 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()
    

