linux shell 中判断进程存在

来源:互联网 发布:金庸群侠传x如何删数据 编辑:程序博客网 时间:2024/04/28 20:11

1. ps

user@user-ThinkPad-Edge:~$ ps -efUID        PID  PPID  C STIME TTY          TIME CMDuser      2111     1  0 09:02 ?        00:00:03 evolutionuser      2113     1  0 09:02 ?        00:00:00 /usr/lib/bamf/bamfdaemonuser      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 evolutionuser@user-ThinkPad-Edge:~$ echo $?0user@user-ThinkPad-Edge:~$ user@user-ThinkPad-Edge:~$ ps -p 3333  PID TTY          TIME CMDuser@user-ThinkPad-Edge:~$ echo $?1

2. pgrep

pgrep根据给出的进程名判断是否有这个名字的进程。如果有这个名字的进程正常退出,退出值0.如果没有这个名字的进程异常退出,退出值1.

user@user-ThinkPad-Edge:~$ pgrep evolution18302111user@user-ThinkPad-Edge:~$ echo $?0user@user-ThinkPad-Edge:~$ pgrep hellouser@user-ThinkPad-Edge:~$ echo $?1

3. /proc

每个进程都会在/proc下有一个以进程PID命名的目录。

user@user-ThinkPad-Edge:~$ ls /proc/2111attr    clear_refs  coredump_filter  environ  fdinfo   limits    mem        mountstats  oom_score    root       sessionid  stat    syscallauxv    cmdline     cpuset           exe      io       loginuid  mountinfo  net         pagemap      sched      smaps      statm   taskcgroup  comm        cwd              fd       latency  maps      mounts     oom_adj     personality  schedstat  stack      status  wchan

编写bash脚本判断一个进程是否存在

#!/bin/bashif [ -z $1 ]then    echo "Need a pid argument"    exit 1fiif [ -d /proc/$1 ];then    exit 0else    exit 1fi



原创粉丝点击