Code that determines whether a process exists in the Linux shell _linux Shell

Source: Internet
Author: User
1 matching names with Pgrep
Copy Code code as follows:

if test $ (pgrep-f $ | wc-l)-EQ 0
Then
echo "process does not exist"
Else
echo "Existence process"
Fi

The following are supplementary content:
Processes in the current system:

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 according to the given PID parameters to determine whether there is this process, if there is a normal exit of this process, the exit value of 0. If this process does not exit unexpectedly, the exit value is 1.
For example:
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 the process by which the name is to be judged according to the given process name. If the process with this name exits normally, the exit value is 0. If the process does not have this name exited unexpectedly, exit value 1.
For example:
apple@ubuntu:~$ pgrep sshd
2442
2994
3003
apple@ubuntu:~$ echo $?
0
apple@ubuntu:~$ Pgrep sshddd
apple@ubuntu:~$ echo $?
1
apple@ubuntu:~$
3./proc
Each process has a directory named with process PID under/Proc.
For example:
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:~$
You can write a bash script to determine whether a process exists, based on the facts above. Here's a 3rd fact, write a script. Other scripts are similar.
Copy Code code as follows:

#!/bin/bash
If [-Z $]
Then
echo "Need a PID argument"
Exit 1
Fi
if [-d/proc/$1];then
Exit 0
Else
Exit 1
Fi
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.