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
0
$
Here is the script I wrote basically to check for validating multiple process running on the server.
#!/bin/bash
set +x
USER=`whoami`
#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 "nn checking the processes"
check_process xenon
check_process upnew
I have 2 suse-11 machine both has same kernel version.
Linux version 2.6.32.59-0.7-default (geeko@buildhost) (gcc version
4.3.4 [gcc-4_3-branch revision 152973] (SUSE Linux) ) #1 SMP 2012-07-13 15:50:56 +0200
but in one machine below command works
ps -u test-o ‘%U %p %P %c’
but in other gives error like below
ERROR: Garbage option.
********* simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
*********** output format ********** *********** long options ***********
-o,o user-defined -f full --Group --User --pid --cols --ppid
-j,j job control s signal --group --user --sid --rows --info
-O,O preloaded -o v virtual memory --cumulative --format --deselect
-l,l long u user-oriented --sort --tty --forest --version
-F extra full X registers --heading --no-heading --context
********* misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum -y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchy
Really not able to figure what is the problem here, can any one suggest me what could be wrong?
EDIT: which ps in both gives
/bin/ps
I’m getting error: Garbage option when I try to run a script with ps -eaf inside.
Why and how do i fix this? I’m using VMWARE Workstation running on my laptop with Vista Home Premium 64-bit. and my guest OS is Oracle Enterpise Linux 4.
ERROR: Garbage option.
-
simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE — DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
-
output format ********** *********** long options ***********
-o,o user-defined -f full —Group —User —pid —cols —ppid
-j,j job control s signal —group —user —sid —rows —info
-O,O preloaded -o v virtual memory —cumulative —format —deselect
-l,l long u user-oriented —sort —tty —forest —version
-F extra full X registers —heading —no-heading —context
-
misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum -y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchy
Applying the docker.containers SLS on CentOS 7 results in the following errors at render time:
[INFO ] Executing command '/usr/bin/ps -p1 | grep -q systemd && echo systemd || echo upstart' in directory '/root'
[ERROR ] Command '/usr/bin/ps -p1 | grep -q systemd && echo systemd || echo upstart' failed with return code: 1
[ERROR ] output: error: garbage option
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).
I’m getting error: Garbage option when I try to run a script with ps -eaf inside.
Why and how do i fix this? I’m using VMWARE Workstation running on my laptop with Vista Home Premium 64-bit. and my guest OS is Oracle Enterpise Linux 4.
ERROR: Garbage option.
-
simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE — DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
-
output format ********** *********** long options ***********
-o,o user-defined -f full —Group —User —pid —cols —ppid
-j,j job control s signal —group —user —sid —rows —info
-O,O preloaded -o v virtual memory —cumulative —format —deselect
-l,l long u user-oriented —sort —tty —forest —version
-F extra full X registers —heading —no-heading —context
-
misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum -y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchy
The other day I wanted to create a simple bash script that would receive the name of a process and print out its pid. I wanted to run ps and then use awk to filter out the correct process and print out its pid. So I created the following bash script (called getPIDOf):
#! /bin/bash
command=”ps -o pid,command -C $1″
for (( i=1; i<=$#; i+=1 )); do
curval=${!i}
command=”$command | grep ”$curval””
done
command=”$command | awk ‘!/awk/ && !/getPIDOf/ {print $1}’”echo $command
$command
I was quite pleased with myself that I had even added in the option to specify multiple identifiers for the process. But when I ran the script I got the following output:
yuval@obt:$ ./getPIDOf nc
ps -o pid,command -C nc | grep “nc” | awk ‘!/awk/ && !/getPIDOf/ {print $1}’
ERROR: Garbage option.
********* simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE — DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
*********** output format ********** *********** long options ***********
-o,o user-defined -f full –Group –User –pid –cols –ppid
-j,j job control s signal –group –user –sid –rows –info
-O,O preloaded -o v virtual memory –cumulative –format –deselect
-l,l long u user-oriented –sort –tty –forest –version
-F extra full X registers –heading –no-heading –context
********* misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum -y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchy
I thought that I must have something wrong with the escaping. But when I ran the command that was printed out, it worked fine:
yuval@obt:$ ps -o pid,command -C nc | grep “nc” | awk ‘!/awk/ && !/getPIDOf/ {print $1}’
892
I even tried the following simplified script:
#! /bin/bash
command=”ps -o pid,command -C $1″
command2=”awk ‘!/awk/ && !/getPIDOf/ {print $1}’”echo “$command | $command2”
$command | $command2
And got the following output:
yuval@obt:$ ./getPIDOf nc
ps -o pid,command -C nc | awk ‘!/awk/ && !/getPIDOf/ {print $1}’
awk: 1: unexpected character ”’
By this time I was quite puzzled. My simple getPIDOf script had become full of strange behaviors. So I decided to take the quick route out and use Perl instead of awk.
Here is my final version of the getPIDOf script:
#!/usr/bin/perl -w
use strict;die “Usage: $0 <identifier1> [identifier2] …n” unless (@ARGV);
my $command = “/bin/ps -o pid,command -C ”$ARGV[0]””;
my $output = `$command`;
foreach my $line (split(/n/, $output))
{
if ($line =~ /^s*(d+)s+(.+?)$/)
{
my $pid = $1;
my $command = $2;
my $found_mismatch = 0;
foreach my $filter (@ARGV)
{
if ($command !~ /$filter/)
{
$found_mismatch = 1;
last;
}
}if ($found_mismatch){next;
}print “$pidn”;
}
}
And the output, as desired, is:
yuval@obt:$ ./getPIDOf nc
892
This entry was posted in Uncategorized. Bookmark the permalink.
Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Hi,
On win8 when I write ‘ ps aux | grep «top» ‘ to the console it says «ERROR: Garbage option», I am writing pipe character by using Ascii code 179 but I don’t think this is the problem. Everything works fine in the console besides ofthis command.
2 Answers
Windows 8 doesn’t come with bash installed which is what the course you are taking is using. So unless you installed bash you have powershell installed which has some similar syntax to bash.
Hi,
The problem was that I used Ascii 179 ( │) instead of 124 ( | ). The two characters looked the same in the Ascii table. Now it works fine. Anyway, I keep in mind your comment, it might be useful later. Thank you.
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 aserror: 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 optionOn 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 0 $
Here is the script I wrote basically to check for validating multiple process running on the server.
#!/bin/bash set +x USER=`whoami` #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 "nn checking the processes" check_process xenon check_process upnew
Answer
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:
- 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. -
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 } pg=pg_cmd_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
?).
Attribution
Source : Link , Question Author : Vajra , Answer Author : Michael Homer