Ps: Unterschied zwischen den Versionen

Aus Foxwiki
Keine Bearbeitungszusammenfassung
K Textersetzung - „= Umgebungsvariablen =“ durch „= Umgebung =“
 
(50 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
'''ps''' (process status) zeigt laufende Prozesse an
{{DISPLAYTITLE:ps}}
'''ps''' (process status) - Staus von Prozesse anzeigen


* Wenn eine ständige Aktualisierung der Prozessliste gewünscht ist, so ist top zu verwenden
== Beschreibung ==
* Mit pgrep lassen sich Prozesse gezielt über den Namen oder eines regulären Ausdrucks suchen
== Installation ==
* pstree kann man alle laufenden Prozesse in Baumform anzeigen
siehe [[Procps]]
 
== Syntax ==
=== Optionen ===
=== Parameter ===
=== Umgebung ===
=== Rückgabewert ===
== Anwendung ==
=== Bestimmte PID ausgeben ===
[[File:2024-01-21_13-00-00_UTC.jpg|mini]]
 
[[File:psExamples.jpg|mini|400px]]


==Nur bestimmte PID ausgeben =
  ps -ef | grep '[j]ava'
  ps -ef | grep '[j]ava'


Or if <tt>pgrep</tt> is available then better to use:
Oder wenn <tt> pgrep </ tt> verfügbar ist, ist Folgendes zu verwenden:
 
  pgrep -f java
  pgrep -f java


You can pipe your output to awk to print just the PID. For example:
Man kann die Ausgabe an awk weiterleiten, um nur die PID zu drucken. Zum Beispiel:
 
  ps -ef | grep nginx | awk '{print $2}'
  ps -ef | grep nginx | awk '{print $2}'
  9439
   
Funktioniert, wenn man die Ausgabe als Variable verwendet a | tr -d '\ n' muss am Ende des Befehls hinzugefügt werden.


Works well, hoverver if you use the output as a variable, a | tr -d '\n' must be added at the end of the command.  
Frage wurde mit Redhat Linux getaggt. Zum Erkennen eines bestimmten Java-Prozesses unter OSX, RHEL Linux und AIX ist <tt> ps -A -o pid , args | grep \ [j] ava </ tt>. Dieser Befehl ignoriert den grep-Prozess und gibt einfach die PID zurück:


Use this: <tt>ps -C <name> -o pid=</tt>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
ps -ef | grep -v grep | grep java | awk '{print $2}'* Kürzer: ps -ef | grep '[j]ava' | awk '{print $2}'
Man benutzt dies, um die PID zu erhalten. '''Achtung:''' wenn man die Ausgabe als Variable a <tt> | verwendet, muss am Ende tr -d '\ n' </ tt> hinzugefügt werden.


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: <tt>ps -A -o pid,args | grep \[j]ava</tt>. This command ignore grep process, and just return PID:
=== pidof ===
ps -ef | grep -v grep | grep java | awk '{print $2}'* Shorter: ps -ef | grep '[j]ava' | awk '{print $2}'
# pidof <process_name>


* I use this to get the PID. Be careful when using the output as a variable, a <tt>| tr -d '\n'</tt> must be added at the end.
Es wird eine Liste von Pids zurückgegeben, die dem Prozessnamen entsprechen
 
==pidof ==
pidof <process_name>
it will return a list of pids matching the process name
[https://linux.die.net/man/8/pidof https://linux.die.net/man/8/pidof]
[https://linux.die.net/man/8/pidof https://linux.die.net/man/8/pidof]
* works as long you don't have multiple instance (e.g. java)  
* funktioniert solange man nicht über mehrere Instanzen verfügt (z.&nbsp;B.&nbsp; Java)
  adb shell procrank | grep TYPE_YOUR_PROCESS_NAME_INSTEAD | awk '{print $1}'
  adb shell procrank | grep TYPE_YOUR_PROCESS_NAME_INSTEAD | awk '{print $1}'
==ps Ausgabeformatierung ==
 
=== Ausgabe ===
Ausgabeformatierung
  ps -A -o pid
  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 <tt>-o pid,cmd,flags</tt>).  
 
Die Ausgabeformatierung des Befehls ist die beste Option. Die Option -o steuert die Ausgabeformatierung. Unten sind einige Argumente aufgelistet, siehe 'man ps' für den Rest (um mehrere zu verwenden, wäre es ''-o pid, cmd, flags'').
  KEY  LONG        DESCRIPTION
  KEY  LONG        DESCRIPTION
   c    cmd          simple name of executable
   c    cmd          simple name of executable
Zeile 45: Zeile 61:
   p    pid          process ID
   p    pid          process ID


==Awk or Cut Would be Better to get Columns ==
=== [[awk]] und  [[cut]] ===
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:
Im Allgemeinen möchte man keinen regulären Ausdruck für die Auswahl der ersten Spalte, sondern möchte ihn zum Ausschneiden leiten oder awk zum Ausschneiden der ersten Spalte wie folgt:
 
  ps ax | awk '{print $1}'
  ps ax | awk '{print $1}'


==Regex is an Option, if not the best ==
=== Regular expression ===
If you were to use regex, it could be something like:
Wenn man [[Regular expression]] verwendet, könnte dies so aussehen
  ps ax | perl -nle 'print $1 if /^ *([0-9]+)/'
  $ ps ax | perl -nle 'print $1 if /^ *([0-9]+)/'
* $1 druckt nur das, was in der Klammer übereinstimmt
* ^ verankert das am Zeilenanfang
* Leerzeichen bedeutet, dass optionale Leerzeichen vor der Nummer zulässig sind
* [0-9] + bedeutet eine oder mehrere Ziffern
 
; Achtung
* Der reguläre Ausdruck darf nicht mit den anfänglichen Leerzeichen vor kleinen Pids übereinstimmen
* Außerdem sollte eine PID mindestens aus einer Ziffer bestehen, nicht aus Null (Achtung: + und nicht * verwenden)
Verwenden Sie den Schalter '' '-o' '', um eine Ausgabe im Cust-Format zu erhalten
$ ps -o pid
 
Der schlechte Weg mit sed kann sein
$ ps -ax | sed 's#^\( *[0-9]\+\) .*$#\1#'
 
''ps -eo pid''' oder '''ps -eo %p''


$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
-e :Alle Prozesse auswählen, -o :Format, pid = Prozess-ID% p sameas pid
* 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
<noinclude>
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'''
== Anhang ==
=== Siehe auch ===
{{Special:PrefixIndex/{{BASEPAGENAME}}}}
==== Dokumentation ====
===== Man-Pages =====
===== Info-Pages =====
==== Links ====
===== Projekt =====
===== Weblinks =====
# https://wiki.ubuntuusers.de/ps/


-e Select all process -o Format pid=process id %p sameas pid
[[Kategorie:Linux/Prozesse]]
[[Kategorie:Linux/Befehl]]


[[category:Linux:Prozessmanagement]]
{{DEFAULTSORT:ps}}
</noinclude>

Aktuelle Version vom 8. September 2024, 11:27 Uhr

ps (process status) - Staus von Prozesse anzeigen

Beschreibung

Installation

siehe Procps

Syntax

Optionen

Parameter

Umgebung

Rückgabewert

Anwendung

Bestimmte PID ausgeben

ps -ef | grep '[j]ava'

Oder wenn pgrep </ tt> verfügbar ist, ist Folgendes zu verwenden:

pgrep -f java

Man kann die Ausgabe an awk weiterleiten, um nur die PID zu drucken. Zum Beispiel:

ps -ef | grep nginx | awk '{print $2}'

Funktioniert, wenn man die Ausgabe als Variable verwendet a | tr -d '\ n' muss am Ende des Befehls hinzugefügt werden.

Frage wurde mit Redhat Linux getaggt. Zum Erkennen eines bestimmten Java-Prozesses unter OSX, RHEL Linux und AIX ist ps -A -o pid , args | grep \ [j] ava </ tt>. Dieser Befehl ignoriert den grep-Prozess und gibt einfach die PID zurück:

ps -ef | grep -v grep | grep java | awk '{print $2}'* Kürzer: ps -ef | grep '[j]ava' | awk '{print $2}'

Man benutzt dies, um die PID zu erhalten. Achtung: wenn man die Ausgabe als Variable a | verwendet, muss am Ende tr -d '\ n' </ tt> hinzugefügt werden.

pidof

# pidof <process_name>

Es wird eine Liste von Pids zurückgegeben, die dem Prozessnamen entsprechen https://linux.die.net/man/8/pidof

  • funktioniert solange man nicht über mehrere Instanzen verfügt (z. B.  Java)
adb shell procrank | grep TYPE_YOUR_PROCESS_NAME_INSTEAD | awk '{print $1}'

Ausgabe

Ausgabeformatierung

ps -A -o pid

Die Ausgabeformatierung des Befehls ist die beste Option. Die Option -o steuert die Ausgabeformatierung. Unten sind einige Argumente aufgelistet, siehe 'man ps' für den Rest (um mehrere zu verwenden, wäre es -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

awk und cut

Im Allgemeinen möchte man keinen regulären Ausdruck für die Auswahl der ersten Spalte, sondern möchte ihn zum Ausschneiden leiten oder awk zum Ausschneiden der ersten Spalte wie folgt:

ps ax | awk '{print $1}'

Regular expression

Wenn man Regular expression verwendet, könnte dies so aussehen

$ ps ax | perl -nle 'print $1 if /^ *([0-9]+)/'
  • $1 druckt nur das, was in der Klammer übereinstimmt
  • ^ verankert das am Zeilenanfang
  • Leerzeichen bedeutet, dass optionale Leerzeichen vor der Nummer zulässig sind
  • [0-9] + bedeutet eine oder mehrere Ziffern
Achtung
  • Der reguläre Ausdruck darf nicht mit den anfänglichen Leerzeichen vor kleinen Pids übereinstimmen
  • Außerdem sollte eine PID mindestens aus einer Ziffer bestehen, nicht aus Null (Achtung: + und nicht * verwenden)

Verwenden Sie den Schalter '-o' , um eine Ausgabe im Cust-Format zu erhalten

$ ps -o pid

Der schlechte Weg mit sed kann sein

$ ps -ax | sed 's#^\( *[0-9]\+\) .*$#\1#'
ps -eo pid oder ps -eo %p

-e :Alle Prozesse auswählen, -o :Format, pid = Prozess-ID% p sameas pid


Anhang

Siehe auch

Dokumentation

Man-Pages
Info-Pages

Links

Projekt
Weblinks
  1. https://wiki.ubuntuusers.de/ps/