6

Getting a background process ID is easy to do from the prompt by going:

$ my_daemon &
$ echo $!

But what if I want to run it as a different user like:

su - joe -c "/path/to/my_daemon &;"

Now how can I capture the PID of my_daemon?

koola
  • 1,302
  • 1
  • 10
  • 15
Dex
  • 11,863
  • 15
  • 65
  • 88

5 Answers5

13

Succinctly - with a good deal of difficulty.

You have to arrange for the su'd shell to write the child PID to a file and then pick the output. Given that it will be 'joe' creating the file and not 'dex', that adds another layer of complexity.

The simplest solution is probably:

su - joe -c "/path/to/my_daemon & echo \$! > /tmp/su.joe.$$"
bg=$(</tmp/su.joe.$$)
rm -f /tmp/su.joe.$$   # Probably fails - joe owns it, dex does not

The next solution involves using a spare file descriptor - number 3.

su - joe -c "/path/to/my_daemon 3>&- & echo \$! 1>&3" 3>/tmp/su.joe.$$
bg=$(</tmp/su.joe.$$)
rm -f /tmp/su.joe.$$

If you're worried about interrupts etc (and you probably should be), then you trap things too:

tmp=/tmp/su.joe.$$
trap "rm -f $tmp; exit 1" 0 1 2 3 13 15
su - joe -c "/path/to/my_daemon 3>&- & echo \$! 1>&3" 3>$tmp
bg=$(<$tmp)
rm -f $tmp
trap 0 1 2 3 13 15

(The caught signals are HUP, INT, QUIT, PIPE and TERM - plus 0 for shell exit.)

Warning: nice theory - untested code...

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
  • Awesome answer. I might be able to get by with just this: `su - joe -c "/path/to/my_daemon & echo \$! > /tmp/su.joe.$$"` escaping the $! and no semi-colon after the `my_daemon &` are certainly gotchas too. I'm going to play around with it a little bit. – Dex Jun 01 '11 at 06:55
  • Be sure to add a space after the $! or the shell may interpret this weirdly. – Tim Ludwinski Dec 12 '13 at 00:35
1

The approaches presented here didn't work for me. Here's what I did:

PID_FILE=/tmp/service_pid_file
su -m $SERVICE_USER -s /bin/bash -c "/path/to/executable $ARGS >/dev/null 2>&1 & echo \$! >$PID_FILE"
PID=`cat $PID_FILE`
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Dave
  • 2,575
  • 7
  • 36
  • 41
1

As long as the output from the background process is redirected, you can send the PID to stdout:

su "${user}" -c "${executable} > '${log_file}' 2>&1 & echo \$!"

The PID can then be redirected to a file owned by the first user, rather than the second user.

su "${user}" -c "${executable} > '${log_file}' 2>&1 & echo \$!" > "${pid_file}"

The log files do need to be owned by the second user to do it this way, though.

abugher
  • 11
  • 3
-1

Here's my solution

su oracle -c "/home/oracle/database/runInstaller" &
pid=$(pgrep -P $!)

Explantation

  • pgrep -P $! - Gets the child process of the parent pid $!
koola
  • 1,302
  • 1
  • 10
  • 15
-1

I took the above solution by Linux, but had to add a sleep to give the child process a chance to start.

su - joe -c "/path/to/my_daemon > /some/output/file" &
parent=$!
sleep 1
pid=$(pgrep -P $parent)

Running in bash, it doesn't like pid=$(pgrep -P $!) but if I add a space after the ! it's ok: pid=$(pgrep -P $! ). I stuck with the extra $parent variable to remind myself what I'm doing next time I look at the script.

pmoench
  • 1
  • 1