1. ps
user@user-ThinkPad-Edge:~$ ps -ef
UID PID PPID C STIME TTY TIME CMD
user 2111 1 0 09:02 ? 00:00:03 evolution
user 2113 1 0 09:02 ? 00:00:00 /usr/lib/bamf/bamfdaemon
user 2132 1 48 09:02 ? 00:27:19 /usr/lib/firefox-3.6.20/firefox-bin
ps -p 根據給定的pid參數判斷是否有這個進程,如果有這個進程正常退出,退出值0.如果沒有這個進程異常退出,退出值1
user@user-ThinkPad-Edge:~$ ps -p 2111
PID TTY TIME CMD
2111 ? 00:00:03 evolution
user@user-ThinkPad-Edge:~$ echo $?
0
user@user-ThinkPad-Edge:~$
user@user-ThinkPad-Edge:~$ ps -p 3333
PID TTY TIME CMD
user@user-ThinkPad-Edge:~$ echo $?
1
2. pgrep
pgrep根據給出的進程名判斷是否有這個名字的進程。如果有這個名字的進程正常退出,退出值0.如果沒有這個名字的進程異常退出,退出值1.
user@user-ThinkPad-Edge:~$ pgrep evolution
1830
2111
user@user-ThinkPad-Edge:~$ echo $?
0
user@user-ThinkPad-Edge:~$ pgrep hello
user@user-ThinkPad-Edge:~$ echo $?
1
3. /proc
每個進程都會在/proc下有一個以進程PID命名的目錄。
user@user-ThinkPad-Edge:~$ ls /proc/2111
attr clear_refs coredump_filter environ fdinfo limits mem mountstats oom_score root sessionid stat syscall
auxv cmdline cpuset exe io loginuid mountinfo net pagemap sched smaps statm task
cgroup comm cwd fd latency maps mounts oom_adj personality schedstat stack status wchan
編寫bash腳本判斷一個進程是否存在
#!/bin/bash
if [ -z $1 ]
then
echo "Need a pid argument"
exit 1
fi
if [ -d /proc/$1 ];then
exit 0
else
exit 1
fi