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.  Download the restarter application
     
    Type:
     
     
     
     
    2. Create a cron job
     
    Cron has its own special editor. To start the editor type:
     

    Code:

     
    crontab –e
     
     
     
    Page down to the bottom and in the first line that does not start with a # type:
     

    Code:

     
    */1 * * * * sudo python /home/restarter.py >/dev/null 2>&1
     
     
     
     
     
     
     

    This command tells the job scheduler to run restarter.py from the /home 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:

     

    Code:

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

    Code:

     
    sudo service cron stop
     
     
    Other useful commands are:
     

    Code:

    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. 

    4. You can easily automate more programs

    If you have other Python programs you want to automate all you need to do is add them to restarter.py (refer the figure below). As we add new programs for different sensors we will add them to restarter.py, but you may want to use it for your own programs. In the diagram copy down the lines highlighted by the yellow block and replace the program name with your new program.



     


     
    Comments