我需要在每次启动时运行一个脚本。为此,我创建了一个.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
服务运行并定义正常 - 我修改123.sh
为回显以写入文件(意味着 - 一个非常简单的任务),我没有收到任何错误。
感谢任何帮助
盖伊
答案1
分享我的成功(在@Edgar Magallon 评论的帮助下)
第 1 阶段 - 创建一个单元文件:
[Unit] Description=Runs terminalwith MQTT subs
PartOf=graphical-session.target
After=graphical-session.target
[Service]
Type=exec
WorkingDirectory=/home/guy
ExecStart=/bin/bash mqtts.sh
[Install]
WantedBy=graphical-session.target
第 2 阶段 - 用户服务:
文件放置在/home/guy/.config/systemd/user/mqtt_subs.service
,包括执行前 ( chmod +x
)
第 3 阶段 - 启动用户服务:
systemctl --user enable mqtt_subs.service
systemctl --user start mqtt_subs.service