Markus
Markus

Reputation: 147

How to successful launch a python cronjob?

In my mind this is an easy task, but i don't know how to realize it.

I have got a python script, that is likely to crash on the first few tries. (It is calling a websocket, that is not yet ready.)

Can i tell cronjob to keep on trying to run this script until it finally works? Or do i have to implement this in the python script itself?

This is the line in Crontab

@reboot sh /home/pi/launcher.sh

And this is the .sh-file.

#!/bin/sh
# launcher.sh
cd /
sudo python /home/pi/example.py
cd /

Upvotes: 0

Views: 90

Answers (1)

Totem
Totem

Reputation: 7369

Whatever the task may be specifically, you would probably be best off implementing it in the python script. This seems more straight-forward. For instance:

while True:
    try:
        # your task
        # check if it was successful and if so
        break
    except Exception: # a relevant exception would be best
        pass # you could even use sleep() here(time module)
             #  to wait a few seconds between tries 

Upvotes: 3

Related Questions