1

I'm trying to get the PID of a java process started with xvfb-run. When started without xvfb-run, I use $! to get the PID of the last backgrounded process but as soon as I use xvfb-run I obviously get the PID of xvfb-run.

Here is the code:

#! /bin/bash
logfile=/var/log/SleepTest.log
pidfile=/var/run/SleepTest.pid
command="java -jar /data/test/SleepTest.jar"
( eval exec -c xvfb-run $command < /dev/null >> $logfile 2>&1 ) &
$! > $pidfile

If I remove the xvfb-run part in the second last line, everything works ok (except the part that I don't have a display and the program crash). I probably have to play with the "()" and "&" but I'm not an expert.

The program SleepTest.jar is a small program I wrote so I don't have to deal with the real thing. It only sleep for 2 minutes.

For those wondering why I use xvfb-run, it's because the java application I need to start use SWT and I don't have display on my server.

For those wondering why I need the pid of the process, it's because I want to create a init.d file to be able to start|stop|status my application

So is there a simple way to get it ?

  • You could use `ps -ef | grep SleepTest.jar | cut ...`. You will have to tinker with the cut part as it differs across os's. You may want to verify that the parent pid is the pid you got from $!... – Lucas Feb 26 '13 at 16:56
  • I'll probably fallback to something like like if I can not get it "cleany". Thanks. – user2108428 Feb 26 '13 at 17:39
  • OK, so with the command: `ps -ef | grep SleepTest.jar | grep $! | sed -e '1d' | awk '{print $2}' > $pidfile` I can get it. I'll test to make sure I always get the good one (and only one). Thanks again... – user2108428 Feb 27 '13 at 16:00

1 Answers1

-1

Run your entire script with xvfb-run (e.g. xvfb-run name_of_script.sh), and remove the xvfb-run from your eval line.

yoz
  • 902
  • 8
  • 20