i need to check whether a process is running or not on server. But challenge is , the keyword I am using is not the userid , it's part of the o/p or ps command.

When i am running as ps -ef | grep -v $$ | grep xenon , I am getting as

error: garbage option

Usage: ps [options]

Try 'ps --help ' or 'ps --help ' for additional help text.

For more details see ps(1). xenon is not running, please check error: garbage option

On server , some reason I see no o/p for pgrep , which will resolve my challenge. He is the o/p from server.

$ps -ef | grep xenon
venom   3157  95310  0 23:08 pts/5    00:00:00 grep --color=auto xenon
root      45943      1  0 Apr12 ?        00:17:47 /opt/xenon/sganet/agent/lib64/sganetwatchdog -vm=/opt/xenon/sganet/agent/lib64/sganetos -logdir /opt/xenon/sganet/log -ini ../conf/ruxitwatchdog.ini
root      45981  45943  2 Apr12 ?        08:20:31 /opt/xenon/sganet/agent/lib64/sganetos -Dcom.compuware.apm.WatchDogTimeout=9000 -Dcom.compuware.apm.WatchDogPort=5000
$pgrep xenon
$pgrep -c xenon

Here is the script I wrote basically to check for validating multiple process running on the server.

set +x
#pg='ps -ef | grep -v $USER | '
pg='ps ax | grep -v grep | '
#--------function to check process ---------#
check_process() {

  $pg $1 >/dev/null && echo -e "$1 is running" || echo -e "$1 is not running, please check "

#--------Checking process on the server---------#

echo -e "\n\n checking the processes"
check_process xenon
check_process upnew

Shell parameter expansion happens after the high-level parsing of pipes breaks the line into structured commands, so your function body runs as though you'd written

ps ax '|' grep -v grep '|'

- that is, with the pipes as literal arguments to ps. That's why you're getting the error from ps (not from grep!) complaining it doesn't understand the | argument.

You could fix that a couple of ways:

  1. Just write the command into the function instead of using the pg variable. This seems like the best version to me, but your actual script may be more complicated.
  2. If there's a real need for something like that, use a function to contain the pipeline instead:

    pg_cmd_1() {
        ps ax | grep -v grep | $1

    and carry on as-is. That would let you define additional functions and change the pg variable dynamically to use them if necessary.

In addition, as currently written your script was trying to run ps ax | grep -v grep | xenon, which I think still isn't what you want (missing a grep?).

  • Yes @Michael , grep is missing . I have copied a wrong line from console. – Vajra Apr 26 at 6:43
  • I have tried as you suggested , I am getting diff error now as pg_cmd: command not found xenon is not running, please check did i miss something ? pg_cmd_1() { ps ax | grep -v $$ | $1 } pg=pg_cmd check_process() { $pg >/dev/null && echo -e "$1 is running" || echo -e "$1 is not running, please check " } – Vajra Apr 26 at 6:47
  • It should have been pg=pg_cmd_1, I've edited the answer. – Michael Homer Apr 26 at 6:49
  • Hello Michael , I thought , it was working , but looks there is something not looking correctly because of my ps command. though the process is not running , its is showing as running . This is the condition I am checking $pg >/dev/null && echo -e "$1 is running" || echo -e "$1 is not running, please check " pg is your logic. – Vajra Apr 26 at 6:57
  • I have marked as working answer is because , as I need to publish this script and option 1 will work to me . Just that I need to add at many places. – Vajra Apr 26 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.