Ubuntu – Start gnome-terminal with Command from systemd Service

systemd-serviceterminalubuntu-20.04

I wrote e service file called /etc/systemd/system/mytest.service . Then I would like to start a new terminal window with some commands that print on the new terminal console.

The file mytest.service has this content:

[Unit]
Description= Test
After= network.target

[Service]
ExecStart=/usr/bin/gnome-terminal -- "echo 'test' > /home/me/test.log; echo 'abcd';"

StandardOutput=journal+console

[Install]
WantedBy=default.target

After running sudo systemctl start mytest and print the status I got this error messages:

Sep 23 21:09:50 ub20 gnome-terminal[4205]: Unable to init server: Could not connect: Connection refused
Sep 23 21:09:50 ub20 gnome-terminal[4205]: # Failed to parse arguments: Cannot open display:
Sep 23 21:09:50 ub20 systemd[1]: ethminer.service: Main process exited, code=exited, status=1/FAILURE

What is wrong here?

Best Answer

  1. Create a systemd user folder in `$HOME/.config/systemd/user
  2. Create or copy the service file to the folder, e.g: mytest.service
  3. Run systemctl --user enable mytest.service