一、设置服务自启动
使用systemctl设置服务自启动
1、首先创建一个配置文件
这个配置文件以.service为后缀。
比如,要运行/home/downey目录下的test.sh脚本,我们可以添加一个配置文件test.service:
[Unit]
Description=
Documentation=
After=network.target
Wants=
Requires=
[Service]
ExecStart=/home/downey/test.sh
ExecStop=
ExecReload=/home/downey/test.sh
Type=simple
[Install]
WantedBy=multi-user.target
将文件放在/usr/lib/systemd/system或者/etc/systemd/system目录下
2、测试一下
sudo systemctl start test.service
然后你可以查看你的/home/downey/test.sh脚本是否已经运行,如果已经运行表示配置文件没有问题。
3、设置自启动
sudo systemctl enable test.service
系统会打印出如下信息:
Created symlink /etc/systemd/system/multi-user.target.wants/test.service→ /usr/lib/systemd/system/test.service.
可以看到,这里在/etc/systemd/system/multi-user.target.wants/目录下创建了一个/usr/lib/systemd/system/test.service文件的软链接,到这里设置 开机自启动 就完成了。
4、配置文件的简单解析
在上面的配置文件中,为了演示起见,我将一些本测试脚本不需要但是比较重要的配置项也写了出来,其实如果不需要可以删除,但是[Unit]/[Service]/[Install]这三个标签需要保留。
我们来一个个简单介绍一下配置项:
Description:运行软件描述
Documentation:软件的文档
After:因为软件的启动通常依赖于其他软件,这里是指定在哪个服务被启动之后再启动,设置优先级
Wants:弱依赖于某个服务,目标服务的运行状态可以影响到本软件但不会决定本软件运行状态
Requires:强依赖某个服务,目标服务的状态可以决定本软件运行。
ExecStart:执行命令
ExecStop:停止执行命令
ExecReload:重启时的命令
Type:软件运行方式,默认为simple
WantedBy:这里相当于设置软件,选择运行在linux的哪个运行级别,只是在systemd中不在有运行级别概念,但是这里权当这么理解。
二、设置图形界面程序自启动
将应用程序的.desktop文件放在自启动目录下,那么当用户登陆桌面环境时,就会自动启动这些应用程序。
opensuse自启动目录是/etc/xdg/autostart和~/.config/autostart