Linux Deploy adds self-starting (pro-test available) linuxdeploy automatic configuration

Add an automatic task at startup, which can save the operation of manually inputting initialization commands after startup

1. Method 1

The run-parts method, that is, the rc.local method (sometimes this method does not work, just follow the configuration in 4)

1. Linux Deploy configuration

1. Click the setting icon in the lower right corner to enter the setting interface

2. Scroll to the "Initialization" section, and tick "Enable"

462a99c5cba9410b9d0a3058c557d75c.png

option "run-parts"

da11a91f5f844c75a9f5f761f1dd70bb.png

 Initialization path: /etc/rc.local

 

2. Write the configuration file

1. Create /etc/rc.local by yourself and add the following default content (add custom content before exit 0, the first line cannot be deleted)

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
exit 0

2. Execute the following command to ensure that rc.local starts automatically

sudo chown root:root /etc/rc.local
sudo chmod 755 /etc/rc.local

3. Examples

rc.local file

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#启动宝塔
bt start
#启动nginx
/etc/init.d/nginx start


exit 0

Realized function: set pagoda and nginx to start automatically

415fc604cb78407ea58d5ca89a452bc7.png

4. rc.local is not executed

In this way, sometimes rc.local will not be executed after restarting. Open the initialization settings and uncheck the asynchronous processing inside.

64f01cb71ffc4ece94042cf0ef818f21.png

 2. Method 2

Use sysv's initialization method (initialization is slower)

8e83b1076a404c40ac81111fefad1331.png

The same asynchronous processing check to remove

1. Use update-rc.dthe step of adding a boot-up self-start script

1. Enter the boot directory cd /etc/init.d

When we use  ls the command, we will find that there are some files in it. These files are programs that run automatically at startup

 

2. Create a startup script vi sunny.sh (this file has format requirements)

 

3. Give execution permission chmod +x /etc/init.d/ sunny.sh

 

4. Set boot self-start update-rc.d sunny.sh defaults

Add required items at the front of the script, otherwise an error will be reported

insserv: warning: script 'xx.sh' missing LSB tags insserv: warning: script 'xx.sh' missing LSB tags insserv: Default-Start undefined, assuming empty start runlevel(s) for script `xx.sh' insserv: Default-Stop undefined, assuming empty stop runlevel(s) for script `xx.sh'

2. Script specification

Parameter description: LSBInitScripts

#!/bin/sh
### BEGIN INIT INFO
# Provides:          xx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts xx
# Description:       starts the xx
### END INIT INFO

A template for a boot entry file:

#!/bin/sh

### BEGIN INIT INFO
# Provides: [程序名称,唯一]
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: [启动项的简短说明]
# Description: [启动项的完整说明]
### END INIT INFO

[需要执行的命令]

exit 0

In this template, Provides  is unique , that is, among all startup items, Provides cannot have any conflict of the same name. Short-Description  and  Description  can be written according to mood. The commands that need to be executed can be written according to the normal Bash Shell writing method.

Friendly reminder: For programs that need to run silently in the background, please use  nohup [command to be executed] >/dev/null 2>&1 &  to start!

Please do not delete the last  exit 0  ! This is a pass-by that returns a normal exit signal!

Then, we imagine that we need to create a startup item named  MyTest , and ask it to write a sentence in the /root/mytest.txt file  when it starts up   : "Hello Linux World!" According to the above template, we need Modify it like this:

#!/bin/sh

### BEGIN INIT INFO
# Provides: MyTest
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: MyTest
# Description: MyTest Test Program
### END INIT INFO

echo "Hello Linux World !" >> /root/mytest.txt

exit 0

Then save the file and close the editor. Next, we  MyTest add the executable attribute to the file we just edited ( if no attribute is added, the startup script will not be able to run ):

chmod +x MyTest

Then, add this startup script to the startup item:

update-rc.d MyTest defaults

Then restart the system, or execute from the command line  /etc/init.d/MyTest , execute the startup script, and then use  cat /root/mytest.txt, to verify that the file was created correctly.

Delete the boot entry we just created

update-rc.d -f MyTest remove

3. Examples

1. Write a script

Use the ngrok self-start script to verify here: start automatically at boot · Sunny-Ngrok documentation

e7446715dc6b47dd864c8211958f4fc7.png

 

/etc/init.d/sunny startup script code

#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          ngrok.cc
# Required-Start:    $network $remote_fs $local_fs
# Required-Stop:     $network $remote_fs $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: autostartup of ngrok for Linux
### END INIT INFO

NAME=sunny
DAEMON=/usr/local/bin/$NAME
PIDFILE=/var/run/$NAME.pid

[ -x "$DAEMON" ] || exit 0

case "$1" in
  start)
      if [ -f $PIDFILE ]; then
        echo "$NAME already running..."
        echo -e "\033[1;35mStart Fail\033[0m"
      else
        echo "Starting $NAME..."
        start-stop-daemon -S -p $PIDFILE -m -b -o -q -x $DAEMON -- clientid 隧道id || return 2
        echo -e "\033[1;32mStart Success\033[0m"
    fi
    ;;
  stop)
        echo "Stoping $NAME..."
        start-stop-daemon -K -p $PIDFILE -s TERM -o -q || return 2
        rm -rf $PIDFILE
        echo -e "\033[1;32mStop Success\033[0m"
    ;;
  restart)
    $0 stop && sleep 2 && $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
esac
exit 0

Replace the [tunnel id] in the code with your own tunnel id

2. Set the boot to start

Ubuntu, Raspberry Pi, Debian series systems

cd /etc/init.d
sudo update-rc.d sunny defaults 90    #加入开机启动
sudo update-rc.d -f sunny remove  #取消开机启动

Operating system below Centos 7

sudo chkconfig --add sunny     #添加系统服务
sudo chkconfig --del sunny    #删除系统服务
sudo chkconfig --list        #查看系统服务
sudo chkconfig sunny on     #设置开机启动
sudo chkconfig sunny off     #设置取消启动
service sunny start         #启动
service sunny stop             #关闭
service sunny restart         #重启

CentOS install start-stop-daemon

wget http://developer.axis.com/download/distribution/apps-sys-utils-start-stop-daemon-IR1_9_18-2.tar.gz
tar -xzvf apps-sys-utils-start-stop-daemon-IR1_9_18-2.tar.gz
# 然后进入解压之后的路径 一直 cd 到start-stop-daemon.c在的目录 
cc start-stop-daemon.c -o start-stop-daemon
cp start-stop-daemon /usr/bin/start-stop-daemon

3. Restart effect

Replace sunny with sunny.sh here

 55026c3da25249cb85f4f8f57ee2ab29.png

 

 

Guess you like

Origin blog.csdn.net/qq_43445867/article/details/131799629
Recommended