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