Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to open a program in a naked X-server on a different virtual terminal. I am using ubuntu 9.10.

The command I'm using is this.

openvt -f -s -- `X :2 & '/path/to/program' -display :2`;

Now, when the program shuts down I want the X-server to be killed as well.

So my question is this: How can I save the PID of the new X-server (X :2) to then later kill it?

It is going to be used in a normal shell script.

share|improve this question

migrated from stackoverflow.com Nov 26 '09 at 23:58

This question came from our site for professional and enthusiast programmers.

Instead of using openvt, you could use Xephyr to open a nested X sesssion. That would make it easier to manage it afterwards.

Use sudo apt-get install xserver-xephyr to install it.

Couple that with using $! to grab the process ID as James suggested.

share|improve this answer

In bash, $! is the process ID of the most recently executed back-ground (asynchronous) command.

Sombrero:Documents polleyj$ xterm &
[1] 41316
Sombrero:Documents polleyj$ KILLPID=$!
Sombrero:Documents polleyj$ echo $KILLPID
41316
Sombrero:Documents polleyj$ kill $KILLPID
Sombrero:Documents polleyj$
[1]+  Killed                  xterm
Sombrero:Documents polleyj$
share|improve this answer

What I did was to store pgrep -f 'X :2' in a variable mypid. Then I simply killed $mypid..

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .