How to Set Programs Starting Automatically–Part 1 System Initialization
There’re lots of ways to start up a program or script when a Linux system boots up. Not all of them work for all types of applications. This article covers several methods you can configure your program to start up automatically at system boot up. It’s based on Ubuntu Linux, but I think they should apply to many other Linux distributions too.
Method 1: Add your Program in the System Initialization
When a Linux system is powered on, it will first run the program in BIOS. The BIOS locates the boot device and then the Linux kernel starts to boot up.
The kernel will mount the root file system, and run /sbin/init program, which normally has a process ID of 1. You can use ps -ax command to check.
1. /etc/init.d/rcS
The /sbin/init program will run script /etc/init.d/rcS, which in turn calls all S??* scripts in /etc/rcS.d/ directory in numerical/alphabetical order. Using ls -l command shows the content of my Ubuntu system as below,
-rw-r--r-- 1 root root 447 2009-09-08 02:58 README lrwxrwxrwx 1 root root 24 2010-11-24 18:16 S06keyboard-setup -> ../init.d/keyboard-setup lrwxrwxrwx 1 root root 21 2010-11-24 18:16 S13pcmciautils -> ../init.d/pcmciautils lrwxrwxrwx 1 root root 16 2010-11-24 18:16 S25brltty -> ../init.d/brltty lrwxrwxrwx 1 root root 18 2010-11-24 18:16 S37apparmor -> ../init.d/apparmor lrwxrwxrwx 1 root root 23 2010-11-24 18:16 S49console-setup -> ../init.d/console-setup lrwxrwxrwx 1 root root 17 2010-11-24 18:16 S55urandom -> ../init.d/urandom lrwxrwxrwx 1 root root 21 2010-12-02 10:47 S65firestarter -> ../init.d/firestarter lrwxrwxrwx 1 root root 24 2010-11-24 18:16 S70screen-cleanup -> ../init.d/screen-cleanup lrwxrwxrwx 1 root root 20 2010-11-24 18:16 S70x11-common -> ../init.d/x11-common lrwxrwxrwx 1 root root 19 2011-04-05 10:50 S75policykit -> ../init.d/policykit
2. /etc/rc2.d
After rcS, the system will switch to default run level. The default run level is defined in /etc/inittab. If not found, it will refer to /etc/init/rc-sysinit.conf file.
Run levels refers to a mode of operation, it’s used to configure a selected group of process to run. There’re 7 of them, from 0 to 6. A very brief summary is given below,
0 halt
1 single user
2-4 user defined
5 X11
6 Reboot
Once the default run level is found, the initialization will execute all the scripts under /etc/rc*.d, where * is the default run level.
In my Ubuntu system, there is no /etc/inittab file and the default level is set to 2 in /etc/init/rc-sysinit.conf file, so the initialization will execute the scripts under /etc/rc2.d/ directory. Use ls -l shows the following in my /etc/rc2.d/ directory,
lrwxrwxrwx 1 root root 13 2011-02-21 15:45 K50ntp -> ../init.d/ntp -rw-r--r-- 1 root root 677 2009-11-10 17:44 README lrwxrwxrwx 1 root root 15 2011-03-01 18:09 S00kdump -> ../init.d/kdump lrwxrwxrwx 1 root root 18 2011-03-11 12:36 S10sysklogd -> ../init.d/sysklogd lrwxrwxrwx 1 root root 15 2011-03-11 12:36 S11klogd -> ../init.d/klogd lrwxrwxrwx 1 root root 14 2011-04-28 10:23 S12dbus -> ../init.d/dbus …... lrwxrwxrwx 1 root root 18 2010-11-24 18:16 S99ondemand -> ../init.d/ondemand lrwxrwxrwx 1 root root 18 2010-11-24 18:16 S99rc.local -> ../init.d/rc.local
Note that there’s README file. It actually contains quite useful information. It’s worth reading it if you want to change something in this directory.
Also note that a the end of the list, there is rc.local, which is execute at the end of rc*.d directory.
3./etc/init.d/rc.local
The /etc/init.d/rc.local script will run all the programs/scripts in the /etc/rc.local script.
All the above actions are carried out in the boot up process and before a specific user is logged in. So if you want to start up your program using super user privilege, you can add your program here. The step-by-step instruction is as below,
1. put scripts/programs to /etc/init.d/
sudo cp /home/my_script /etc/init.d/
2. sudo chown root scriptname.sh
3. sudo chmod 4755 scriptname.sh
4. sudo update-rc.d scriptname.sh defaults
Leave a Reply Cancel reply
40% Discount on My Book — Android NDK Cookbook
Android NDK Cookbook ebook 40% discount with promotion code MREANC40 at Packt Publishing The promotion code is valid until 15th June.Categories
- Android Apps (18)
- Android Audio Editor (1)
- TS 2 (3)
- Video Converter Android (8)
- Video2Gif (1)
- Android Tutorial (26)
- Android Dev Tools (1)
- API illustrated (8)
- Multimedia API (3)
- ffmpeg on Android (4)
- NDK (6)
- UI (5)
- Animation (1)
- Code Snippet (2)
- Coding Beyond Technique (18)
- a word, a world (4)
- Bug Rectified (4)
- Programming Habit (1)
- Software as a Career (1)
- Software as User Experience (1)
- Compilers and Related (2)
- ELF (2)
- Computer Languages (31)
- C/C++ (13)
- Java (9)
- JavaScript (2)
- PHP (1)
- Python (8)
- Data Structure & Algorithms (29)
- Bits (1)
- Data Structure (5)
- Integers (10)
- BigInteger (1)
- Prime (4)
- Search (3)
- Sorting (5)
- Strings (5)
- Database (1)
- SQLite (1)
- Digital Signal Processing (33)
- Distributed Systems (17)
- Apache Cassandra (6)
- Apache Hadoop (8)
- Apache Avro (3)
- Apache Nutch (3)
- Apache Solr (1)
- Linux Study Notes (40)
- crontab (1)
- Linux Kernel Programming (8)
- Linux Programming (12)
- IPC (2)
- Linux Network Programming (5)
- Linux Signals (2)
- Linux Shell Scripting (1)
- ssh (3)
- Machinery (30)
- misc (1)
- My Ideas (1)
- My Project (3)
- Mobile Caching (1)
- Selective Decoding (2)
- My Publication (1)
- My Readings (1)
- Networking (15)
- Program for Performance (8)
- Uncategorized (1)
- Virtual Machine (2)
- Web Dev (8)
- web components (3)
- Android Apps (18)
Recent Comments
Archives
- May 2013 (1)
- April 2013 (1)
- March 2013 (4)
- December 2012 (2)
- November 2012 (6)
- October 2012 (6)
- September 2012 (3)
- August 2012 (13)
- July 2012 (15)
- June 2012 (3)
- May 2012 (8)
- April 2012 (4)
- March 2012 (13)
- February 2012 (19)
- January 2012 (9)
- December 2011 (11)
- November 2011 (12)
- October 2011 (4)
- September 2011 (12)
- August 2011 (16)
- July 2011 (15)
- June 2011 (6)
- May 2011 (10)
- April 2011 (13)
- March 2011 (20)
- February 2011 (4)
- November 2010 (2)
- May 2010 (1)
- April 2010 (1)
- February 2010 (1)




