1

I have used the crontab to run a command that repeats every 30 minutes. From the tutorials, I written the following command to /etc/crontab

*/30 * * * * root /usr/bin/python /home/pi/do.py>>/home/pi/output

As per the tutorials found on google, it should run for 0,30,60...minutes. But it won't run at reboot. However, it runs for every 30 minutes after reboot.

If I use the command @reboot /usr/bin/python /home/pi/do.py>>/home/pi/output, then it will run at reboot. But actually, I need to run the command at boot and also for every 30 minutes.

How can I configure the same for a run at reboot also?

mcv
  • 43
  • 3
  • 7
  • Use this article to achieve your result: https://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/ – Novice May 06 '19 at 10:32
  • Possible duplicate of [Cron job to run python script at reboot does not work](https://askubuntu.com/questions/970771/cron-job-to-run-python-script-at-reboot-does-not-work) – Novice May 06 '19 at 10:34
  • @Novice, I can run the command `@reboot /usr/bin/python /home/pi/do.py>>/home/pi/output` without any problem. Don't make it as same question. – mcv May 06 '19 at 10:53
  • Maybe [Cronjob, every 20 minutes. Starting direct after reboot](https://askubuntu.com/q/1051066/504066) is of help. It suggests replacing the `cronjob` with a `systemd timer` that is able to do both: run at boot time _and_ every N minutes. – PerlDuck May 06 '19 at 10:58
  • @PerlDuck, Which method will be better to use cron or systemd timer ? – mcv May 06 '19 at 11:07
  • @PerlDuck Add an answer explaining the systemd timer please! – dessert May 06 '19 at 11:12
  • @mcv That's a wide field ;-) In general _cronjobs_ are easier to handle and it's easier to get help for them because they exist since decades and many people know how they work. _systemd timers_ are relatively new, a bit more complicated to install, but far more flexible. For instance, you can have _systemd timers_ trigger a command always 30 minutes after it last _stopped_, etc. That is not possible with _cron_ alone. – PerlDuck May 06 '19 at 11:12

2 Answers2

4

You can’t combine both in one cron line, but there’s nothing wrong with simply having two lines, one for the start at boot and one for running the command every 30 minutes:

*/30 * * * * root /usr/bin/python /home/pi/do.py>>/home/pi/output
@reboot root /usr/bin/python /home/pi/do.py>>/home/pi/output
dessert
  • 39,392
  • 12
  • 115
  • 163
1

Use the following:

@reboot root /usr/bin/python /home/pi/do.py>>/home/pi/output
double-beep
  • 195
  • 1
  • 4
  • 12
pub
  • 11
  • 1