ubuntu 18.04 how to set automatic startup script

Introduction: This paper mainly introduces how to realize the automatic running script of ubuntu 18.04 system startup through systemd.

For image download, domain name resolution and time synchronization, please click Alibaba open source mirror station

1, Overview

Ubuntu 18.04 does not have / etc / rc.exe by default Local file. It is impossible to ensure the automatic execution of startup by adding scripts to this file. So how to ensure that the script you write can be started and run automatically? Starting from Ubuntu 16.10, the initd management system is no longer used, but the systemd management system is used instead. Through the systemd management system, you can start up and run your own script. This article explains how to realize the automatic running script after starting up through systemd.

2, Configuration method

be careful:

  • If you have risk operations such as modifying or changing the instance or data, you must pay attention to the disaster tolerance and fault tolerance of the instance to ensure data security.
  • If you make configuration and data modifications to instances (including but not limited to ECS and RDS), it is recommended to create snapshots in advance or enable RDS log backup and other functions.
  • If you have authorized or submitted security information such as login account and password on Alibaba cloud platform, you are recommended to modify it in time.

The systemd management system reads the configuration file of / etc/systemd/system by default, and the files in this directory will link the files of / lib/systemd/system /. Execute the ls /lib/systemd/system command, and you can see that there are many startup scripts, among which you need to open RC local. Service file and view the contents.

#  This file is part of systemd.
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
Description=/etc/rc.local Compatibility
ExecStart=/etc/rc.local start

The normal startup file is mainly divided into the following three parts:

  • [Unit] section: startup order and dependency.
  • [Service] section: startup behavior, how to start, and startup type.
  • [Install] section: define how to Install this configuration file, that is, how to start up.

You can see / etc / RC The startup order of the local file is behind the network, but obviously it lacks the [Install] section, and there is no definition of how to start up, so this configuration is invalid. Therefore, we need to add the [Install] paragraph for him later:


Because Ubuntu 18 04 no / etc / rc Local, so you need to create it. Then you need to write the startup script to / etc / rc Local file. Finally, after the test is started, the following text files are generated in the root Home directory.

# 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 Test file > ~root/text.log
exit 0

Note: the script or command to be executed after startup should be before exit0.

1. Create RC. In the / etc/systemd/system directory local. Soft link of service.

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

2. Or execute the following command to set the startup self startup of the service.

systemctl enable rc-local.service

This article is transferred from: ubuntu 18.04 how to set automatic startup script - Alibaba cloud developer community

Keywords: Linux Operation & Maintenance Ubuntu

Added by corylulu on Wed, 16 Feb 2022 16:18:53 +0200