Ubuntu22.04 - 无法使用 systemd 运行脚本

Ubuntu22.04 - 无法使用 systemd 运行脚本

我需要在每次启动时运行一个脚本。为此,我创建了一个.sh文件,该文件根据需要使用终端运行。

为了使其在启动期间运行,我遵循教程。

.sh文件:

#! /bin/bash

sub1="myHome/Messages"
sub2="myHome/log"
sub3="myHome/debug"
user="guy"
pass="kupelu9e"
mqtt_broker="192.168.2.100"

gnome-terminal --tab --title $sub1 -- mosquitto_sub -u $user -P $pass -h $mqtt_broker -t $sub1
gnome-terminal --tab --title $sub2 -- mosquitto_sub -u $user -P $pass -h $mqtt_broker -t $sub2
gnome-terminal --tab --title $sub3 -- mosquitto_sub -u $user -P $pass -h $mqtt_broker -t $sub3

gnome-terminal -- mosquitto_pub -u $user  -P $pass -h $mqtt_broker -t "myHome/All" -m "status"

单元文件

[Unit]
Description=Runs terminalwith MQTT subs
After=network.target auditd.service

[Service]
User=guy
WorkingDirectory=/home/guy
ExecStart=/bin/bash 123.sh

运行守护进程会产生错误:

guy@desktop:~$ systemctl status mqtt_subs.service 
× mqtt_subs.service - Runs terminalwith MQTT subs
     Loaded: loaded (/etc/systemd/system/mqtt_subs.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Tue 2023-04-11 15:12:53 IDT; 10s ago
    Process: 5193 ExecStart=/bin/bash mqtts.sh (code=exited, status=1/FAILURE)
   Main PID: 5193 (code=exited, status=1/FAILURE)
        CPU: 273ms

Apr 11 15:12:52 desktop systemd[1]: Started Runs terminalwith MQTT subs.
Apr 11 15:12:52 desktop bash[5195]: # Failed to parse arguments: Cannot open display:
Apr 11 15:12:53 desktop bash[5197]: # Failed to parse arguments: Cannot open display:
Apr 11 15:12:53 desktop bash[5199]: # Failed to parse arguments: Cannot open display:
Apr 11 15:12:53 desktop bash[5201]: # Failed to parse arguments: Cannot open display:
Apr 11 15:12:53 desktop systemd[1]: mqtt_subs.service: Main process exited, code=exited, status=1/FAILURE
Apr 11 15:12:53 desktop systemd[1]: mqtt_subs.service: Failed with result 'exit-code'.
guy@desktop:~$ 

为了确保systemd服务运行并定义 OK - 我修改123.sh为回显以写入文件(意思是 - 一个非常简单的任务),我没有收到任何错误。

感谢任何帮助

盖伊

相关内容