0 前言

Linux系统早期初始化进程用的是inited,后来改用systemd,两者的软件开机自启设置是不同的。此外,桌面应用的自动方法和普通进程/服务的自启动方式也是不同的,本文一并说明。

1 inited

Ubuntu16.04之前的系统初始化进程采用的都是inited,可通过本节方法设置自启动。

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/profile

在这个文件的末尾添加相应的命令,实现的是“登陆启动”,即是说,需要用户登陆系统才启动。


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/rc.local”中添加需要的启动项目即可。

Tips :上述rc-local.service其实就是一个标准的systemd服务

#  SPDX-License-Identifie