[Solved] Shell service is immediately inactive after systemctl startup

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
fisticuff
Level 1
Level 1
Posts: 3
Joined: Fri Jul 01, 2022 7:15 pm

[Solved] Shell service is immediately inactive after systemctl startup

Post by fisticuff »

Heyo, new mint user here. Having an issue with `systemctl` not keeping a simple shell script service alive persistently. The script itself runs as expected when ran manually via the terminal; the systemctl service activated one does not. Curious on if I can get a hand on what the solution could be.

OS:
Linux Mint 20.3 Cinnamon

Shell Script:
Simple script that watches dbus for system "lock" and "unlock" signals.

Code: Select all

#! /bin/bash

dbus-monitor --session "type='signal',interface='org.cinnamon.ScreenSaver'" |
    while read x; do
        case "$x" in
            *"boolean true"*) 
                "/home/<user>/scripts/systemLockUnlock/systemOnLock.sh";;
            *"boolean false"*)
                "/home/<user>/scripts/systemLockUnlock/systemOnUnlock.sh";;
        esac
    done
Service File (/etc/systemd/system/lockUnlock.service):

Code: Select all

[Unit]
Description=Toggle LED's based when the cinnamon screen saver sends out a locked/unlocked signal.

[Service]
Type=simple
ExecStart=/home/<user>/scripts/systemLockUnlock/lockUnlock.sh

[Install]
WantedBy=default.target
This command produces the following logs:

Code: Select all

sudo systemctl daemon-reload; sudo systemctl enable lockUnlock.service; sudo systemctl start lockUnlock.service; sudo systemctl status lockUnlock.service

Code: Select all

Created symlink /etc/systemd/system/default.target.wants/lockUnlock.service → /etc/systemd/system/lockUnlock.service.
● lockUnlock.service - Toggle LED's based on if the system is locked or not.
     Loaded: loaded (/etc/systemd/system/lockUnlock.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Sat 2022-07-02 12:22:12 CDT; 6ms ago
    Process: 9143 ExecStart=/home/<user>/scripts/systemLockUnlock/lockUnlock.sh (code=exited, status=0/SUCCESS)
   Main PID: 9143 (code=exited, status=0/SUCCESS)

Jul 02 12:22:12 <system> systemd[1]: Started Toggle LED's based on if the system is locked or not..
Jul 02 12:22:12 <system> lockUnlock.sh[9145]: Failed to open connection to session bus: Unable to autolaunch a dbus-daemon without a $DISPLA>
Jul 02 12:22:12 <system> systemd[1]: lockUnlock.service: Succeeded.
* Ignore the different description messages in the logs and the service file. Created and deleted few of these trying to figure out what the problem could be
* Note that user and system names have been replaced with <user> and <system>
Last edited by LockBot on Sun Jan 01, 2023 11:00 pm, edited 3 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
fisticuff
Level 1
Level 1
Posts: 3
Joined: Fri Jul 01, 2022 7:15 pm

Re: Shell service is immediately inactive after systemctl startup

Post by fisticuff »

Figured it out thanks to a somewhat related github post https://github.com/openrazer/openrazer/issues/639 based on the error message:
"Failed to open connection to session bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11".

Just had to change the daemon from being system based to user based. I guess the `dbus-monitor` is user based? Not sure still. The "lock" and "unlock" scripts needed use of a few user based daemons anyway.

Here's the steps I took:
1. Move the `.service` file to the user folder:

Code: Select all

sudo mv /etc/systemd/user/lockUnlock.service ~/.config/systemd/user/
2. Do the usual `systemctl` commands but with the `--user` flag and no `sudo` as its user based:

Code: Select all

systemctl --user daemon-reload; systemctl --user enable lockUnlock.service; systemctl --user start lockUnlock.service
Would still prefer this particular daemon to run on system boot rather than user login, but that would be for a different post.
Locked

Return to “Beginner Questions”