linux下判断进程存在,linux shell 中判断进程存在

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-binps -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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值