0 前言
Linux系统早期初始化进程用的是inited,后来改用systemd,两者的软件开机自启设置是不同的。此外,桌面应用的自动方法和普通进程/服务的自启动方式也是不同的,本文一并说明。
1 sysVinit
Ubuntu16.04之前采用的虽然是Upstart,但兼容sysVinit的部分机制,本节总结sysVinit的自动执行命令机制。
1.1 /etc/rc.local
Ubuntu14.04的/etc/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. exit 0可见它其就是一个shell脚本,根据注释说明,只要在exit 0之前添加你希望开机启动的命令即可!
1.2 /etc/rc?.d
详见《Linux系统服务与开机启动》。
2 systemd
Ubuntu18.04开始用systemd取代inited(其它发行版也都基本切换到到systemd,例如Deepin、UOS),开机启动项的设置也随之发生变化:所有开机启动项都以服务的形式定义于“/etc/systemd/system”或“/etc/systemd/user”目录,开机后会自动启动相关服务。
2.1 服务模式
详见参考资料[5]的第3种方式。
2.2 兼容模式
systemd提供了兼容第1节设置的方法[4]:
sudo ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service sudo touch /etc/rc.local sudo chmod 755 /etc/rc.local然后在“/etc/