You are not logged in.

#1 2008-04-08 04:40:36

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

[SOLVED] sh: Getting PID at launch time

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 smile

LOL: toomanytermshx3.th.png 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

#2 2008-04-08 11:05:59

luca
Member
From: Rome
Registered: 2005-10-30
Posts: 280

Re: [SOLVED] sh: Getting PID at launch time

Hi peets,
you can use pidof:

pidof <processname>

Offline

#3 2008-04-08 12:04:51

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: [SOLVED] sh: Getting PID at launch time

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

#4 2008-04-08 13:56:30

luca
Member
From: Rome
Registered: 2005-10-30
Posts: 280

Re: [SOLVED] sh: Getting PID at launch time

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

#5 2008-04-08 14:09:14

robertp
Member
From: Warszawa, Poland
Registered: 2007-09-11
Posts: 123

Re: [SOLVED] sh: Getting PID at launch time

You can obtain PID this way:

your_command&
PID=$!

and later check whether process still is running:

if [ -d "/proc/${PID}"]; then 
...
fi

Offline

#6 2008-04-08 14:10:09

dyscoria
Member
Registered: 2008-01-10
Posts: 1,007

Re: [SOLVED] sh: Getting PID at launch time

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

#7 2008-04-08 17:16:48

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: [SOLVED] sh: Getting PID at launch time

robertp: thanks, that's exactly what I was looking for!

http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html wrote:

!
    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

Board footer

Powered by FluxBB