In the following examples I’m going to start a program called servod which manages a servo. The program runs in the background and uses the Pi’s GPIO pins to set the position of a servo.
Note : This procedure is common for all kinds of scripts ( written in python, CGI, java, .Js etc) to run in Raspberry Pi / any Embedded Linux SBC( Single Board Computer ).
The simplest method is to put a command in /etc/rc.local. This file is executed while Linux is booting up and before any users have logged in.
When rc.local executes, the path environmanet variable (or any other environment variable) may not have been set up so it’s important to use the program or script’s full path. It’s also important to make sure the program doesn’t prompt for user input, as users won’t have logged in when rc.local runs. If the program waits for user input, it won’t complete, and the boot process will grind to a halt before users can login.
Similar problems can occur with programs that aren’t meant to complete like servod, which runs continuously as a service or daemon. The solution in this instance is to run servod in the background using the ‘&’ symbol. This tells the bash shell to execute the program without waiting for it to complete.
The cron daemon is a Linux thread that runs once a minute. It checks a file called the crontab file to see if there are any jobs that need to be executed. Usually entries in crontab specify a time to run tasks, but ‘@reboot’ can be used to tell cron to run a task as soon as your Pi boots. You can edit a crontab file by typing this command at a terminal:
Your crontab file will be opened in the nano editor. Use the arrow keys to move the cursor to the end of the file and add the command that you want to run:
To save changes to the file, press control-o. You will be prompted to save to a temporary file name. Press return, and the crontab program will install the temporary file in the correct place. It’s best practice to use a program or script’s full path in crontab files.
Sometimes it’s necessary to restart a service if it’s configuration files have changed. For example, if Apache’s configuration files are modified, it needs to be restarted using this command:
This command executes a script in /etc/init.d/ which can start, stop or restart the Apache web server. You can create your own init.d script for programs that you want to run at boot time. It should be put in /etc/init.d. It must start with a shebang (#!/bin/bash) followed by a LSB header, a set of specially formatted comments that contain information about the service.
This is an init script that I created to control servod:
An example of the header can be found in /etc/init.d/skeleton.
The code that follows the LSB is usually a bash case statment for processing the start/stop command line option. The option is stored in $1, and the case statement tests to see if the option is start or stop. If no option (or the wrong option) was supplied the default case executes, and a usage message is printed. If the start option is used, the script runs servod, and if the option is stop, the script kills the servod process.
Sometimes when a computer is shutdown, services need to be stopped in a controlled way. Programs may need to finish logging data or release lock files before they are terminated. If a script is started using cron or rc.local, it will simply stop when the Pi is shutdown. Using the init.d method to control scripts means that when your Pi shuts down, Linux will gracefully shutdown the script so that it has a chance to finish off any final tasks.
To make the script executable:
To start the servod program with this command:
…and stop it again with this one:
In order to make the program run on start up, it’s necessary to run this command:
This creates a link to /etc/init.d/servod in directories from /etc/rc0.d through to /etc/rc6.d. When Linux boots up or shuts down, it looks in these folders to see if any scripts or programs need to be run. When I restart my Pi the servod program starts automatically.