Linux / Bash, using ps -o to get process by specific name? - linux

I am trying to use the ps -o command to get just specific info about processes matching a certain name. However, I am having some issues on this, when I try to use this even to just get all processes, like so, it just returns a subset of what a normal ps -ef would return (it doesn't return nearly the same number of results so its not returning all running processes)
ps -ef -o pid,time,comm
I want to try something like this (below) but incorporate the ps -o to just get specific info from it (just the PID)
ps -ef |grep `whoami`| grep firefox-bin
Any advice is appreciated as to how to do this properly, thanks

This will get you the PID of a process by name:
pidof name
Which you can then plug back in to ps for more detail:
ps -p $(pidof name)

This is a bit old, but I guess what you want is: ps -o pid -C PROCESS_NAME, for example:
ps -o pid -C bash
EDIT: Dependening on the sort of output you expect, pgrep would be more elegant. This, in my knowledge, is Linux specific and result in similar output as above. For example:
pgrep bash

ps -fC PROCESSNAME
ps and grep is a dangerous combination -- grep tries to match everything on each line (thus the all too common: grep -v grep hack). ps -C doesn't use grep, it uses the process table for an exact match. Thus, you'll get an accurate list with: ps -fC sh rather finding every process with sh somewhere on the line.

Sometimes you need to grep the process by name - in that case:
ps aux | grep simple-scan
Example output:
simple-scan 1090 0.0 0.1 4248 1432 ? S Jun11 0:00

Sorry, much late to the party, but I'll add here that if you wanted to capture processes with names identical to your search string, you could do
pgrep -x PROCESS_NAME
-x Require an exact match of the process name, or argument list if -f is given.
The default is to match any substring.
This is extremely useful if your original process created child processes (possibly zombie when you query) which prefix the original process' name in their own name and you are trying to exclude them from your results. There are many UNIX daemons which do this. My go-to example is ninja-dev-sync.

Related

Linux ps aux with grep check for specific PHP process ID

I'm trying to figure out whether a PHP process is running or not using the ps aux command and passing grep to it, for instance:
I need it to return and tell me whether a process ID on php is running or not but whenever I try the following I always seem to get a result where the result is appending 1234 at the end, what am I missing?
ps aux | grep 'php|1234'
Suggesting pgrep command instead of ps aux
pgrep -af "php"
The reason your get always one line:
php process is not matched with grep 'php|123123123'
ps aux list the grep command you submitted and the grep command match itself
maybe you meant grep -E 'php|123123123' to match php or 123123123
The solution I've come across thanks to a user above is to do:
ps aux | grep '123456' | grep 'grep' -v
Where 123456 would be the process ID

Getting User from processid when multiple user processes exist

I'm trying to tweak a bash script to pull back PID's of the individual application accounts when there are multiple applications running as a masterId. This used to run under individual user accounts, but recent changes have forced the applications to all run under a combined "masterId", but still maintain a unique application Id that I can grep against.
Normally
pgrep -u "appId"
would give me a single PID. Now I have to run:
pgrep -u "masterId"
it returns all of the PID's (each one is it's own application).
1234
2345
3456
I'm trying to come up with a command to bring me back just the PID of the appAccount(n) so I can pipe it into other useful commands. I can do a double grep (which is closer to what I want):
ps aux | grep -i "masterId" | grep -i "appAccount(n)"
and that will get me the entire single process information, but I just want the PID to do something like:
ps aux | grep -i "masterId" | grep -i "appAccount(n)" | xargs sudo -u appAccount(n) kill -9
How do I modify the initial above command to get just the PID? Is there a better way to do this?
pgrep --euid "masterId" --list-full | awk '/appAccount(n)/ {print $1}'
Output the full process command line, then select the one with the desired account and print the first field (pid).

How do I tail top -c?

