ubuntu 启动脚本变化
ubuntu-16.10 开始不再使用initd管理系统,改用systemd…
快速看了 systemd 的使用方法,发现改动有点大, 包括用 systemctl 命令来替换了 service 和 chkconfig 的功能。
systemd 默认读取 /etc/systemd/system 下的配置文件,该目录下的文件会链接/lib/systemd/system/下的文件。
执行 ls /lib/systemd/system 你可以看到有很多启动脚本,其中就有我们需要的 rc.local.service
打开脚本内容(如果没有就创建):
1 # This file is part of systemd. 2 # 3 # systemd is free software; you can redistribute it and/or modify it 4 # under the terms of the GNU Lesser General Public License as published by 5 # the Free Software Foundation; either version 2.1 of the License, or 6 # (at your option) any later version. 7 8 # This unit gets pulled automatically into multi-user.target by 9 # systemd-rc-local-generator if /etc/rc.local is executable. 10 [Unit] 11 Description=/etc/rc.local Compatibility 12 ConditionFileIsExecutable=/etc/rc.local 13 After=network.target 14 15 [Service] 16 Type=forking 17 ExecStart=/etc/rc.local start 18 TimeoutSec=0 19 RemainAfterExit=yes
一般正常的启动文件主要分成三部分
[Unit] 段: 启动顺序与依赖关系
[Service] 段: 启动行为,如何启动,启动类型
[Install] 段: 定义如何安装这个配置文件,即怎样做到开机启动
可以看出,/etc/rc.local 的启动顺序是在网络后面,但是显然它少了 Install 段,也就没有定义如何做到开机启动,所以显然这样配置是无效的。 因此我们就需要在后面帮他加上 [Install] 段:
[Install] WantedBy=multi-user.target Alias=rc-local.service
所以完整的 rc.local.service
文件是这样的:
[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
这里需要注意一下,ubuntu-18.04 server 版默认是没有 /etc/rc.local 这个文件的,需要自己创建
sudo touch /etc/rc.local
然后把你需要启动脚本写入 /etc/rc.local ,我们不妨写一些测试的脚本放在里面,以便验证脚本是否生效.
echo "this just a test" > /usr/local/text.log
记得给加上执行权限:
sudo chmod +x /etc/rc.local
做完这一步,还需要最后一步 前面我们说 systemd 默认读取 /etc/systemd/system 下的配置文件, 所以还需要在 /etc/systemd/system 目录下创建软链接
ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
OK, 接下来,重启系统,然后看看 /usr/local/text.log 文件是否存在就知道开机脚本是否生效了
https://blog.csdn.net/timewaitfornoone/article/details/121876412?utm_term=ubuntu20.04%E5%BC%80%E6%9C%BA%E8%87%AA%E5%90%AF%E5%8A%A8&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-2-121876412&spm=3001.4430
开机自启动
ubuntu作为服务器使用时,常常需要在机器重启时能自动启动我们开发的服务。有时候我们想要脚本开机自动运行,那么就需要设置开机自启动脚本。网上有很多种解决方案,基本上是分为三种:
修改/etc/r.local
我在ubuntu18和ubuntu20.10都亲测 /etc/rc.d/rc.local开机启动脚本不生效。主要有以下步骤:
查看是否有/etc/rc.d/rc.local
ls -l /etc/rc.d/rc.local
1
我这边20这个版本的话,都没有这个文件。那就不管这个方式了
/lib/systemd/system增加开机启动服务
通常在网上现有的方法,主要是通过修改/etc/init.d,最后修改权限生成。但 Ubuntu 18.04 不再使用initd管理系统,改用systemd,包括用systemctl命令来替换了service和chkconfig的功能。systemd 默认读取 /etc/systemd/system 下的配置文件,该目录下的文件会链接/lib/systemd/system/下的文件。不同于以往的版本,ubuntu18.04默认不带/etc/rc.local文件,我们需要通过配置来让service生效。
检查系统目录/lib/systemd/system/test.service
如果没有自己新建,文件内容为(如果文件存在本身是没有[Install]项的,需要自己添加进去)
vim /lib/systemd/system/test.service
# 编写内容
[Unit]
Description=test
Requires=network-online.target #若需要联网后启动的话,则需要加入该参数
After=network-online.target #若需要联网后启动的话,则需要加入该参数
[Service]
Type=forking
ExecStart=/bin/bash /home/test/test.sh #执行的内容是脚本test.sh中的内容,其中包括它的绝对地址
[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
此处可以自行增加一个service在该路径下,根据该路径下的格式,自行定制即可。详细参数信息可以见下Systemd 添加自定义服务(开机自启动).
修改配置文件后需要重加载配置
sudo systemctl daemon-reload
1
创建test.sh
vim /home/test/test.sh
# 写入如下内容
#!/bin/bash
echo `date`,"ok" >>/tmp/test.log
1
2
3
4
5
赋予可执行权限
chmod +x /home/test/test.sh
1
设置开机启动
systemctl enable test.service
1
查看启动的状态
systemctl status test.service
root@rex:/home/rex# systemctl status test.service
● test.service - test
Loaded: loaded (/lib/systemd/system/test.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sat 2021-12-11 08:46:19 UTC; 35s ago
Process: 967 ExecStart=/bin/bash /home/test/test.sh #执行的内容是脚本test.sh中的内容,其中包括它的绝对地址 (code=exited, status=0/SUCCESS)
Dec 11 08:46:19 rex systemd[1]: Starting test...
Dec 11 08:46:19 rex systemd[1]: Started test.
# 查看文件是否写入
root@rex:/tmp# cat test.log
Sat Dec 11 08:46:19 UTC 2021,ok
1
2
3
4
5
6
7
8
9
10
11
12
关闭开机启动
systemctl disable test.service
# 输出
root@rex:/tmp# systemctl disable test.service
Removed /etc/systemd/system/multi-user.target.wants/test.service.
1
2
3
4
update-rc.d增加开机启动服务
该方法主要是适用于/etc/init.d的服务的情况,现今低版本的linux主要是用它。现阶段不在赘述。下面给到链接。仅供参考。
https://m.linuxidc.com/Linux/2017-09/147178.htm.
https://www.cnblogs.com/yehui/p/9874866.html.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?