It's not possible to pass multiple parameters to the a systemd template (see related mailing list discussion). However, since we have only two parameters and one is the username, it makes sense to make it a user service instead of a system service.
User services can be run at boot if you enable lingering for those users:
sudo loginctl enable-linger username
From man loginctl
:
enable-linger [USER...], disable-linger [USER...]
Enable/disable user lingering for one or more users. If enabled for
a specific user, a user manager is spawned for the user at boot and
kept around after logouts. This allows users who are not logged in
to run long-running services. Takes one or more user names or
numeric UIDs as argument. If no argument is specified,
enables/disables lingering for the user of the session of the
caller.
You can create a user service at /etc/systemd/user
, which would look like (adapting your earlier service):
[Unit]
Description=Start TigerVNC Server at startup
After=syslog.target network.target
[Service]
Type=forking
PAMName=login
PIDFile=%h/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver :%i -depth 24 -geometry 1920x1080 -nolisten tcp -localhost
ExecStop=/usr/bin/vncserver -kill :%i
[Install]
WantedBy=default.target
Use the %h
specifier for the user home directory. I have omitted the User=
field, which doesn't make sense in user sessions. Also note the change to WantedBy
in [Install]
from multi-user.target
(which does not exist in user sessions) to default.target
.
The users can then control this service using systemctl --user
commands. (Note that you might need to restart after enabling linger if that user hasn't logged in yet, so that a user session is started for them.)
User=%u
? – muru Mar 09 '18 at 07:32systemctl enable vncserver@5.service
, thensystemctl start vncserver@5.service
. Is there something I am missing? – Kartik Mar 09 '18 at 07:35systemctl --user start ...
? – muru Mar 09 '18 at 07:36systemctl --user
, how would I make it available at startup for multiple users? Also, it gives me an error: "Failed to connect to bus: No such file or directory" – Kartik Mar 09 '18 at 07:41linger
for those user (see this comment):sudo loginctl enable-linger username
, user services can be started at boot. At this time, you can pass only one parameter for systemd templates, so this seems to be the only practical way to have both username and X11 display variable. Also see: this mailing list discussion – muru Mar 09 '18 at 07:54