Ubuntu 16.04 以后的版本不再支持 update-rc.d 方式添加开机自启脚本,只能使用 systemctl 命令进行添加。之前开机启动的方式可以查看我之前的笔记:https://sulao.cn/post/562
接下来我们直接看看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
这样就算是配置好了,当机器重启时,这个服务在启动时会执行一次脚本退出,以下是我重启以后验证的。
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://sulao.cn/post/882
相关推荐
- ubuntu22.04部署openvpn和openvpn客户端配置
- ubuntu22.04编译安装postgresql17.5
- ubuntu使用deb包安装指定版本内核
- ubuntu修改grub引导切换到指定内核的方法
- ubuntu使用nvbandwidth测试单节点gpu带宽性能
- ubuntu24.04LTS添加apt源
- ubuntu下使用qperf工具测试RDMA网络带宽和延迟
- ubuntu22.04关闭自动更新和禁止unattended-upgrades服务开机启动
- ubuntu22.04使用nccl-tests进行单机多卡通信测试
- ubuntu22.04编译安装hwloc/libevent/ucx/openpmix/openmpi
评论列表