How to enable Linux programs to run automatically at startup

Source: Internet
Author: User

Linux has its own complete startup system, which captures the context of linux Startup. the linux Startup Process will no longer be mysterious. You can manually add and delete the required application services as needed.

In this article, we assume that the init tree set in inittab is:

/Etc/rc. d/rc0.d

/Etc/rc. d/rc1.d

/Etc/rc. d/rc2.d

/Etc/rc. d/rc3.d

/Etc/rc. d/rc4.d

/Etc/rc. d/rc5.d

/Etc/rc. d/rc6.d

/Etc/rc. d/init. d

1. about linux Startup

Init is the father of all processes

Init reads/etc/inittab and executes the rc. sysinit script.

(Note that the file name is not certain. Some unix statements may even be written directly in the inittab)

The rc. sysinit script has done a lot of work:

Init $ PATH

Config network

Start swap function

Set hostname

Check root file system, repair if needed

Check root space

....

Rc. sysinit: Execute rc according to inittab ?. D script

Linux is a multi-user system, and getty is the watershed between multiple users and individual users.

The system script is run before getty.

2. About rc. d

All startup scripts are placed under/etc/rc. d/init. d.

Rc ?. D contains the link of the script in init. d. The naming format is:

S {number} {name}

K {number} {name}

The file starting with S passes the start parameter to the script.

Files starting with K pass the stop parameter to the script

Number determines the execution order

3. STARTUP script example

This is a/etc/rc. d/init. d/apache script used to start httpd:

     
      CODE:#!/bin/bashsource /etc/sysconfig/rcsource $rc_functionscase "$1" in    start)          echo "Starting Apache daemon..."          /usr/local/apache2/bin/apachectl -k start          evaluate_retval          ;;    stop)          echo "Stopping Apache daemon..."          /usr/local/apache2/bin/apachectl -k stop          evaluate_retval          ;;    restart)          echo "Restarting Apache daemon..."          /usr/local/apache2/bin/apachectl -k restart          evaluate_retval          ;;    status)          statusproc /usr/local/apache2/bin/httpd          ;;              *)          echo "Usage: $0 {start|stop|restart|status}"          exit 1          ;;esac[Copy to clipboard]
     
We can see that it accepts the start, stop, restart, and status parameters.

Then we can establish rc ?. D link:

     
      CODE:cd /etc/rc.d/init.d &&ln -sf ../init.d/apache ../rc0.d/K28apache &&ln -sf ../init.d/apache ../rc1.d/K28apache &&ln -sf ../init.d/apache ../rc2.d/K28apache &&ln -sf ../init.d/apache ../rc3.d/S32apache &&ln -sf ../init.d/apache ../rc4.d/S32apache &&ln -sf ../init.d/apache ../rc5.d/S32apache &&ln -sf ../init.d/apache ../rc6.d/K28apache[Copy to clipboard]
     
4. About rc. local

Rc. local, which is frequently used, is a habit rather than a standard.

Different releases have different implementation methods, which can be implemented as follows:

     
      CODE:touch /etc/rc.d/rc.localchmod +x /etc/rc.d/rc.localln -sf /etc/rc.d/rc.local /etc/rc.d/rc1.d/S999rc.local &&ln -sf /etc/rc.d/rc.local /etc/rc.d/rc2.d/S999rc.local &&ln -sf /etc/rc.d/rc.local /etc/rc.d/rc3.d/S999rc.local &&ln -sf /etc/rc.d/rc.local /etc/rc.d/rc4.d/S999rc.local &&ln -sf /etc/rc.d/rc.local /etc/rc.d/rc5.d/S999rc.local &&ln -sf /etc/rc.d/rc.local /etc/rc.d/rc6.d/S999rc.local[Copy to clipboard]
     
5. bash Startup Script

/Etc/profile

/Etc/bashrc

~ /. Bash_profile

~ /. Bashrc

Is the bash STARTUP script.

It is generally used to set the startup environment for a single user. It can also be used to launch a single user program, but it should be clear that they are in the bash category rather than the system category.

Their specific functions are described as follows:

The/bin/bash command interpreter (shell) uses a series of startup files to create a runtime environment:

/Etc/profile

/Etc/bashrc

~ /. Bash_profile

~ /. Bashrc

~ /. Bash_logout

Each file has special functions and has different effects on the login and interaction environments.

/Etc/profile and ~ /. Bash_profile is called when an interactive login shell is started.

/Etc/bashrc and ~ /. Bashrc is called when an interactive non-login shell is started.

~ /. Bash_logout is read when the user logs out.

An interactive login shell will run after/bin/login is successfully logged in. An interactive non-login shell is run through the command line, such as [prompt] $/bin/bash. Generally, a non-interactive shell appears when running a shell script. It is called a non-interactive shell because it does not wait for input on the command line but only executes the script program.

6. About Automatic startup of the boot program

System scripts can be placed in/etc/rc. d/init. d and/etc/rc. d/rc ?. D links can also be directly placed in/etc/rc. d/rc. local.

The init. d script contains complete parameters such as start, stop, status, and reload. It is a standard practice and is recommended.

Programs used by specific users. If some users need to use Chinese input methods, some do not need ~ .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.