Python Forum
python-crontab - How to use @reboot
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python-crontab - How to use @reboot
#1
I am trying to use python-crontab to run a program at reboot. From the webpage https://pypi.org/project/python-crontab/ there is a special case @reboot which will do it but I have no idea of the format to use it. Can anyone help please?
I am running Python3 on a Raspberry Pi
Thanks
Mick
Reply
#2
The @reboot seems to be a reference to the crontab files' syntax, not python syntax. Looking in crontab.py, there is a method every_reboot() that could work for you. The docstring of crontab.py shows examples, some involving every_reboot():
from crontab import CronTab
import sys

# Create a new non-installed crontab
cron = CronTab(tab='')
job  = cron.new(command='/usr/bin/echo')

job.minute.during(5,50).every(5)
job.hour.every(4)

job.dow.on('SUN')
job.month.during('APR', 'JUN')
job.month.also.during('OCT', 'DEC')

job.every(2).days()
job.setall(1, 12, None, None, None)

job2 = cron.new(command='/foo/bar', comment='SomeID')
job2.every_reboot()

jobs = list(cron.find_command('bar'))
job3 = jobs[0]
job3.clear()
job3.minute.every(1)

sys.stdout.write(str(cron.render()))

job3.enable(False)

for job4 in cron.find_command('echo'):
    sys.stdout.write(job4)

for job5 in cron.find_comment('SomeID'):
    sys.stdout.write(job5)

for job6 in cron:
    sys.stdout.write(job6)

for job7 in cron:
    job7.every(3).hours()
    sys.stdout.write(job7)
    job7.every().dow()

cron.remove_all(command='/foo/bar')
cron.remove_all(comment='This command')
cron.remove_all(time='* * * * *')
cron.remove_all()

output = cron.render()

cron.write()

cron.write(filename='/tmp/output.txt')

#cron.write_to_user(user=True)

#cron.write_to_user(user='root')

# Croniter Extentions allow you to ask for the scheduled job times, make
# sure you have croniter installed, it's not a hard dependancy.

job3.schedule().get_next()
job3.schedule().get_prev()
Reply
#3
Many thanks for your help, that works perfectly Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Architecture question for website with python crontab script rockie12us 1 1,712 Aug-09-2021, 10:07 AM
Last Post: Larz60+
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,250 May-28-2020, 05:27 PM
Last Post: micseydel
  python crontab remove_all(comment="Minute*") vvarrior 1 2,738 Aug-06-2018, 12:39 AM
Last Post: Larz60+
  first paramiko ssh script getting getting stuck.. while run through crontab anna 2 4,256 Mar-27-2018, 12:27 PM
Last Post: anna
  subprocess Popen not working via crontab nitin23c 8 11,987 Mar-21-2017, 05:39 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020