logo
Published on

Minimizing a GUI application after auto starting with systemd

Authors
  • avatar
    Name
    Mae
    Twitter

I ran into a problem that on my Arch Linux install with Teamviewer (for which I use for non lan remote access). I am able to use systemctl to start the teamviewerd on startup but this doesn't make the desktop environment accessible automatically on startup. In order to start the teamviewer user interface. Normally this would be simple, just run the command from a systemd user script that runs the teamviewer program on startup. The problem with this is teamviewer starts with the GUI open and there is no way to change this. This is a personal pet-peeve of mine. To resolve that I chose to use xdotool which is available on Arch Linux to minimize the two windows that teamviewer starts with the main process.

Using the dropbox systemd unit as a template I created this unit to do the following:

  1. Start teamviewer
  2. Wait a few seconds till the GUI is available
  3. Close "Computer & Contacts" window
  4. Close "TeamViewer" window

From file: /usr/lib/systemd/user/teamviewer.service

[Unit]
Description=Teamviewer

[Service]
Type=simple
ExecStart=/usr/bin/teamviewer
ExecStartPost=/usr/bin/sleep 1
ExecStartPost=/usr/bin/bash -c "/usr/bin/xdotool windowminimize $$(xdotool search --name "TeamViewer")"
ExecStartPost=/usr/bin/bash -c "/usr/bin/xdotool windowminimize $$(xdotool search --name "Computers & Contacts")"
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

[Install]
WantedBy=default.target

Note: Literal $'s must be escaped using $$ and quotes inside main quotes for bash -c must be escaped as well.

To install this script place it in /usr/lib/systemd/user/ and set the permissions to 644 as root:root. Then run

systemctl --user start teamviewer systemctl --user enable teamviewer People with systemd units will be familiar with ExecStart and some of the other lines however ExecStartPost may be new. ExecStartPre and ExecStartPost: Additional commands that are executed before or after the command in ExecStart=, respectively. systemd does not allow for shell commands so you must launch a shell if you wish to use expansion such as $(find . -name *.so) you must launch a shell (in my case /usr/bin/bash). You must also use full paths and cannot use executable files that are include in the path variable.

Feel free to use the above script to solve any problems with applications that do not start with some kind of -s or --silent flag.