I have a couple ruby scripts running on my machine and some other ruby processes. The only way I can differentiate them with top is by doing top -c (so I can see the command, otherwise everything is just 'ruby').
I want to be able to watch how many scripts are running so I can restart them if one fails.
I am thinking I can do this with top -c -n 1 | grep "script-name" but I can't figure out how to tail -f that or if that command is the best way to do it in the first place.
I think that top it's not the best choice here, because it's an interactive command and you can't really pipe its whole output (probably there is a way). One of the fair enough ways to do it would be using ps:
ps -e -o pid,cmd | grep "script-name"
If you want to periodically investigate this, you can also use watch:
watch 'ps -e -o pid,cmd | grep "script-name"'
In general, it's a bad practice to grep ps output, but I suppose in your case will work. If you only want the number of running processes that match against a pattern or you just want their PIDs, you'd better go with pgrep.
pgrep "script-name"

How to test if a process is running with grep in bash?

I have the command:
ps ax | grep my_application
Which outputs a large string including the port of the proccesses involved in my_application.
If my_application is not running it outputs:
3873 pts/0 S+ 0:00 grep my_application
I need a condition to test the output of ps ax | grep my_application and do exit 2in case my_application is still running.
Any ideas?
You can add brackets to exclude the grep process:
ps ax | grep -q '[m]y_application' && exit 2
If my_application is running, ps ax will print my_application along with the grep command and pattern. Grep understands [m] as a character class, but it will not match the litteral string '[m]' printed by ps ax, so the grep process is excluded.
The simplest solution is to use pgrep, if it is available on your system.
Otherwise, you can customize the way ps reports processes. You don't have to use the default format, which includes (some) command-line arguments.
For example:
ps ax -ocomm
will only output the executable name. If you want pids as well,
ps ax -opid,comm
For convenient grepping, you might want to remove the headers
ps ax -opid=,comm=
Any of those should work just fine as input to grep application_name, although you still need to watch out for application_name being a substring of another application name.
man ps should give you the list of possible output fields. There are lots of them.
You can exclude stuff from the results returned by grep by using something like this:
ps ax | grep my_application | grep -v grep
This excludes the process that is returning when your application is not running that shows the grep running. When this is run and your application is not running, it will return nothing. Check for the empty string, and exit that way.

Why does my process counting script give false positives?

I have the following bash script, that lists the current number of httpd processes, and if it is over 60, it should email me. This works 80% of the time but for some reason sometimes it emails me anyways when it is not over 60. Any ideas?
#!/bin/bash
lines=`ps -ef|grep httpd| wc -l`
if [ "$lines" -gt "60" ]
then
mailx -s "Over 60 httpd processes" me#me.com < /dev/null
fi
There is a delay between checking and emailing. In that time, some httpd processes might finish, or start, or both. So, the number of processes can be different.
You are including the grep process in the processes (most of the time, it could happen that the ps finishes before grep starts). An easy way to avoid that is to change your command to ps -ef | grep [h]ttpd. This will make sure that grep doesn't match grep [h]ttpd.
On linux, you have pgrep, which might be better suited for your purposes.
grep ... | wc -l can usually be replaced with grep -c ....
If you want to limit the number of httpd requests, I am sure you can set it in apache configuration files.
You've probably thought of this, but ...
At time t0, there are 61.
At time t1, when you read the email, there are 58.
Try including the value of $lines in the email and you'll see.
Or try using /proc/*/cmdline, it might be more reliable.
grep httpd finds all processes that include httpd in their name, including possibly grep httpd itself, and perhaps other ones.
"ps -ef|grep httpd" doesn't find just httpd processes, does it? It finds processes whose full (-f) listing in ps includes the string "httpd".
This probably doesn't solve your issue but you could simplify things by using pgrep instead.
you can do it this way too, reducing the use of grep and wc to just one awk.
ps -eo args|awk '!/awk/&&/httpd/{++c}
END{
if (c>60){
cmd="mailx -s \047Over 60\047 root"
cmd | getline
}
}'

Resources