本文是该文的整理版。redis
Ubuntu18.04再也不使用initd管理系统,改用systemd。为了像之前同样,在/etc/rc.local中设置开机启动程序,须要如下几步:bash
一、systemd默认读取/etc/systemd/system下的配置文件,该目录下的文件会连接/lib/systemd/system/下的文件。通常系统安装完/lib/systemd/system/下会有rc-local.service文件,即咱们须要的配置文件。spa
建立软连接:.net
ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
cd /etc/systemd/system/ cat rc-local.service
rc-local.service内容blog
# SPDX-License-Identifier: LGPL-2.1+
#
# 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.
[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
区块解释:ip
[Unit]:启动顺序与依赖关系。get
[Service]:启动行为,如何启动,启动类型。generator
[Install] :定义如何安装这个配置文件,即怎样作到开机启动。it
二、建立/etc/rc.local文件,赋可执行权限io
touch /etc/rc.local
chmod 755 /etc/rc.local
三、编辑/etc/rc.local,添加须要开机启动的任务
#!/bin/bash
echo "test rc " > /var/test.log
四、执行reboot重启系统,而后查看test.log
sudo reboot