Homepage Wiki Forum Buy

Application autostart

From GNUBLIN

Contents

For beginners

If you want to start an application automatically when the operating system boots, you need to not much. It reaches a simple start / stop scripts for almost any purpose.

Custom scripts

Example: automatically flashing LED

We explain here a simple example of how to create such a script and how it is structured.

We want to automatically start the LED1 on the board Gnublin flashing. The flashing program , there are already examples/misc/gpio/blink.sh.

The first thing you put on a script, best with the nano text editor :

root@gnublin:~# nano /etc/init.d/LEDautoblink

In the script you write the following:

  1. #! /bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:          Laesst die LED1 blinken
  4. # Required-Start:    
  5. # Required-Stop:     
  6. # Default-Start:     2 3 4 5
  7. # Default-Stop:      0 1 6
  8. # Short-Description: LED auto blink
  9. # Description:       LED auto blink
  10. ### END INIT INFO
  11. # Author: Name <email@domain.tld>
  12.  
  13. # Aktionen
  14. case "$1" in
  15.     start)
  16.         echo "LED blink start"
  17.         /root/examples/misc/gpio/blink.sh > /dev/null &
  18.         echo $! > /var/run/blink.pid
  19.         ;;
  20.     stop)
  21.         echo "LED blink stop"
  22.         if [ -e /var/run/blink.pid ]
  23.         then
  24.             if ( kill -TERM $(cat /var/run/blink.pid) 2> /dev/null )
  25.             then
  26.                 c=1
  27.                 while [ "$c" -le 30 ]; do
  28.                   if ( kill -0 $(cat /var/run/blink.pid) 2> /dev/null )
  29.                   then
  30.                       sleep 1
  31.                   else
  32.                       break
  33.                   fi
  34.                   c=$((++c))
  35.                 done
  36.              fi
  37.              if ( kill -0 $(cat /var/run/blink.pid) 2> /dev/null )
  38.              then
  39.                  kill -KILL $(cat /var/run/blink.pid)
  40.              fi
  41.         fi
  42.         rm /var/run/blink.pid
  43.         ;;
  44. esac
  45.  
  46. exit 0

If this is done, we still make the file executable:

root@gnublin:~# chmod +x /etc/init.d/LEDautoblink

We then execute:

root@gnublin:~# update-rc.d LEDautoblink defaults

Now our program should start automatically after a reboot and flash the GNUBLIN LED1 on the board!

The script explained in more detail

The comment block line of 2-11 is important and should not be deleted. This is read in:

root@gnublin:~# update-rc.d LEDautoblink defaults


After start) are the commands that are executed at startup. The "& " in

 /root/examples/misc/gpio/blink.sh > /dev/null &

in line 17 ensures that the boot process does not hang up, and other commands can be executed. On line 18, then the process ID is saved in the directory /var/run/ in the file blink.pid. We need this file later if the service should be stopped.

Stop that happens in lines 20 to 42. First, in line 22 it is checked if the file containing the process ID exists. Then in line 24 is sent a kill command to the program. Then in a loop (lines 27-35 ) is 30 times to check whether the program has finished. If the program after 30 continuous still not finished, the program is in line with 39

 kill -KILL $(cat /var/run/blink.pid)

completed. In line 42, the process ID file is created or deleted.

Start/stop program at run time

To stop/restart the program at run time is easy:

root@gnublin:~# /etc/init.d/LEDautoblink stop

followed by :

root@gnublin:~# /etc/init.d/LEDautoblink start

Here the commands are simply executed, that we've inserted in the script earlier:

    start)
        echo "script start"
        /opt/beispiel start
        ;;
    stop)
        echo "script stop"
        /opt/beispiel stop
        ;;

Remove from autostart program

The program can be easily removed with a single command. In our case happens with

root@gnublin:~# update-rc.d -f LEDautoblink remove

For advanced

What's obvious for Linux cracks might not be for you: with the technique described here the application can print to the console (stdout), but it will not receive keyboard input (stdin). If you need this, a combination of Automatic_login_to_Gnublin and autostart via .bashrc might be your solution. And a caveat: for applications running without keyboard input you should provide a way to cleanly 'halt' your Linux system. A power disruption or a reset may damage your file system! You could poll the button at gpio15 for instance to initiate a shutdown.


Possible problems

After a

root@gnublin:~# reboot

could be pointed to a missing "libpcsclite1". In this case, it is necessary to download the package

  • Libpcsclite1 [1]

according to the instructions

  • [Http://wiki.gnublin.org/index.php/Debian_Pakete_installieren Debian install packages]

then also this script should automatically start and connect to the Internet.


Literature records

<-Article-End->

In other languages