Score:0

Unable to run a shell script as a systemd service

hk flag

I have a small script to transform midi input notes to media functions (volume up, next song, etc). It looks like this:

#!/bin/bash
aseqdump -p "CMD MM-1" | \
while IFS=" ," read src ev1 ev2 ch label1 data1 label2 data2 rest; do
    case "$ev1 $ev2 $data1" in
        "Note on 32" ) xdotool key XF86AudioRaiseVolume ;;
        "Note on 31" ) xdotool key XF86AudioLowerVolume ;;
        "Note on 51" ) xdotool key XF86AudioMute ;;
        "Note on 28" ) xdotool key XF86AudioPrev ;;
        "Note on 29" ) xdotool key XF86AudioNext ;;
        "Note on 50" ) xdotool key XF86AudioPlay ;;
    esac
done

I can launch it from the terminal and it remains unable to receive commands until I press ctrl+c. So far so good. My question is, how can I use this script as a service so that I don't have to worry about it being in a visible terminal and running on reboot?

I have a service file in /etc/systemd/system/ with the following content:

[Unit]
Description=My Shell Script for Sync

[Service]
ExecStart=/bin/bash /usr/bin/midi2keys.sh

[Install]
WantedBy=multi-user.target
bac0n avatar
cn flag
I believe you should start your service as a user service instead `/etc/systemd/user` then `systemctl --user enable ...`
Score:0
in flag

To survive the reboot you should save your script in

cd /opt/systemd/ then sudo chmod a+x YOURSCRIPTFILENAME

then create a service like

sudo touch /etc/systemd/system/YOURSERVICENAME.service

Then Edit the file by typing sudo nano /etc/systemd/system/YOURSERVICENAME.service and then typing in :

[Unit]
Description=My Shell Script for Sync

[Service]
ExecStart=/bin/bash /usr/bin/midi2keys.sh

[Install]
WantedBy=multi-user.target

then Ctrl + X to exit , Y to save and enter to write file.

Now to install the service and ensure it loads at boot time type:

systemctl daemon-reload
systemctl enable YOURSERVICENAME.service

reboot the machine.

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.