Cron is a time-based job scheduler that forms an integral part of Unix-like operating systems such as Linux and its many derivatives.
Open a terminal, and type crontab -e. If it's the first time that you've edited your crontab, then it will ask you to choose which editor you'd like to use. I prefer to use nano, and this is the default, so you can just press enter to select it.
At the top of the crontab, you'll see a lot of blurb about how to use it, on lines that are commented out (they begin with #). Scroll all the way down past these lines to the bottom (using the down arrow key) and type, on the last line, the following:
@reboot python3 /home/lick/Documents/Raspberry-Pi/one_led.py
Once you've added that line, press control-x, y and enter to exit nano.
It should say crontab: installing new crontab assuming you saved the crontab successfully.
There are a few items to make sure of before you restart your pi and see if this works.
You need to have an LED hooked up to pin 18 (long leg) and GND. Since that's what the one_led.py code is controlling.
You need to make sure to move or copy the one_led.py code to your documents folder since that's where cron will be looking for it.
Once that's all set, you can restart your Pi. Once it boots up, it should automatically run the LED code and the LED should flash.
This will now happen every time you start up the pi. But maybe you don't want this to happen forever!
To turn the task off:
Open a terminal, and type crontab -e.
Scroll down to the task you added.
Put a # at the beginning of the line.
Once you've added that line, press control-x, y and enter to exit nano.
It should say crontab: installing new crontab assuming you saved the crontab successfully.
The task should now be turned off
Troubleshooting:
Sometimes you need a delay when you start up....
@reboot sleep 40 && python3 /home/lick/Documents/Raspberry-Pi/one_led.py
Sometimes there are other weird errors that are hard to identify. If this happens….
Try adding >> out.txt 2>&1" at the end of your crontab task. It will create an error text file in your home directory that you can open and read about the problem.
Example:
@reboot python3 /home/lick/example.py >> out.txt 2>&1
Sometimes Pygame has trouble opening a console on startup. If this happens….
@reboot sleep 40 && export DISPLAY=:0 && python3 /home/lick/Documents/Raspberry-Pi/one_led.py
want to know more? Check this out
Here are some ongoing notes I've been taking as we encounter challenges.