背景
开发过程中,有时需要一些sh脚本、python脚本、命令等在开机的时候自动执行;通过查看网上的许多文章和教程,得出了两种比较便捷的开机自启方法。
目录
方法一:rc.local脚本
这种方法比较适合于ubuntu16及之前的版本;毕竟像ubunutu18本来是不带rc.local脚本的(但是本文也能实现)
简介
rc.local脚本是一个Ubuntu开机后会自动执行的脚本,在该脚本内添加命令行,开机时会自动执行。
-
脚本路径:/etc/rc.local
-
需要root权限才能修改。
实现(ubutu16 及之前的版本)
1)打开rc.local脚本
sudo vi /etc/rc.local
不熟悉vi编辑工具的朋友可以使用vim、gedit等工具代替vi
2)在rc.local脚本添加命令
在exit 0前添加要执行的命令,里面可以直接写命令或者执行Shell脚本文件sh
例如:让ubuntu系统实现每隔5s执行一次温度检测脚本:net-temperature.sh
这里可以指定sh脚本的路径目录;
watch -n 5 是指每隔5s重复执行net-temperature.sh (关于对watch不了解的朋友,推荐搜索认识一下)
rc.local脚本的设置开机自动执行的sudo命令也是可以执行的;比如:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.:
sudo sh run_name.sh
exit 0
rc.local命令不执行,程序不启动的问题
1、添加log,查看程序执行情况
2、rc.local文件头部/bin/sh修改为/bin/bash
3、如果是执行sh文件,那么要赋予执行权限sudo chmod +x xxx.sh,然后启动时加上sudo sh xxx.sh
实现(Ubuntu18)
Ubuntu 18 不再使用initd
管理系统,而是使用systemd
参阅下列链接
ubuntu-18.04不能像ubuntu16一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.local重新发挥作用。
1、建立rc-local.service文件
sudo
vi
/etc/systemd/system/rc-local
.service
2、将下列内容复制进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
内容解释:
启动文件主要分成三部分
[Unit] 段: 启动顺序与依赖关系;
[Service] 段: 启动行为,如何启动,启动类型;
[Install] 段: 定义如何安装这个配置文件,即怎样做到开机启动;
3、创建文件rc.local
sudo vi /etc/rc.local
4、将下列内容复制进rc.local文件
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
echo "如果能看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
exit 0
创建软链接
systemd 默认读取 /etc/systemd/system 下的配置文件, 所以还需要在 /etc/systemd/system 目录下创建软链接
ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
5、给rc.local加上权限
sudo
chmod
+x
/etc/rc
.
local
6、启用服务
sudo
systemctl
enable
rc-
local
这个命令 等同于
sudo ln -s '/usr/lib/systemd/system/rc-local' '/etc/systemd/system/multi-user.target.wants/rc-local'
Systemd 默认从目录/etc/systemd/system/读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录/usr/lib/systemd/system/,真正的配置文件存放在那个目录。 systemctl enable命令用于在上面两个目录之间,建立符号链接关系。
如果配置文件里面设置了开机启动,systemctl enable命令相当于激活开机启动。
与之对应的,systemctl disable命令用于在两个目录之间,撤销符号链接关系,相当于撤销开机启动。(这个是选择操作)
sudo systemctl disable rc-local
所以说之前问题可能出现在上一步软链接创建失败,或者说光创建链接未激活开机启动。
7、启动服务并检查状态
sudo
systemctl start rc-
local
.service
sudo
systemctl status rc-
local
.service
8、重启并查看test.log文件
cat
/usr/local/test
.log
方案二:update-rc.d
使用 update-rc.d,我们可以添加和删除服务,并将它们添加到Ubuntu / Debian启动脚本中,实现开启自启动效果。
实现过程:
1)新建个脚本文件 new_service.sh
#!/bin/bash
# command content
exit 0
2)设置权限
sudo chmod +x new_service.sh
3)把脚本放置到启动目录下
sudo mv new_service.sh /etc/init.d/
4、将脚本添加到开机启动脚本
执行如下指令,在这里90表明一个优先级,越高表示执行的越晚 (优先级范围:0~90)
cd /etc/init.d/
sudo update-rc.d new_service.sh defaults 90
然后重启电脑;
5)查看全部服务列表
sudo service --status-all
这时应该能看到新加开机启动脚本(new_service.sh)的名字在列表中;说明开机时会启动这个sh脚本的。
附加:看到列表中,启动前面会可能出现以下三种状态de
- [+] –具有此标志的服务当前正在运行。
- [–] –具有此标志的服务当前未运行。
- [?] –没有 状态开关的服务。
左图中只是列举一部分
6)服务的启动停止状态
xxx为第5步查询得来的启动服务(脚本)名字
sudo service xxx status
sudo service xxx start
sudo service xxx stop
sudo service xxx restart
其他
移除开机启动脚本
sudo update-rc.d -f new_service.sh remove
update-rc.d的详细参数
使用update-rc.d命令需要指定脚本名称和一些参数,它的格式看起来是这样的(需要在 root 权限下):
update-rc.d [-n] [-f] <basename> remove
update-rc.d [-n] <basename> defaults
update-rc.d [-n] <basename> disable|enable [S|2|3|4|5]
update-rc.d <basename> start|stop <NN> <runlevels>
-n: not really
-f: force
disable|enable:代表脚本还在/etc/init.d中,并设置当前状态是手动启动还是自动启动。
start|stop:代表脚本还在/etc/init.d中,开机,并设置当前状态是开始运行还是停止运行。(启用后可配置开始运行与否)
NN:是一个决定启动顺序的两位数字值。(例如90大于80,因此80对应的脚本先启动或先停止)
runlevels:则指定了运行级别。
希望对你有帮助。