Ps
ps (processes) liefert eine Momentaufnahme der laufenden Prozesse
- Wenn eine ständige Aktualisierung der Prozessliste gewünscht ist, so ist top zu verwenden.
- Mit pgrep lassen sich Prozesse gezielt über den Namen oder eines regulären Ausdrucks suchen
- pstree kann man alle laufenden Prozesse in Baumform anzeigen.
Vorlage:Anchor Nur bestimmte PID ausgeben
ps -ef | grep '[j]ava'
Or if pgrep is available then better to use:
pgrep -f java
You can pipe your output to awk to print just the PID. For example:
ps -ef | grep nginx | awk '{print $2}' 9439
Works well, hoverver if you use the output as a variable, a | tr -d '\n' must be added at the end of the command.
Use this: ps -C <name> -o pid=Why is this voted down? Not only does it seem to work, but does so using the desired command ps, and no pipe filters. In my case, I couldn't use pipes (reasons..) so this was a lifesaver. You could spend a whole day reading the man page for
Maybe because it's not extremely portable, but then again the other solutions aren't either, and the original question was tagged with Redhat Linux. Just happened to see a commit by one of my engineers who needed to have a portable way to detect a specific java process on OSX, RHEL Linux and AIX, and this is what they came up with: ps -A -o pid,args | grep \[j]ava. This command ignore grep process, and just return PID:
ps -ef | grep -v grep | grep java | awk '{print $2}'* Shorter: ps -ef | grep '[j]ava' | awk '{print $2}'
- I use this to get the PID. Be careful when using the output as a variable, a | tr -d '\n' must be added at the end.
Vorlage:Anchor pidof
pidof <process_name>
it will return a list of pids matching the process name https://linux.die.net/man/8/pidof
- works as long you don't have multiple instance (e.g. java)
adb shell procrank | grep TYPE_YOUR_PROCESS_NAME_INSTEAD | awk '{print $1}'
Vorlage:Anchor ps Ausgabeformatierung
ps -A -o pid
Output formatting of the command is the best option. The o option controls the output formatting. I listed some of arguments below below, see 'man ps' for the rest ( to use multiple it would be -o pid,cmd,flags).
KEY LONG DESCRIPTION c cmd simple name of executable C pcpu cpu utilization f flags flags as in long format F field g pgrp process group ID G tpgid controlling tty process group ID j cutime cumulative user time J cstime cumulative system time k utime user time o session session ID p pid process ID
Vorlage:Anchor Awk or Cut Would be Better to get Columns
Generally you wouldn't want a regex for selecting the first column, you would want to pipe it to cut or awk to cut out the first column like:
ps ax | awk '{print $1}'
Vorlage:Anchor Regex is an Option, if not the best
If you were to use regex, it could be something like:
ps ax | perl -nle 'print $1 if /^ *([0-9]+)/'
$1 prints only what was matched in the parenthesis. ^ anchors the to the start of the line. Space asterisk means allow for optional space characters before the number. [0-9]+ means one or more digits. But I wouldn't recommend regex for this particular task, see why? :-)* thanks! but the regular expression would be a great addition, if anyone could contribute with a second answer
- really, use awk.
- Pay attention, the regex would not match the initial spaces eventually being before small pids. Moreover a pid should be make of a digit at least, not zero (you should use + and not *)
- AlberT, oh good point, Didn't see that they were right justified. That is why I said regex is not the best I guess, will fix :-)
- I can't argue how to use cut, by means of the initial spaces of course. Can you point out to me how to use cut? Just for the sake of curiosity :)
Use the -o switch to have a cust format output
ps -o pid
The bad way using sed, as you explicitly asked may be
ps -ax | sed 's#^\( *[0-9]\+\) .*$#\1#'
ps -eo pid or ps -eo %p
-e Select all process -o Format pid=process id %p sameas pid