linux里判断进程是否运行,Linux Shell中判断进程是否存在的代码
Linux Shell中判断进程是否存在的代码更新时间:2013年01月12日 16:42:52作者:有时候我们需要在linux中判断进程是否存在,然后再执行相应的操作,这里简单的分享下,方便需要的朋友1 利用pgrep 匹配名字if test $( pgrep -f $1 | wc -l ) -eq 0thenecho "进程不存在"elseecho "存在进程"fi以下是补充内容:当前系...
Linux Shell中判断进程是否存在的代码
更新时间:2013年01月12日 16:42:52 作者:
有时候我们需要在linux中判断进程是否存在,然后再执行相应的操作,这里简单的分享下,方便需要的朋友
1 利用pgrep 匹配名字
if test $( pgrep -f $1 | wc -l ) -eq 0
then
echo "进程不存在"
else
echo "存在进程"
fi
以下是补充内容:当前系统中的进程:
apple@ubuntu:~$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 13:57 ? 00:00:02 /sbin/init
root 2 0 0 13:57 ? 00:00:00 [kthreadd]
root 3 2 0 13:57 ? 00:00:00 [migration/0]
root 4 2 0 13:57 ? 00:00:00 [ksoftirqd/0]
root 5 2 0 13:57 ? 00:00:00 [watchdog/0]
root 6 2 0 13:57 ? 00:00:00 [migration/1]
root 7 2 0 13:57 ? 00:00:00 [ksoftirqd/1]
root 8 2 0 13:57 ? 00:00:00 [watchdog/1]
root 9 2 0 13:57 ? 00:00:00 [events/0]
root 10 2 0 13:57 ? 00:00:00 [events/1]
root 11 2 0 13:57 ? 00:00:00 [khelper]
root 12 2 0 13:57 ? 00:00:00 [kstop/0]
root 13 2 0 13:57 ? 00:00:00 [kstop/1]
root 14 2 0 13:57 ? 00:00:00 [kintegrityd/0]
root 15 2 0 13:57 ? 00:00:00 [kintegrityd/1]
root 16 2 0 13:57 ? 00:00:00 [kblockd/0]
root 17 2 0 13:57 ? 00:00:00 [kblockd/1]
root 18 2 0 13:57 ? 00:00:00 [kacpid]
root 19 2 0 13:57 ? 00:00:00 [kacpi_notify]
root 20 2 0 13:57 ? 00:00:00 [cqueue]
root 21 2 0 13:57 ? 00:00:00 [ata/0]
root 22 2 0 13:57 ? 00:00:00 [ata/1]
root 23 2 0 13:57 ? 00:00:00 [ata_aux]
root 24 2 0 13:57 ? 00:00:00 [ksuspend_usbd]
root 25 2 0 13:57 ? 00:00:00 [khubd]
root 26 2 0 13:57 ? 00:00:00 [kseriod]
root 27 2 0 13:57 ? 00:00:00 [kmmcd]
root 28 2 0 13:57 ? 00:00:00 [btaddconn]
root 29 2 0 13:57 ? 00:00:00 [btdelconn]
root 30 2 0 13:57 ? 00:00:00 [pdflush]
root 31 2 0 13:57 ? 00:00:00 [pdflush]
root 32 2 0 13:57 ? 00:00:00 [kswapd0]
root 33 2 0 13:57 ? 00:00:00 [aio/0]
root 34 2 0 13:57 ? 00:00:00 [aio/1]
root 35 2 0 13:57 ? 00:00:00 [ecryptfs-kthrea]
root 38 2 0 13:57 ? 00:00:00 [pciehpd]
root 39 2 0 13:57 ? 00:00:00 [scsi_eh_0]
root 40 2 0 13:57 ? 00:00:00 [scsi_eh_1]
root 41 2 0 13:57 ? 00:00:00 [kstriped]
root 42 2 0 13:57 ? 00:00:00 [kmpathd/0]
root 43 2 0 13:57 ? 00:00:00 [kmpathd/1]
root 44 2 0 13:57 ? 00:00:00 [kmpath_handlerd]
root 45 2 0 13:57 ? 00:00:00 [ksnapd]
root 46 2 0 13:57 ? 00:00:00 [kondemand/0]
root 47 2 0 13:57 ? 00:00:00 [kondemand/1]
root 48 2 0 13:57 ? 00:00:00 [krfcommd]
root 256 2 0 13:57 ? 00:00:00 [mpt_poll_0]
root 717 2 0 13:57 ? 00:00:00 [scsi_eh_2]
root 753 2 0 13:57 ? 00:00:00 [kdmflush]
root 764 2 0 13:57 ? 00:00:00 [kdmflush]
root 795 2 0 13:57 ? 00:00:00 [kjournald]
root 915 1 0 13:57 ? 00:00:00 /sbin/udevd --daemon
root 1617 2 0 13:57 ? 00:00:00 [kpsmoused]
root 2166 1 0 13:57 tty4 00:00:00 /sbin/getty 38400 tty4
root 2167 1 0 13:57 tty5 00:00:00 /sbin/getty 38400 tty5
root 2172 1 0 13:57 tty2 00:00:00 /sbin/getty 38400 tty2
root 2173 1 0 13:57 tty3 00:00:00 /sbin/getty 38400 tty3
root 2174 1 0 13:57 tty6 00:00:00 /sbin/getty 38400 tty6
root 2244 1 0 13:57 ? 00:00:00 /usr/sbin/acpid -c /etc/acpi/eve
root 2259 1 0 13:57 ? 00:00:00 /usr/sbin/syslog-ng -p /var/run/
105 2279 1 0 13:57 ? 00:00:00 /bin/dbus-daemon --system
root 2311 1 0 13:58 ? 00:00:00 /usr/sbin/incrond -f /etc/incron
root 2381 1 0 13:58 ? 00:00:00 dhclient3 -e IF_METRIC=100 -pf /
root 2442 1 0 13:58 ? 00:00:00 /usr/sbin/sshd
root 2453 1 0 13:58 ? 00:00:00 /usr/lib/postfix/master
postfix 2465 2453 0 13:58 ? 00:00:00 pickup -l -t fifo -u -c
postfix 2468 2453 0 13:58 ? 00:00:00 qmgr -l -t fifo -u
108 2472 1 0 13:58 ? 00:00:00 /usr/sbin/hald
root 2475 1 0 13:58 ? 00:00:00 /usr/sbin/console-kit-daemon
root 2538 2472 0 13:58 ? 00:00:00 hald-runner
root 2567 2538 0 13:58 ? 00:00:00 hald-addon-input: Listening on /
root 2594 2538 0 13:58 ? 00:00:00 hald-addon-storage: polling /dev
root 2595 2538 0 13:58 ? 00:00:00 hald-addon-storage: no polling o
108 2600 2538 0 13:58 ? 00:00:00 hald-addon-acpi: listening on ac
daemon 2647 1 0 13:58 ? 00:00:00 /usr/sbin/atd
root 2672 1 0 13:58 ? 00:00:00 /usr/sbin/cron
root 2691 1 0 13:58 tty1 00:00:00 /sbin/getty 38400 tty1
root 2994 2442 0 14:11 ? 00:00:00 sshd: apple [priv]
apple 3003 2994 0 14:11 ? 00:00:00 sshd: apple@pts/0
apple 3004 3003 1 14:11 pts/0 00:00:00 -bash
apple 3024 3004 0 14:11 pts/0 00:00:00 ps -ef
apple@ubuntu:~$
1.ps -p
ps -p 根据给定的pid参数判断是否有这个进程,如果有这个进程正常退出,退出值0.如果没有这个进程异常退出,退出值1.
例如:
apple@ubuntu:~$ ps -p 2442
PID TTY TIME CMD
2442 ? 00:00:00 sshd
apple@ubuntu:~$ echo $?
0
apple@ubuntu:~$ ps -p 1234
PID TTY TIME CMD
apple@ubuntu:~$ echo $?
1
apple@ubuntu:~$
2.pgrep
pgrep根据给出的进程名判断是否有这个名字的进程。如果有这个名字的进程正常退出,退出值0.如果没有这个名字的进程异常退出,退出值1.
例如:
apple@ubuntu:~$ pgrep sshd
2442
2994
3003
apple@ubuntu:~$ echo $?
0
apple@ubuntu:~$ pgrep sshddd
apple@ubuntu:~$ echo $?
1
apple@ubuntu:~$
3./proc
每个进程都会在/proc下有一个以进程PID命名的目录。
例如:
apple@ubuntu:~$ ls /proc/2442
ls: cannot read symbolic link /proc/2442/cwd: Permission denied
ls: cannot read symbolic link /proc/2442/root: Permission denied
ls: cannot read symbolic link /proc/2442/exe: Permission denied
attr clear_refs cpuset exe io loginuid mountinfo net pagemap sched smaps status wchan
auxv cmdline cwd fd latency maps mounts oom_adj personality schedstat stat syscall
cgroup coredump_filter environ fdinfo limits mem mountstats oom_score root sessionid statm task
apple@ubuntu:~$
可以根据上面的事实,编写bash脚本判断一个进程是否存在。下面以第3个事实,编写脚本。其他的脚本类似。
#!/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
相关文章
这篇文章主要介绍了本地文件与服务器文件同步shell脚本的方法,然后在文章给大家补充介绍了shell脚本备份本地服务器的文件到远程服务器的实例代码,需要的朋友可以参考下2017-11-11
在linux中有很多查找命令,今天小编抽空给大家讲解find和locate两个命令,非常不错,具有参考借鉴价值,需要的朋友参考下吧2017-11-11
这篇文章主要介绍了Shell脚本避免重复执行的方法的,本文给出的代码只要放在需要执行的脚本头部即可避免重复执行,非常方便实用,需要的朋友可以参考下2015-01-01
这篇文章主要介绍了linux下2个检查tcp连接的命令,需要的朋友可以参考下2016-08-08
Windows 系统自带一个名为Shutdown.exe的程序,可以用于关机操作(位置在Windows\System32下),一般情况下Windows系统的关机都可以通过调用程序 shutdown.exe来实现的,同时该程序也可以用于终止正在计划中的关机操作2017-09-09
下面小编就为大家带来一篇linux修改tomcat默认访问项目的具体步骤(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-03-03
最近因为工作需要,需要编写shell脚本。编写大量shell脚本时,累计了大量经验,也让自己开始迷糊几种函数输出调用的区别。后面和前辈们请教了一下这个问题,大致明白了区别。这里分享给大家,希望对大家能有所收获。下面来一起看看吧。2016-10-10
这篇文章主要介绍了Shell脚本判断IP地址是否合法的方法,需要的朋友可以参考下2014-03-03
BASH 学习笔记小结,想要学习linux bash编程的朋友可以参考下2012-05-05
这篇文章主要介绍了自己常用的一些shell脚本分享,包含20多个脚本实例,需要的朋友可以参考下2014-08-08
最新评论
更多推荐
所有评论(0)