Loading...

You have no items in your shopping cart.

 
 
 
×

Registration

Profile Informations

Login Datas

or login

First name is required!
Last name is required!
First name is not valid!
Last name is not valid!
This is not an email address!
Email address is required!
This email is already registered!
Password is required!
Enter a valid password!
Please enter 6 or more characters!
Please enter 16 or less characters!
Passwords are not same!
Terms and Conditions are required!
Email or Password is wrong!
Captcha is required!
Captcha is wrong!

Starting Your Software Automatically on Boot in Linux

Brief

Sometimes we need to start our software applications automatically on boot in linux, especially when we play with things like BeagleBone and Raspberry Pi in embedded world. Here are two ways to do it.

1. Using .profile

When a linux user logins in, the file ‘.profile’ in the user's home directory will be read and the codes in this file will execute automatically. So this can be a way to start our applications.

Firstly, we need to auto-login with root. In Debian OS without ‘systemd’, edit the file /etc/inittab with the following code:


# Example how to put a getty on a serial line (for a terminal)
#
#T0:23:respawn:/sbin/getty -L ttyS0 9600 vt100
#T1:23:respawn:/sbin/getty -L ttyS1 9600 vt100
# Example how to put a getty on a modem line.
#
#T3:23:respawn:/sbin/mgetty -x0 -s 57600 ttyS3
T0:23:respawn:/sbin/agetty -a root -L ttyO0 115200 vt102

In this case, we use program agetty to auto-login with root on a serial terminal ttyO0. The baud rate is 115200.

Then, we can put our compiled program ‘my_program’ into root's home directory. Add `./my_program` at the end of file '.profile'. In this way, the ‘my_program’ will auto-execute next time after rebooting the system. And also root logins in.

2. Using Subsystems

Subsystems examples are a web-server, data base server, OS network layer etc. Linux provides an elegant and modular way to organize the subsystems initialization. These subsystems are organized under the /etc/init.d and /etc/rc.d/rcN.d directories. They can be executed during the system booting. So another way to start our applications is to turn our software into a subsystem. 

Firstly, create a file ‘my_program’ with the following code. Put the file ‘my_program’ into /etc/init.d/ directory. Notice that the real compiled program's path is defined in this file.


#!/bin/sh
#/etc/init.d/my_program
program=path_to_my_program/my_program
test -x "$program" || exit 0
case "$1" in
    start)
        echo "starting my_program"
        start-stop-daemon --start --quiet --exec $program
        echo "."
        ;;
    stop)
        echo "Stopping my_program"
        start-stop-daemon --stop --quiet --pidfile /var/run/my_program.pid
        echo "."
        ;;
    *)
        echo "Usage: /etc/init.d/my_program {start|stop}"
        exit 1
esac
exit 0

Then, create a link into directory /etc/rc5.d(rcN.d), using the command below. Notice that the rcN.d depends on the runlevels id in /etc/inittab.

#ln -s /etc/init.d/my_program /etc/rc5.d/S99my_program

After that, we can start our program automatically in next time system boots.

Contributed by our software engineer Mars Wu

Referance: http://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/boot.html