Score:0

Ubuntu22.04 - fail to run script using systemd

cn flag

I need to run a script at every boot. For that, I've create a .sh file, which runs as needed using a terminal.

To make it run during boot, I followed this tutorial.

.sh file:

#! /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 file

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

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

running the deamon yeild an error:

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:~$ 

To be sure that the systemd service runs and defined OK - I modified 123.sh into an echo to write into a file (meaning - a very simple task), I don't get any error.

Appreciate any help

Guy

uz flag
Jos
You run a command (`gnome-terminal`) that needs a graphic environment, at boot. The graphic environment may not have been started at that point, so you want to make the service dependent on that target. Second, you will need to provide a DISPLAY parameter, which is usually `:0` on a single-user system.
guyd avatar
cn flag
Can you write the correct syntax for first part ?
uz flag
Jos
I believe `Requires=graphical.target` in the `[Unit]` section should work.
nobody avatar
gh flag
I think this has to be a user systemd.service. I was using the https://wiki.archlinux.org/title/Systemd/User
guyd avatar
cn flag
@nobody can you explain more ?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.