[ SHELL ] PID - check ob Prozess läuft

peace_maker

Cadet 4th Year
Registriert
Dez. 2010
Beiträge
108
Hallo,
ich wollte ein kleines Shell-Script schreiben, dass überprüft ob die PID('s), die ihm übergeben wurde(n) [Bash oder File] läuft/laufen oder nicht.

Es würde mit einem Abgleich der Ausgabe von `ps aux` gehen. z.B.:
ps aux | awk '/[0-9]/ { print $2 }'
oder gleich in eine Variabel
meine_PIDS=$(ps aux | awk '/[0-9]/ { print $2 }' )

Dadurch erhalte ich dann eine Liste mit den PID's, die mit den übergebenen PID's auf Gleichheit geprüft werden sollen.
Nun meine Frage - wie kann ich die Listen miteinander vergleichen ? :)

Gruß
peace_maker
Ergänzung ()

Soo .. für Variablen hab ichs schonmal abgedeckt . was noch fehlt ist die Mögliche den Input auch über manuelle Eingabe oder Textdatei einzuholen. . kommt noch.

Ist leider etwas dick geworden - is aber auch die erste version also.. muss später noch schauen was raus kann.
________________________________________________________________________________________
#Test if Process is running
is_running(){
run=1
all_run=1
found=0
all_pid=$(ps aux | awk '/[0-9]/ { print $2 }')
#Testing if PID was found by ps aux
#Comparing values
pid_temp=$(echo "$PID" | awk 'NR=='$run'{ print $1 }')
all_pid_temp=$(echo "$all_pid" | awk 'NR=='$all_run'{ print $1 }')
while [[ -n "$pid_temp" ]]
do

pid_temp=$(echo "$PID" | awk 'NR=='$run'{ print $1 }')
while [[ -n "$all_pid_temp" ]]
do
all_pid_temp=$(echo "$all_pid" | awk 'NR=='$all_run'{ print $1 }')
if [ "$pid_temp" = "$all_pid_temp" ]
then
echo -n "The process $pid_temp is running"
found=1
fi
all_run=$(($all_run + 1))
done
run=$((run +1))
done

if [ $found -eq 0 ]
then
echo "PID(s) not found."
fi
menu
}
________________________________________________________________________________

Gruß
peace_maker
 
Moinsens,

Mit python ist das ein 5 Zeiler:

Code:
import sys
import psutil

for arg in sys.argv[1:]:

    if psutil.pid_exists(int(arg)) == True:
        print "PID %s is running" % arg
    else:
        print "PID %s is not running" % arg

Code:
[foobar@xxx python]# python pid_checker.py 4226 4225
PID 4226 is running
PID 4225 is not running
[foobar@xxx python]#


Du brauchst zwar die psutil lib - aber kommst doch elegant zum Ziel :-)
 
Zurück
Oben