Ubuntu startup script or command

Background In the development process, sometimes some sh scripts, python scripts, commands, etc. need to be executed automatically when the machine i...
Method 1: rc.local script

Background

In the development process, sometimes some sh scripts, python scripts, commands, etc. need to be executed automatically when the machine is started; by checking many articles and tutorials on the Internet, two relatively convenient methods of starting the machine automatically are obtained.

Method 1: rc.local script

This method is more suitable for ubuntu16 and earlier versions; after all, ubunutu18 does not have the rc.local script (but it can be implemented in this article)

brief introduction

rc.local script is a script that will execute automatically after Ubuntu starts. Add a command line in the script, and it will execute automatically when Ubuntu starts.

  • Script path / etc/rc.local

  • root permission is required to modify.

Implementation (Ubuntu 16 and earlier)

1) Open rc.local script

sudo vi /etc/rc.local

Friends who are not familiar with vi editing tools can use vim, gedit and other tools to replace vi

2) Add command in rc.local script

Add the command to execute before exit 0. You can write the command directly or execute the Shell script file sh

For example: let ubuntu system execute the temperature detection script: net temperature.sh

Here you can specify the path directory of sh script;

watch -n 5 means to execute net temperature.sh repeatedly every 5s

The setting of rc.local script enables the sudo command to be executed automatically after power on;

For example:

#!/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.: sudo sh xx.sh exit 0

The problem of rc.local command not executing and program not starting

1. As shown below, add log to check the program execution

2. Change the header / bin/sh of rc.local file to / bin/bash

3. If you want to execute the SH file, give sudo chmod +x xxx.sh the execution permission, and then add sudo sh xxx.sh at startup

Implementation (Ubuntu 18)

Ubuntu 18 no longer uses the initd management system, it uses systemd

See the following links

https://askubuntu.com/questions/886620/how-can-i-execute-command-on-startup-rc-local-alternative-on-ubuntu-16-10

ubuntu-18.04 can't set the startup script by editing rc.local like Ubuntu 16. After the following simple settings, rc.local can play a role again.

1. Create rc-local.service file

sudo vi /etc/systemd/system/rc-local.service

2. Copy the following into the rc-local.service file

[Unit] Description=/etc/rc.local Compatibility Documentation=man:systemd-rc-local-generator(8) ConditionFileIsExecutable=/etc/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 RemainAfterExit=yes GuessMainPID=no [Install] WantedBy=multi-user.target Alias=rc-local.service

Content explanation:

The startup document is mainly divided into three parts

[Unit] section: start sequence and dependency;

[Service] section: start behavior, how to start, start type;

[Install] section: define how to Install the configuration file, that is, how to start the machine;

3. Create the file rc.local

sudo vi /etc/rc.local

4. Copy the following into the 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. echo "If you can see this line, it indicates that the self starting script is added successfully." > /usr/local/test.log exit 0

Create soft link

By default, systemd reads the configuration files under / etc/systemd/system, so you need to create soft links under / etc/systemd/system directory

ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/

5. Add permissions to rc.local

sudo chmod +x /etc/rc.local

6. Enable service

sudo systemctl enable rc-local

This command is equivalent to

sudo ln -s '/usr/lib/systemd/system/rc-local' '/etc/systemd/system/multi-user.target.wants/rc-local'

By default, Systemd reads the configuration file from the directory / etc/systemd/system /. However, most of the files stored in it are symbolic links, pointing to the directory / usr/lib/systemd/system /, where the real configuration files are stored. The systemctl enable command is used to establish a symbolic link between the above two directories.

If boot is set in the configuration file, the systemctl enable command is equivalent to activating boot.

Correspondingly, the systemctl disable command is used to undo the symbolic link relationship between two directories, which is equivalent to undoing the startup. (this is the selection operation)

sudo systemctl disable rc-local

So the previous problem may be that the soft link creation failed in the previous step, or the light link creation was not activated and started.

7. Start service and check status

sudo systemctl start rc-local.service

sudo systemctl status rc-local.service

8. Restart and view the test.log file

cat /usr/local/test.log

I hope it helps you.

Guo Pu 174 original articles published, 362 praised, 270000 visitors+ Private letter follow

16 January 2020, 09:41 | Views: 4654

Add new comment

For adding a comment, please log in
or create account

0 comments