ubuntu20.04添加开机启动

Ubuntu 16.04 以后的版本不再支持 update-rc.d 方式添加开机自启脚本,只能使用 systemctl 命令进行添加。之前开机启动的方式可以查看我之前的笔记:https://sulao.cn/post/565.html

接下来我们直接看看ubuntu20.04添加开机启动的方法

首先我们需要修改/usr/lib/systemd/system/rc-local.service这个服务文件

#  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

主要是增加了最后一行的install

然后我们接着要创建/etc/rc.local文件,我调用了一个root下的脚本,内容如下

cat /etc/rc.local
#!/bin/bash

/root/test.sh

我们看看脚本内容啥样

cat /root/test.sh
#!/bin/bash

echo "`date` test !" > /root/test.log

同时我们需要将test.sh脚本和rc.lcoal文件均要赋予执行权限

chmod +x /etc/rc.local
chmod +x /root/test.sh

最后启动刚才修改配置的rc-local.service服务

systemctl enable rc-local.service
systemctl status rc-local.service

image.png

这样就算是配置好了,当机器重启时,这个服务在启动时会执行一次脚本退出,以下是我重启以后验证的。

image.png

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/885.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。