New main PID does not belong to service, and PID file is not owned by root

When running vncserver (tightvncserver) on Xubuntu 18.04 as a service, I get this error

New main PID 2095 does not belong to service, and PID file is not owned by root. Refusing.

My service file looks like this

/etc/systemd/system/[email protected] 

[Unit]
Description=Start TightVNC server at startup
After=syslog.target network.target

[Service]
Type=forking
User=user
PAMName=login
PIDFile=/home/user/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

According to this bug report, it has something to do with this change in systemd.

Any suggestions on updating my service file so it works with systemd again?


Solution 1:

You're probably referencing the 16.04 version of this Digital Ocean VNC guide rather than the 18.04 version. In the 18.04 version the /etc/systemd/system/[email protected] file should look like this:

[Unit]
Description=Start TightVNC server at startup
After=syslog.target network.target

[Service]
Type=forking
User=sammy
Group=sammy
WorkingDirectory=/home/sammy

PIDFile=/home/sammy/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

Remember to replace all 4 instances of sammy with your own user name. The 18.04 version fixed my issues with the same PID file is not owned by root errors that you reported.

Solution 2:

Commenting out PAMName=login solved the issue on 16.04