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编译安装hwloc/libevent/ucx/openpmix/openmpi
- ubuntu安装openvpn并配置连接
- ubuntu22.04使用gpu-burn进行gpu显卡测试
- ubuntu24.04使用deepin-wine安装企业微信
- ubuntu在线安装mysql8无法使用账户密码登陆的问题
- ubuntu22.04编译安装mysql8
- ubuntu22.04编译安装nginx
- ubuntu22.04撰写定时重启和开机启动执行指定脚本
- ubuntu22.04安装nvidia-container-runtime
- ubuntu22.04离线安装containerd和crictl管理工具
评论列表