Linux systemd 系统服务管理
Systemd 介绍
Systemd 是目前 Linux 系统上主要的系统守护进程管理工具,由于 init 一方面对于进程的管理是串行化的,容易出现阻塞情况,另一方面 init 也仅仅是执行启动脚本,并不能对服务本身进行更多的管理。所以许多 Linux 发行版都由 systemd 取代了 init 作为默认的系统进程管理工具。
设置开机自动执行
假设需要在系统开机时自动运行一个基于 Qt 图形界面的应用程序,那么可以在 /etc/systemd/system 目录添加一个 autorun.service 文件,内容如下:
[Unit]
Description=Test Qt Application
After=multi-user.target local-fs.target weston.service
[Service]
User=root
Restart=no
Type=simple
EnvironmentFile=/opt/root_env
ExecStart=/opt/autorun.sh
StandardOutput=console
[Install]
WantedBy=multi-user.target weston.service
/opt/root_env 包含一系列环境变量(包括 Qt 的一些配置),该文件可由 env 命令生成。
env > /opt/root_env
执行脚本 /opt/autorun.sh 的内容如下,其中 /opt/mainwindow 是一个 Qt 自带的 example 程序。
#!/bin/sh
# Run a simple Qt application
/opt/mainwindow
准备好上述几个文件后,即可执行下面命令将其加入 systemd 系统管理。
systemctl enable autorun.service
重启系统,mainwindow 程序就会自动运行。