Ubuntu 20.04 sets the boot auto-start script

1. Use rc-local.service
rc-local.service is a self-starting service that comes with the system. However, in the systemd startup mode of Ubuntu20, this service is not enabled by default. , enabling it requires some simple configuration.

The script of rc-local.service in the path /lib/systemd/system/rc-local.service specifies the startup sequence and behavior of rc.local

1 Establish a startup service

sudo vim   /lib/systemd/system/rc-local.service

ExecStart=/etc/rc.local start

This line of code stipulates that the command executed by this service when starting up is: /etc/rc.local start. That is, run the /etc/rc.local script. However, it can be seen that the content of this script is missing the [Install] section, that is to say, it does not define how to start it at boot, so obviously the service is invalid at this time. Therefore, we need to add the [Install] section for him later.


2. Modify the rc-local.service file, add the [Install] section
sudo chmod 777 /lib/systemd/system/rc-local.service to grant modification permissions, and then add the above [Install] statement

  Enter the following

 [Install]
WantedBy=multi-user.target
Alias=rc-local.service

3. Then set the rc-local.service service to start at boot:
systemd reads the configuration file under /etc/systemd/system by default, so you need to create a soft link in the /etc/systemd/system directory

sudo systemctl enable rc-local.service

4. (Focus on starting the program) Create /etc/rc.local. 
Ubuntu 20.04 does not have /etc/rc.local by default. You need to create touch /etc/rc.local yourself

Note: The & must be added at the end of the call to the sh script to allow the script to run in the background after startup, otherwise it may be stuck on the startup interface (this is my own running program script)

#!/bin/bash
cd /home/sne/wmrtc
nohup ./wmrtc &

exit 0

Then execute the following command to grant execution permission to /etc/rc.local. This step must be present, otherwise it will have no effect.

sudo chmod 777 /etc/rc.local

# or

sudo chmod +x /etc/rc.local 

Check the status of the rc-local service, showing loaded and enabled

sudo systemctl status rc-local.service

Finally try rebooting 

Guess you like

Origin blog.csdn.net/weixin_44692055/article/details/131839118