You are not logged in.
Pages: 1
Hi I'm writing a shell daemon (in plain sh), and I'd like to know the PID of a background process I launch to later check if it's still running. Is there a way to do this without having to parse the output of ps or figure out what to give as input to pidof?
Otherwise I'll go with C and fork()... that could be fun
LOL: you can see the "daemon" I'm trying to cook in the window at 9 o'clock.
Last edited by peets (2008-04-08 17:17:06)
Offline
Hi peets,
you can use pidof:
pidof <processname>
Offline
what's the right thing to use for the process name? Right now it's been giving me wrong results (it doesn't match what ps gives me.)
Offline
Hi peets,
try with full pathname of the program:
pidof /usr/bin/myscript.sh
From the man page:
When pidof is invoked with a full pathname
to the program it should find the pid of, it is reasonably safe.
Otherwise it is possible that it returns pids of running programs
that happen to have the same name as the program you're after
but are actually other programs.
Last edited by luca (2008-04-08 13:58:18)
Offline
You can obtain PID this way:
your_command&
PID=$!
and later check whether process still is running:
if [ -d "/proc/${PID}"]; then
...
fi
Offline
I believe you have to do this for a script. You can safely use this permanently as it makes sure that scripts are also detected:
pidof -x myscript.sh
Hopefully this helps:
$ ps axjf
# pid is second column
3436 17055 17055 17055 ? -1 Ss 1000 0:00 \_ xterm
17055 17056 17056 17056 pts/2 17057 Ss 1000 0:00 \_ bash
17056 17057 17057 17056 pts/2 17057 S+ 1000 0:00 \_ top
3436 17364 17364 17364 ? -1 Ss 1000 0:00 \_ xterm
17364 17365 17365 17365 pts/2 17365 Ss+ 1000 0:00 \_ bash
$ pidof -x xterm
17055 17364
$ pidof -x xterm/bash/top
17057
Last edited by dyscoria (2008-04-08 14:12:23)
flack 2.0.6: menu-driven BASH script to easily tag FLAC files (AUR)
knock-once 1.2: BASH script to easily create/send one-time sequences for knockd (forum/AUR)
Offline
robertp: thanks, that's exactly what I was looking for!
!
Expands to the decimal process ID of the most recent background command (see Lists) executed from the current shell. (For example, background commands executed from subshells do not affect the value of "$!" in the current shell environment.) For a pipeline, the process ID is that of the last command in the pipeline.
dyscoria: that's cool, I didn't know that either!
Offline
Pages: 1