Monday, May 28, 2018

Run Program's at Startup in Raspberry Pi


At a certain moment we may want to run certain programs or commands at startup or at first raspberry pi on turn on. This is needed when we want to not have to repeatedly run the same command or program, so raspberry pi will remember it to continue to run at startup.

The following example I will run 2 programs / commands that I usually type in the terminal, but the following will be executed when raspberry pi has power On. Example The command is running the command to direct the modem connect and run the gpsd program, following the command.

sudo gpsd / dev / ttyS0 -F /var/run/gpsd.sock
sudo wvdial apaaja &
for that there are several methods to run programs or commands at startup in raspberry pi but here I will only give you two method that I think the easiest to use, here's how:

1. edit the rc.local file

by adding commands to the rc.local file we can run certain commands at startup. First open the rc.local file with the following root permisions example using the nano editor program:
sudo nano /etc/rc.local
press enter and edit on the above exit 0 as follows:
#commands is running at startup
sudo gpsd /dev/ttyS0 -F /var/run/gpsd.sock
sudo wvdial apaaja &
exit 0
The above command example in the first command does not use the '&' sign at the end because the command does not contain looping or the process is not repetitive. Differences with wvdial commands are continuously running. So it needs to be given a suffix '&' is intended so that the command is given on different prosses and the boot process can continue to run.

Then to check it we can restart on raspberry pi. With command as follows:

sudo reboot

Additional: If we want to run a script that is in a particular directory then we need to mention the script file path to it completely. Suppose we want to run the program tracker.py in the directory / home / pi / Download then need to write as follows:

python3 /home/pi/Download/tracker.py

2. using crontab

crontab is usually used to run programs or commands that repeatedly time every time for example once every 5 minutes or 1 hour once, but the crontab can also be used to run the program at start up only. Here's how to use it:

1. open crontab with command

crontab -e

2. add the command by putting the word '@reboot' without quotes like the following example

@reboot sudo gpsd /dev/ttyS0 -F /var/run/gpsd.sock
@reboot sudo wvdial apaaja&

To find out we need to reboot raspberry pi.

if your need to debug your commands or program you can see log with running this following command :

grep cron / var / log / syslog

that two method which use to run commands/ programs at startup on Raspberry Pi. Thank You has read my article hopefully useful for you :)