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安装cuda失败提示"Uninstall manifest corrupt"
- ubuntu使用ssh命令批量设置集群节点具有sudo权限的账户sudo免密切换
- ubuntu22.04算力环境基础配置一键验证脚本
- ubuntu22.04算力环境基础配置一键脚本
- ubuntu22.04忘记root密码进入单用户模式修改密码
- linux中update-initramfs的用途和功能
- ubuntu22.04屏蔽使用apt安装软件时出现弹窗要求选择重启服务的方法
- ubuntu22.04删除系统中的新内核并回退的方法
- ubuntu22.04部署chrony时间同步服务
- ubuntu22.04部署ntp时间同步服务器
评论列表