5

I have a custom systemd service. Sometimes it fails because I need to regenerate my ssh key. The error message is as follows:

unison.service - Unison Sync
   Loaded: loaded (/etc/systemd/system/unison.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2019-10-07 09:14:25 IST; 884ms ago
  Process: 14607 ExecStart=/home/adamfg/bin/unison default (code=exited, status=3)
Main PID: 14607 (code=exited, status=3)

When this error occurs I want a terminal to open running the command required to regenerate mt ssh key, how can I do this?

1
  • To help us understand more, could you please add the output of cat /etc/systemd/system/unison.service and if possible the output of cat /home/adamfg/bin/unison after removing confidential information like usernames and passwords from the second file. Please edit your question to add that.
    – Raffa
    Oct 11, 2019 at 15:11

1 Answer 1

5
+50

Firstly, your service should be a user service instead. System services cannot open a terminal to do what you want. If it must be a system service, you'll need to create a method of sending yourself a notification when you log in to the system, so that you can perform the task and restart the service. As a user service though, it will run while you are logged in, and you can use the following method to pop a terminal up when you log in if the service fails to start.

Your service file should have PartOf=graphical-session.target in the [Unit] section of the service, and Restart=always in [Service] with your ExecStart= pointing to a shell script which runs

/home/adamfg/bin/unison default || gnome-terminal -e "bash -c 'genkey_command && exit 1'"

Or similar. This will cause your service to start when you log into the graphical session, and if it fails, open a terminal with genkey_command awaiting input, but will always exit 1, causing the service to restart after generating the new key.

You could also make the shell script more complex so it starts directly instead of relying on systemd service restarting, after generating the new key, and exits cleanly, instead.

5
  • @adam-griffiths Please try dobey`s answer first before responding to requests in my comment above, it should work.
    – Raffa
    Oct 11, 2019 at 17:48
  • Thanks for your answer, it seems like it should work. However, when I attempt to run it I get the following error: /home/adamfg/bin/unison: unknown option '-e'. It seems like it is interpreting the flag that's meant to be passed to gnome-termimal as a flag for unison. How can I fix this? (My default shell is zsh, could this be the issue?) Oct 14, 2019 at 8:16
  • You placed the double pipe characters between the unison command and gnome-terminal command, yes?
    – dobey
    Oct 14, 2019 at 14:53
  • Yes I did: ExecStart=/home/adamfg/bin/unison default || gnome-terminal -e "bash -c 'ssh-keygen && exit 1'" Oct 15, 2019 at 6:20
  • 1
    I was being dumb, I put the command straight into ExecStart instead of putting it in a shell script and then calling the shell script (as you said to do) Oct 15, 2019 at 6:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .