What is the correct way to kill a vncsession in linux?

25

4

When I run the following command in my remote linux box that I connect to from my windows 7 laptop via VNC I get the following :

 [subhrcho@slc04lyo ~]$ ps -ef|grep vnc
subhrcho 20113 19804  0 21:40 pts/8    00:00:00 grep vnc
subhrcho 27486     1  0 Jan28 ?        00:05:35 Xvnc :1 -desktop slc04lyo:1 (subhrcho) -httpd /usr/share/vnc/classes -auth /home/subhrcho/.Xauthority -geometry 1680x1050 -depth 16 -rfbwait 30000 -rfbauth /home/subhrcho/.vnc/passwd -rfbport 5901 -pn
subhrcho 27493     1  0 Jan28 ?        00:00:00 vncconfig -iconic

How can I kill this session elegantly ? I know kill -9 <pid> would do it but I think that is a forceful clean and might lead to corrupted files .

P.S: I also read from this source to use the kill option from vncserver but not sure how to figure out display#.

Geek

Posted 2013-02-11T05:50:52.063

Reputation: 1 251

Answers

30

As you noticed, from the man vncserver:

  -kill :display#
          This kills a VNC desktop previously started with vncserver. It does
          this by killing the Xvnc process, whose process ID is stored in the
          file "$HOME/.vnc/host:display#.pid". It actually ignores anything
          preceding a ":" in its argument. This can be useful so you can write
          "vncserver -kill $DISPLAY", for example at the end of your xstartup
          file after a particular application exits.

The display number is connected to the port number of the display if one hasn't set that manually (and differently), where

Display number = (Port number) ‒ 5900

e.g. port 5901 → display :1. This information can be found in man Xvnc (vncserver is just a wrapper script that calls this tool) where it says:

   -rfbport port
          Specifies the TCP port on which Xvnc listens for connections from
          viewers (the protocol used in VNC is called RFB - "remote
          framebuffer").  The default is 5900 plus the display number.

If you don't know the number by heart (but you need to know it if you are going to connect to the server anyway), you can check e.g. ps ax | grep vnc for information. If I do that locally, I see the processes

25697 ?        S     55:38 Xvnc4 :1 [...]
[...]
30481 ?        S     17:57 Xvnc4 :2 [...]

and thus I know that they represent VNC servers with display numbers :1 and :2 respectively, and can be killed by

vncserver -kill :1
vncserver -kill :2

In your case, you see that the display number is :1 for the server listed in your ps output.

Daniel Andersson

Posted 2013-02-11T05:50:52.063

Reputation: 20 465

1

I tried the answer above and it didn't work for me. It gave me an error message as in this question: Killing VNC Process Manually

So I had to kill them manually. I tried kill -9, and then I couldn't log in with rdp anymore. I got xrdp_mm_process_login_response: login failed when I tried to log in.

The answer was found here: http://linuxtoolkit.blogspot.com/2013/03/xrdpmmprocessloginresponse-login-failed.html

Basically, there is a session file not cleaned up when the Xvnc server is killed. The file is named for the display, so if you're on display :12, it's /tmp/.X11-unix/X12. Delete that file after kill -9 and you're back in business.

Mnebuerquo

Posted 2013-02-11T05:50:52.063

Reputation: 541

Script I wrote to auto-kill idle sessions: https://gist.github.com/mnebuerquo/e825530cf2bfd363b6c3cd82fe697d94

– Mnebuerquo – 2016-06-24T11:47:09.520