AutoRun your Python Code on Raspberry Pi – using CrontabSharath
Unlike Arduino, the Raspberry Pi won’t run the code until it is executed manually. We need a scheduler to run the python code when the Pi is powered up or rebooted. Crontab (cron table) is scheduler used in Linux to schedule a specific task at a specific time. It is mostly used by Linux system Administrators to automate his daily tasks like scheduling backups and deleting old files, etc., Here we will be using it to launch the code during the startup, so we will be creating a shell script to navigate to the python code first and then launch it during startup using crontab.
First, create a ‘blink’ folder and then create an empty ‘blink.py’ file. It should be inside the path /home/pi/blink. (The /home/pi/ is the default user files path).
Python code: blink.py
import RPi.GPIO as GPIO import time LedPin = 11 # pin11 def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output GPIO.output(LedPin, GPIO.HIGH) # Turn ON led def blink(): while True: GPIO.output(LedPin, GPIO.HIGH) # led on time.sleep(1) GPIO.output(LedPin, GPIO.LOW) # led off time.sleep(1) def destroy(): GPIO.output(LedPin, GPIO.LOW) # led off GPIO.cleanup() # Release resource if __name__ == '__main__': # Program start from here setup() try: blink() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destroy()
Open the terminal and navigate to the ‘blink’ folder by using the following command
Create a launcher script by typing the command in the terminal.
Now the editor will get launched. Copy and paste the following code to the editor. This shell script will navigate to the blink code.
#!/bin/sh # launcher.sh # navigate to home directory, then to this directory, then execute python script, then back home cd / cd home/pi/blink sudo python blink.py cd /
Now press Ctrl – X and the press Y to Save. It will ask for filename leave it as ‘launcher.sh’ itself and press enter.
We want to make this launcher.sh script executable. Type the following command for it.
chmod 755 launcher.sh
You can test the launcher script by typing the following command if you want to test.
Create a log directory for capturing logs incase of any errors. Navigate back to home directory and create a log directory there.
We need to schedule the launcher script to run during startup itself. Open the crontab by using the command.
sudo crontab –e
Once the crontab window opens add this line at the end of the file.
@reboot sh /home/pi/bbt/launcher.sh >/home/pi/logs/cronlog 2>&1
This will run the launcher script during startup. Now reboot the Pi and check the python code runs automatically. Connect the LED according to the circuit and you will see the LED blinking (blink.py runs) automatically when the Pi is powered up.
You can modify the file path and file name to run a python program during startup. The crontab can also be used to run a specific program at a specific time or every 10 minutes like that.
Leave a Reply
You must be logged in to post a comment.