Linux setting to start automatically at boot

1. Add a self-starting command to the /etc/rc.local file

Note: If there is content to add directly, if it does not exist, check whether the following method is used.

example

cat /usr/lib/systemd/system/rc-local.service
#也可能在下面这个路径下
cat /etc/systemd/system/rc-local.service

insert image description here

2. Add a self-starting script in the /etc/init.d directory + update-rc.d to add automatic execution actions

1. Copy or soft link the script to the /etc/init.d/ directory

2. The content of the script is as follows. The content in bold is a template comment and cannot be changed.

cat /etc/init.d/mayue.sh

The content of the script is as follows

#!/bin/sh                                          
                                                               
echo "mayue.sh 8888" >> /app/mayue.txt

case "$1" in
    start)
        echo "mayue.sh start" >> /app/mayue.txt
        ;;

    stop)
        echo "mayue.sh stop" >> /app/mayue.txt
        ;;
    *)
        echo "Usage: /etc/init.d/mayue.sh {start|stop}"
        exit 1
        ;;
esac

exit 0

3. Grant permission to the script file

sudo chmod 755 /etc/init.d/mayue.sh

4. Join the startup

 #在ubuntu环境认可的命令、成功后自动添加到对应的rc.d文件夹中
sudo update-rc.d /etc/init.d/mayue.sh defaults 90   #若报错用下面命令
#或
cd /etc/init.d;update-rc.d mayue.sh defaults 90 

#在centos环境认可的命令
sudo chkconfig --add mayue.sh  && chkconfig mayue.sh on 

5. Restart the verification and check the output:

root@imx6qdlsabresd:~# cat /app/mayue.txt 
mayue.sh 8888
mayue.sh stop
mayue.sh 8888
mayue.sh start

Since the reboot command is called to stop and then start, the printed content is as above.

3. Add a self-starting script in the /etc/init.d directory + add an automatic execution action (busybox) in /etc/inittab

To add a self-starting program in busybox init, you can follow the steps below:

  1. In the BusyBox system, the programs that need to be started automatically can be placed /etc/init.din the directory. For example, you can put the programs you need to start automatically /etc/init.d/myservicein the directory.

  2. Create a shell script file in /etc/init.dthe directory, and the file name is the name of the program you need to start automatically. For example, if your self-starter is named myservice, you can /etc/init.dcreate a myserviceshell script file named in the directory.

  3. In the shell script file you create, write the commands to start the self-starter. For example, if your autostarter is a Python script, you can start it with:

    #!/bin/sh
    python /path/to/myservice.py &
    

    In this example, /path/to/myservice.pyis the path to the Python script you need to start. Use &the symbol to run the script in the background.

  4. Make the shell script file you created executable. For example, if your self-starting program is called myservice, you can use the following command to set it as an executable file:

    chmod +x /etc/init.d/myservice
    
  5. Add a reference to the autostart program you created in BusyBox's startup script. For example, /etc/inittabadd the following lines to the file:

    ::sysinit:/etc/init.d/myservice
    

    This will automatically run your self-starter program at system startup.

  6. Save and restart the BusyBox system, your self-starter should run automatically. You can use ps auxthe command to check if the program is already running.

3.2 Introduction to /etc/inittab

  1. <id>: A unique identifier for a task or service, usually consisting of letters, numbers, and underscores. The value of this field must be unique.

  2. <runlevels>: Specifies the runlevels under which to start the task or service. Runlevels refer to how the system operates in different states. There are usually 0-6 run levels, where 0 means shutdown, 1 means single-user mode, 2-5 means multi-user mode, and 6 means restart. If you wish to run a task or service in multiple runlevels, you can specify the ranges using comma separators or dashes.

  3. <action>: Specifies the action to take when the task or service terminates. Commonly used operations are: respawn (restart task or service), wait (wait for task or service to terminate), once (run only once) and off (disable task or service).

  4. <process>: The process or command to run. This field is usually an absolute path specifying the executable or script to start.

Here's an example line:

my_script:2345:respawn:/path/to/my_script.sh

The value range of action is as follows :

  • respawn: If the process exits, restart it.
  • once: Start the process only once when init starts, and will not restart after the process exits.
  • wait: wait for the process to exit, and then perform the next operation.
  • boot: Executes the process at startup, but does not wait for it to exit.
  • bootwait: Executes a process at boot time and waits for it to exit.
  • sysinit: similar to boot, but executed before starting other services.
  • shutdown: Execute the process when the system shuts down.

These actions can be combined as needed to meet different needs.

4. Create a Linux service and set it to start automatically

Take nginx as an example, start it by making a linux service, and set it to start at boot

1) Precondition: nginx has been installed, the default installation path is: /usr/local/nginx

2) Create the nginx.service file under the /etc/systemd/system/ path and write the following content

[Unit]

Description=nginx - high performance web server

After=nginx.service

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s stop

Execenable=/usr/local/nginx/sbin/nginx

[Install]

WantedBy=multi-user.target

3) Set the boot to start automatically

# 设置开机启动
systemctl enable nginx

# 取消开机自启动
#systemctl disable nginx

# 查看服务当前状态
systemctl status nginx

# 启动nginx服务
systemctl start nginx

# 停止nginx服务
systemctl stop nginx

# 重启nginx服务
systemctl restart nginx

4. References

Guess you like

Origin blog.csdn.net/mayue_web/article/details/130201782