使用systemd启动应用程序

使用systemd启动应用程序

我想创建一个Linux服务来启动另一个应用程序。为了简单起见,我们就说它是 Firefox。

系统信息:

➜  ~ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:    18.04
Codename:   bionic

我创建了一个 bash 脚本来运行 Firefox:

#!/bin/bash
currentTime=$(date +"%I:%M:%S")
echo "My Test: Starting Firefox @ $currentTime"
firefox &

我已经为 systemd 创建了一个 .service 文件

[Unit]
Description=My Test Service
After=network.target

[Service]
Type=simple
User=myuser
SyslogIdentifier=mytest
ExecStart=/home/myuser/test.sh

[Install]
WantedBy=multi-user.target

我已将 .service 文件放入/etc/systemd/system并使用以下命令启用/启动它:

sudo systemctl daemon-reload
sudo systemctl enable mytest.service
sudo systemctl start mytest

这是来自的输出sudo systemctl status mytest

● mytest.service - My Test Service
Loaded: loaded (/etc/systemd/system/mytest.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Tue 2020-02-11 16:11:59 CST; 6s ago
Process: 4131 ExecStart=/home/myuser/linux-scripts/test.sh (code=exited, status=0/SUCCESS)
Main PID: 4131 (code=exited, status=0/SUCCESS)
Feb 11 16:11:59 NUC5i3RYH systemd[1]: Started My Test Service.
Feb 11 16:11:59 NUC5i3RYH mytest[4131]: My Test: Starting firefox @ 04:11:59

该脚本似乎在运行,但 Firefox 未启动。如果我直接运行 bash 脚本,Firefox 会立即打开。有人能看到我在这里做错了什么吗?我是 Linux 及其实用程序的新手,因此我们将不胜感激。

相关内容