You are not logged in.

#1 2010-07-13 20:19:11

pokraka
Member
From: Brussels
Registered: 2009-01-15
Posts: 63

[SOLVED] checking if a process is running using ps

Hi.

I'm writing a shell script which has to check if a given process is running in order to know what to do.

I use the ps command this way :

if test -n `ps -ewwo args | grep '^[^ ]*transmission-daemon'`; then
    echo "Transmission is running."
else
    echo "Transmission is not running."
fi

The ^[^ ]* part is to avoid grep catching his own line in the ps output.

But it doesn't work and I don't understand why.
Even weirder, if I run this command :

echo `ps -ewwo args | grep '^[^ ]*transmission-daemon'`

In the terminal then I get (with transmission-daemon running) : "/usr/bin/transmission-daemon", which seems normal.
But if I put the exact same command in a shell script, then I get an empty string!

What's the cause of this?

And if you have a better idea to check if a process is running it interests me as well.

Thanks.

Last edited by pokraka (2010-07-17 13:52:52)

Offline

#2 2010-07-13 20:31:43

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] checking if a process is running using ps

ls /var/run/ | grep dbus

Offline

#3 2010-07-13 20:42:05

sisco311
Member
From: Romania
Registered: 2008-05-23
Posts: 112

Re: [SOLVED] checking if a process is running using ps

if [ $(pidof transmissions-daemon) ]; then

don't drink unwashed fruit juice.
i never make predictions, especially about the future.

Offline

#4 2010-07-13 20:43:01

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED] checking if a process is running using ps

Use pgrep.

pgrep "$1" >/dev/null && echo "$1 is running" || echo "$1 is not running"

Offline

#5 2010-07-13 20:45:52

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: [SOLVED] checking if a process is running using ps

pokraka wrote:

And if you have a better idea to check if a process is running it interests me as well.

Uhh.. I don't looked at the you attempt, because parsing ps output with grep don't look a good idea.

You have two options (well, you have more but I'm gonna describe two of then).

First is the option karol (somewhat) suggested. Use a run file, i.e. after starting the processes you save the pid to a file, this depend of how you start the processes, one example is

sleep 10 & echo $! >run

The advantage is that you pin point the process.

The other one is something like you are doing but using pgrep instead of ps and grep. Something like this

pgrep transmission-daemon && echo yes || echo no

man pgrep to more options (-x, -f can be useful)

EDIT: falconindy is faster...

Last edited by kazuo (2010-07-13 20:46:36)

Offline

#6 2010-07-13 20:47:48

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] checking if a process is running using ps

> Use pgrep.
+1

As for

echo `ps -ewwo args | grep '^[^ ]*transmission-daemon'`

I get an empty string in the terminal, which means that transmission-daemon isn't running, which is correct.

[karol@black ~]$ echo `ps -ewwo args | grep '^[^ ]*transmission-daemon'`

[karol@black ~]$ echo `ps -ewwo args | grep '^[^ ]*dbus'`
/usr/bin/dbus-daemon --system dbus-launch --autolaunch 0dcc5d71ea3c9513b2b3b6e24aac1fee --binary-syntax --close-stderr /usr/bin/dbus-daemon --fork --print-pid 5 --print-address 7 --session

Offline

#7 2010-07-16 17:48:20

pokraka
Member
From: Brussels
Registered: 2009-01-15
Posts: 63

Re: [SOLVED] checking if a process is running using ps

Thanks for all the replies.

I'm finally using test -e /var/run/daemons/transmissiond to check if transmission-daemon is running, it works well and I don't see a better way to do it in this case.
I keep the pgrep thing somewhere in my head if I ever need it later.

Offline

Board footer

Powered by FluxBB