iis服务器助手广告广告
返回顶部
首页 > 资讯 > 操作系统 >Linux Shell中判断进程是否存在的代码
  • 140
分享到

Linux Shell中判断进程是否存在的代码

是否存在进程代码 2022-06-04 21:06:37 140人浏览 薄情痞子
摘要

1 利用pgrep 匹配名字 if test $( pgrep -f $1 | wc -l ) -eq 0 then echo "进程不存在" else echo "存在进程" fi 以下是补充内容:

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

--结束END--

本文标题: Linux Shell中判断进程是否存在的代码

本文链接: https://www.lsjlt.com/news/18180.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Linux Shell中怎么判断进程是否存在
    本篇内容介绍了“Linux Shell中怎么判断进程是否存在”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!1 利用pgrep 匹配名字 代码...
    99+
    2023-06-09
  • linux中怎么用shell判断进程是否存在
    在Linux中,可以使用shell脚本来判断一个进程是否存在。可以使用`ps`命令来查看当前正在运行的进程,并使用`grep`命令来...
    99+
    2023-10-26
    linux shell
  • Shell中怎么判断进程是否存在
    Shell中怎么判断进程是否存在,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。简洁版:#! /bin/bash# author caoxin#...
    99+
    2023-06-09
  • Linux shell:脚本判断进程是否在运行
    一.命令说明 ps aux | grep 进程名 | grep -v grep :进程存在则输出信息,不存在则没输出 ps -ef | grep 进程名 | grep -v grep | wc -l ...
    99+
    2023-10-05
    linux 服务器 运维
  • Shell脚本中怎么判断Apache进程是否存在
    Shell脚本中怎么判断Apache进程是否存在,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。写一个脚本检查Apache进程是否存在,若不存在则显示不存在,若存在则显示进程...
    99+
    2023-06-09
  • linux shell如何判断文件是否存在
    这篇文章主要讲解了“linux shell如何判断文件是否存在”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“linux shell如何判断文件是否存在”吧!在linux shell中,可以利...
    99+
    2023-06-21
  • 怎么在linux中素shell 判断文件是否存在
    这期内容当中小编将会给大家带来有关怎么在linux中素shell 判断文件是否存在,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。如果存在某文件,则删除if [ -f tria...
    99+
    2023-06-09
  • linux中怎么用shell判断路径是否存在
    在Linux中,可以使用shell脚本中的test命令来判断路径是否存在。 下面是一个示例脚本,用于判断给定路径是否存在: #!/b...
    99+
    2023-10-27
    linux shell
  • 怎么使用shell脚本判断进程是否存在
    可以使用以下方式来判断进程是否存在: #!/bin/bash # 判断进程是否存在的函数 check_process() { ...
    99+
    2024-04-02
  • DOS中怎么判断进程是否存在
    这篇文章主要讲解了“DOS中怎么判断进程是否存在”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“DOS中怎么判断进程是否存在”吧!检测进程是否存在,并做出预定动作。tasklist ...
    99+
    2023-06-08
  • linux如何判断线程是否存在
    这篇文章主要讲解了“linux如何判断线程是否存在”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“linux如何判断线程是否存在”吧!在mysql中,可用“pthread_kill()”函数判...
    99+
    2023-06-29
  • shell脚本判断文件是否存在
    这篇文章主要介绍“shell脚本判断文件是否存在”,在日常操作中,相信很多人在shell脚本判断文件是否存在问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”shell脚本判断文件是否存在”的疑惑有所帮助!接下来...
    99+
    2023-06-09
  • shell怎么判断进程是否运行
    在shell中,可以使用以下命令来判断进程是否运行:1. 使用`ps`命令和`grep`命令结合,查找指定进程名的进程,并使用`gr...
    99+
    2023-08-12
    shell
  • JavaScript判断文件是否存在的实例代码
    一、业务场景 最近一直在做和文件上传和下载相关的开发,在涉及到下载的时候,我使用如下的方法进行下载 //获取或者赋值一个下载路径 let downUrl; /...
    99+
    2024-04-02
  • golang判断key是否在map中的代码
    个人常用函数 func IsContain(items []string, item string) bool { for _, eachItem := range items...
    99+
    2024-04-02
  • linux怎么判断进程是否在运行
    在Linux中,有以下几种方法可以判断进程是否在运行:1. 使用ps命令:可以使用ps命令查看系统中的进程列表,并通过进程名或PID...
    99+
    2023-10-10
    linux
  • shell中怎么判断文件目录是否存在
    这期内容当中小编将会给大家带来有关shell中怎么判断文件目录是否存在,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。核心代码#!/bin/sh myPath="/var/log/htt...
    99+
    2023-06-09
  • shell脚本怎样判断文件是否存在
    目录shell脚本判断文件是否存在shell脚本之文件是否存在、权限校验判断目录是否存在判断文件是否存在判断目录/文件是否存在判断文件权限判断文件是否属于当前用户判断文件是否与当前用户相同用户组比较文件之间是否为新建复合...
    99+
    2023-06-07
    shell脚本 shell判断文件是否存在 shell判断文件
  • android中判断服务或者进程是否存在实例
    判断进程是否存在 public boolean isUIProcess() { ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERV...
    99+
    2023-05-31
    android 进程 存在
  • 怎么在shell中判断字符串是否存在数组中
    怎么在shell中判断字符串是否存在数组中?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。语法格式: [[ "${array[@]}" =~ ...
    99+
    2023-06-09
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作