Automate your alarm and sensors

Now that you have your alarm system or sensors up and running you probably want to make sure they are running all the time. You can configure Linux to run the alarm or sensor programs in the background. In this tutorial I will show you how to tell Linux to always run the a program or series of programs and start them up automatically if they are not running. 

 

This will allow you to do other tasks with your Raspberry Pi. After all, Linux is a multitasking operating system so that should be easy, and it is. 

 

So in order to accomplish this we will use three components which we will explain first and then install.

 

1. NOHUP

2. An application restarter

3. CRON JOBS


 

NOHUP is a command that will put your program execution into a background process. 


A restarter application will check if a program is running and if it isn't then it will execute a NOHUP command to run the program as a background process. 


A cron job is a scheduling application that is very useful for scheduling tasks. We will schedule the restarter application periodically (e.g. every minute). 


Let's get started!

 

Follow these steps:

 

1. Edit globals.py and configure which applications should auto-start

At the command prompt type:

  cd /home/pi/pep
  nano globals.py


Page down to the auto restart settings as per figure 1.


Figure 1 - Auto Start Settings

The four applications that can be auto-restarted are listed in these settings. If you do not want to auto-restart an application then set it's value to False, otherwise set to True, as per Figure 1. If you have installed the PrivateEyePi software in a directory other than /home/pi/pep then you must change the install_directory setting to your install directory. 

2. Create a cron job
 
Cron has its own special editor. To start the editor type:

crontab -e
 
Page down to the bottom and in the first line that does not start with a # type:
 
*/1 * * * * python /home/pi/pep/restarter.py >/dev/null 2>&1
 
 

This command tells the job scheduler to run restarter.py from the /home/pi/pep directory every minute. A minute is the smallest unit supported, but you can change the /1 to /10 and it will restart every 10 minutes.

To save the file press crtl-x followed by y and enter key.
 

3. Now you need to start the cron service, type:

 
  sudo service cron start  

If you want to stop the service, type:
 
  sudo service cron stop

Other useful commands are:
 
  crontab -l      Will list all active cron jobs
  ps ax              Will list all processes that are running
  kill pid            Will kill a process, where pid is the process id from te output of ps ax command
 
You can find the process id using the ps ax command and then taking note of the process id on the left hand side in the list. This is useful for stopping alarm.py so that you and make modifications, or download a new version. If you kill alarm.py remember the cron job will restart it after a minute, so stop the cron service (sudo service cron stop) before killing alarm.py.