1

I am trying to run two Python programs at reboot with my Raspberry Pi 3. The problem is that crontab doesn't run at reboot.

I tried to run the programs with crontab every 5 minutes and everything went correct.

This is working:

*/5 * * * * python /home/pi/Desktop/Telegram/pyTelegramBotAPI/ComandosRes.py
*/5 * * * * sudo python /home/pi/Desktop/Telegram/pyTelegramBotAPI/Correo+.py

This is not working:

@reboot sudo python /home/pi/Desktop/Telegram/pyTelegramBotAPI/Correo+.py
@reboot python /home/pi/Desktop/Telegram/pyTelegramBotAPI/ComandosRes.py

Any idea or suggestion is welcomed. Thanks in advance!

1
  • Since we don't know the script content, it's not clear if this matters, but you list the programs in a different order between the two cases. Does one depend on the other? (If so, you should make a better fix than just the order here, but the order might matter.)
    – Brick
    Sep 18, 2017 at 13:46

1 Answer 1

3

The Debian stretch manpage for cron notes the following:

Please note that startup, as far as @reboot is concerned, may be before some system daemons, or other facilities, were startup. This is due to the boot order sequence of the machine.

You could try adding a sleep duration to the start of your script, or possibly launching it later in the boot process using systemd. Here's an example of a service file I've used to launch after networking is up and running, and with a 10 second safety buffer:

  1. Create /etc/systemd/system/yourname.service containing:

    [Install]
    WantedBy=multi-user.target
    
    [Unit]
    Description=Example service
    Wants=network-online.target
    After=network-online.target
    
    [Service]
    User=youruser
    Group=yourgroup
    ExecStart=/path/to/your/script.py
    ExecStartPre=/bin/sleep 10
    Type=simple
    
    [Timer]
    OnStartupSec=25
    
  2. Enable the service with systemctl enable yourname.

  3. Start the service with systemctl start yourname.
  4. Verify proper startup with systemctl status yourname.

There is a lot more to systemd, but hopefully this will get you started. Note that this completely replaces the cron @reboot and/or /etc/rc.local approaches which don't provide as much control over when services are launched.

7
  • Thanks for answering. I will try it tomorrow and I will comment something. Thanks again.
    – Andermutu
    Sep 18, 2017 at 13:52
  • @Andermutu I just went through something similar. The problem was a script launching before the network was fully ready at reboot. I wound up fixing it by adding "ExecStartPre=/bin/sleep 10" to the .service file I installed via systemd. For some reason, "After=network-online.target" was not sufficient. You might add some network-readiness checks to your script rather than just adding delays. To do this "properly", you might want to read up on systemd rather than cron.
    – bobstro
    Sep 18, 2017 at 15:12
  • Thanks again for your help bobstro. This days I have very little time to try it. As soon as I have time I will try it and I will accept your answer and give you positive mark. Thanks!
    – Andermutu
    Sep 19, 2017 at 20:22
  • Hello Bobstro; I am trying to implement your solution but I dont understand exactly were I have to add "ExecStartPre=/bin/sleep 10". Do I have to add it in my Crontab file, Pyhton file or do I have to create a file in systemd?Thanks and sorry, I am a begginer.
    – Andermutu
    Sep 21, 2017 at 15:08
  • Better is to determine where in the startup process you want your program to launch and use Wants= and After= lines. If you find that you still have problems, you can insert a short delay with ExecStartPre=/bin/sleep 10 under the [Service] block. I'll edit my answer to include more detail.
    – bobstro
    Sep 21, 2017 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.