How to find a PID of a process whose name I don't know exactly? - linux

I can get the PID of a specific process name by
pidof$(ps -C netns)
but what if I don't know the name of the process exactly?
I can't type something like
pidof$(ps -C net*)
so is there any wildcard character, or is there another solution?

Use the -A (all processes) option and filter the result through grep:
pidof $(ps -A | grep "net*")

Just use pgrep -l, eg:
$ pgrep -l sh
1821 sshd
2590 ssh-agent
2658 sh
2677 bash
3025 gvfsd-trash
14785 ksh93
17723 ksh93

try the following and see if you can discover the process as such
This will give you all processes for all users, in a full-format listing
ps auxf
where :
axu = To see every process on the system using BSD syntax
f = fullformat
if the list is too long you can filter if you have an idea of the process name
For example the command below will show you the pids for chrome.
ps auxf | grep chrome

you can use grep and pip :
pidof$(ps -c |grep yor_pattern)

Related

linux ps command to count number of running processes

does anyone know how I can pipe the results of ps -ef | grep ^$USER to wc -1
I already used ps -ef | grep ^$USER but know i want to pipe the command
As per my understanding of your question
you want all the running process from a particular user and pipe it to wc ( note its wc -l not -1)
so i used this
ps aux | grep ^$USER|wc -l
-a : Information for all processes associated with terminals.
-u : Information for processes in userlist.
-x : username (user running this command)
or this can also work
ps -u $USER|wc -l
for any commands if you want to know the details try man command in terminal for example man ps

Pidof not finding the process

I want to find out a shell script process ID using pidof or ps command or any.
All i want is only the process id of it. I have used 'pidof -x test.sh'. Which is not working. Note: I don't want to invoke the /bin/sh or /bin/bash - because the script will not work. If i invoke /bin/sh in script, pidof is working.
Please help
pgrep -f script is giving the expected result.
Thanks
Another caveat even for non-script processes:
pidof ignores:
zombie processes
processes in disk sleep
So pidof as well as killall (same codebase) unlike pgrep won't not see your process as long as it is blocked in disk i/o.
I just experienced this with pidof - found, not found, found, ...
ps -ef | grep your_search_string | awk {printf $2}
both pidof and pgrep are options to find the pid for a certain process. do not run ps -ef | grep "your_command" because you have now polluted your result with the grep match too.
use pidof -s [program] to find the parent process id.
use pidof [program] to find ALL pids (ie. python is running multiple times on your system).
use pgrep [program] to match the name of your binary that is run by python (because pidof is not well suited for this use-case).
Fox explained the difference between pidof vs pgrep quite well. see his answer
I'll copy it here for convenience:
The programs pgrep and pidof are not quite the same thing, but they
are very similar. For example:
$ pidof 'firefox'
5696
$ pgrep '[i]ref'
5696
$ pidof '[i]ref'
$ printf '%s\n' "$?"
1
As you can see, pidof failed to find a match for [i]ref. This is
because pidof program returns a list of all process IDs associated
with a program called program. On the other hand, pgrep re returns a
list of all process IDs associated with a program whose name matches
the regular expression re.
In their most basic forms, the equivalence is actually:
$ pidof 'program'
$ pgrep '^program$'
As yet another concrete example, consider:
$ ps ax | grep '[w]atch'
12 ? S 0:04 [watchdog/0]
15 ? S 0:04 [watchdog/1]
33 ? S< 0:00 [watchdogd]
18451 pts/5 S+ 0:02 watch -n600 tail log-file
$ pgrep watch
12
15
33
18451
$ pidof watch
18451

Why can't pgrep find this process?

If I execute the following, which is just a long command that will wait forever
grep 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd'
then pgrep -f can't find the process, if I search for abcd which is contained in the last segment of the long command.
ps auxww|grep abcd finds the process, but I can't use it in a script, as it also finds the grep process self.
If you remove just one a then pgrep -f abcd can find the process, but I have very long command with arguments, so I have run into this pgrep limitation.
Question
What is the correct way to check for such process based on the unique string abcd?
Your edited command is found by either of these commands:
pgrep -f abcd
or even:
ps uxww | grep '[a]bcd'
Let me try that...
$ grep 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd'
Now in another terminal window:
$ pgrep grep
1842
Found it, or at least some grep process:
$ ps -f $(pgrep grep)
UID PID PPID C STIME TTY TIME CMD
501 1842 1836 0 8:59AM ttys004 0:00.00 grep aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd
Yup, that was the process found.
Let's try this:
$ pgrep -f 'abcd'
1842
Seems to work for me.

Bash - Killing all process in a single command

I have the following bash command which works quite good:
ps aux | grep mpg321 | grep -v "grep mpg321" | awk '{print $2}' | xargs kill
The only problem is, when there is no mpg321 being executed it returns an awful message which I would not like to display to the user.
Is there a "quiet" parameter of a way to put an if in this statement to avoid returning the message?
Usage:
kill [options] <pid> [...]
Options:
<pid> [...] send signal to every <pid> listed
-<signal>, -s, --signal <signal>
specify the <signal> to be sent
-l, --list=[<signal>] list all signal names, or convert one to a name
-L, --table list all signal names in a nice table
-h, --help display this help and exit
-V, --version output version information and exit
For more details see kill(1).
Thank you!
How about killall?
killall mpg321
You can also use pkill as an alternative to killall
By the way xargs has a --no-run-if-empty option if you don't want to execute the command when there are no parameters to add.

Given a PPID, how to find all of associated PID?

It might be a weird question as I searched and found many people asking about how to find the PPID given a PID. However, I'm interested in finding out all the PID of a given PPID.
The incentive was to run a process check using pidstat and from my test with pidstat, it seems to be that pidstat only reports meaningful child process stats and if I have a driver script and I want to access each individual processes within the driver script, I need to htop and find out the PIDs individually. When I invoked a driver script, such as bash script.sh, there is a ID associated with this command and this ID becames the PPID of all the processes within the driver scripts (if I understand it correctly).
So does anyone know how to get all the PIDs of a PPID?
Thanks!
Use pgrep. man pgrep for usage.
pgrep -P <ppid>
Try doing this :
$ ps --ppid <YOUR PPID> -o pid=
$ ps -x -o pid,ppid | grep -E '[^0-9]{Enter PPID HERE}$' # on Mac OS Terminal
.
e.g. if ppid = 1
$ ps -x -o pid,ppid | grep -E '[^0-9]1$' # on Mac OS Terminal

Resources