1820619 Members
1871 Online
109626 Solutions
New Discussion юеВ

Re: Regarding ps command

 
SOLVED
Go to solution
vind123
Regular Advisor

Regarding ps command

I want to check whether a process is running in my unix shell script. If the process is not running i want to quit from the shell script
I do the below from unix shell prompt
ps -eaf | grep -i rpt

I want to put this one in a shell script and do the above check. how can i do it?
12 REPLIES 12
RAC_1
Honored Contributor
Solution

Re: Regarding ps command

Simple.

ps -ef|grep [p]rocess_name || exit 1

But, it's not good enough.

UNIX95= ps -C"Process_name" || { echo "Process not running" ; exit 1 ; }
There is no substitute to HARDWORK
vind123
Regular Advisor

Re: Regarding ps command

ps -C "test1.sh"
ps: illegal option -- C
usage: ps [-edaflP] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup]

Also i want to see whether the daemon runs under any user id.
I am getting the below output if i ran when the daemon process is not running
ps -eaf | grep -i test1.sh
hal 1531 1506 1 06:04:35 ttyp4 0:00 grep -i test1.sh
RAC_1
Honored Contributor

Re: Regarding ps command

Look at my post. for doing ps -C, you need to do export UNIX95=1, else just do
UNIX95= ps -C"process_name"
There is no substitute to HARDWORK
James R. Ferguson
Acclaimed Contributor

Re: Regarding ps command

Hi:

RAC's method using the UNIX95 behavior of 'ps' to look for a process by name is guaranteed to match the process's name. Using 'grep' without regular expression anchors in the expression can lead to finding processes you *don't* intend to find!

*However*, don't export UNIX95 into your environment. You may find that the behavior of commands other than 'ps' change in unexpected ways!

By doing:

# UNIX95= ps -C mything -o pid=

...for example, you arm UNIX95 behavior *only* for the duration of the command line -- a safe mode. Note that a space follows the equal sign and no semicolon appears before the 'ps'.

In the example, above, if there was a process named "mything", one line of output would be returned containing the process pid. The equal sign after the keyword "pid" suppresses the normal column header that would appear over it in its absence.

The manpages for 'ps' document and provide more information for these behaviors.

Regards!

...JRF...
spex
Honored Contributor

Re: Regarding ps command

vind123,

If you really want to go the 'ps ... | grep ...' route, you must exclude the grep process itself from the results:

# ps -ef | grep -i test1.sh | grep -v grep

As RAC suggested, "ps -C" is the safer option, as must be an exact match.

# UNIX95=1 ps -o comm= -C test1.sh
will elimate the header line.

#!/usr/bin/sh
UNIX95=1 ps -o comm= -C test1.sh
if [[ ${?} -ne 0 ]]
then
echo "test1.sh not running!" >&2
exit 1
fi
...What to do if test1.sh is running here...
exit 0

PCS
vind123
Regular Advisor

Re: Regarding ps command

I ran the test1.sh in another window and i tried the below things but i am still getting some errors
==> UNIX95=ps -C"test1.sh"
ksh: -Ctest1.sh: not found
==> UNIX95=ps -C test1.sh
ksh: -C: not found
==> UNIX95=ps -C "test1.sh"
ksh: -C: not found
James R. Ferguson
Acclaimed Contributor

Re: Regarding ps command

Hi (again):

You need a space character (or tab) *after* the equal sign and *before* the 'ps' command:

# UNIX95= ps -C test1.sh

...NOT:

# UNIX95=ps -C test1.sh

Since whitespace is difficult to see on the Forum, cut-and-paste the above renditions if you need to see this.

Note that enclosing the program name argument for the '-C' switch is unnecessary.

Regards!

...JRF...

Bill Hassell
Honored Contributor

Re: Regarding ps command

It will help to understand how the -C option works. The man page for ps explains that -C (and -o -H and other options) is not standard. Instead, these options are only valid when XPG4 behavior is requested. The method is to set the variable UNIX95 to anything, which is the same as defining the variable. You can turn on this feature with UNIX95=1 or UNIX95=YYY or the simplest way is UNIX95=. But because UNIX95 can have unexpected results with other programs, you set it temporarily on the same line. Essentially, there are two commands on the line: UNIX95= followed by ps and options.

It may help to understand this temporary construct by using the command this way:

UNIX95=1 ps -o pid= -o args= -C test.sh

I have posted this answer using the Retain formatting option so you can better see the spaces. Again this is all on 1 line.


Bill Hassell, sysadmin
vind123
Regular Advisor

Re: Regarding ps command

Thanks a lot. It's working

1) The below command is working. Hope it will work even if the test.sh runs under another unix user id or root user id?

UNIX95= ps -C test1.sh

2. I tried this one and it gives some error

UNIX95= ps -C test1.sh pid=
ps: Unknown option (pid=).
James R. Ferguson
Acclaimed Contributor

Re: Regarding ps command

Hi (again):

You need:

# UNIX95= ps -C test1.sh -o pid

...whereas you had:

# UNIX95= ps -C test1.sh pid=

...with the '-o ' to signal that the 'pid=' was the option.

Similarly, if you want to add additional fields (here, without labels), repliate the '-o' switch like:

# UNIX95= ps -C vhand -o pid= -o ppid= -o comm=

Regards!

...JRF...
Regards!

...JRF...
vind123
Regular Advisor

Re: Regarding ps command

Thanks a lot for the info
vind123
Regular Advisor

Re: Regarding ps command

Thanks a lot for the info