Code used to determine whether a process exists in Linux Shell

Source: Internet
Author: User

1. Use pgrep to match the name Copy codeThe Code is as follows: if test $ (pgrep-f $1 | wc-l)-eq 0
Then
Echo "the process does not exist"
Else
Echo "process"
Fi

The following is a supplement:
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 :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 :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 :57? 00:00:00 [kstriped]
Root 42 2 0 13:57? 00:00:00 [kmpathd/0]
Root 43 2 0 :57? 00:00:00 [kmpathd/1]
Root 44 2 0 :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? 00:00:00 [mpt_poll_0]
Root 717 2 0? 00:00:00 [scsi_eh_2]
Root 753 2 0? 00:00:00 [kdmflush]
Root 764 2 0? 00:00:00 [kdmflush]
Root 795 2 0? 00:00:00 [kjournald]
Root 915 1 0? 00:00:00/sbin/udevd -- daemon
Root 1617 2 0? 00:00:00 [kpsmoused]
Root 2166 1 0 00:00:00 tty4/sbin/getty 38400 tty4
Root 2167 1 0 00:00:00 tty5/sbin/getty 38400 tty5
Root 2172 1 0 00:00:00 tty2/sbin/getty 38400 tty2
Root 2173 1 0 00:00:00 tty3/sbin/getty 38400 tty3
Root 2174 1 0 00:00:00 tty6/sbin/getty 38400 tty6
Root 2244 1 0? 00:00:00/usr/sbin/acpid-c/etc/acpi/eve
Root 2259 1 0? 00:00:00/usr/sbin/syslog-ng-p/var/run/
105 2279 1 0? 00:00:00/bin/Alibaba-daemon -- system
Root 2311 1 0? 00:00:00/usr/sbin/incrond-f/etc/incron
Root 2381 1 0? 00:00:00 dhclient3-e IF_METRIC = 100-pf/
Root 2442 1 0? 00:00:00/usr/sbin/sshd
Root 2453 1 0? 00:00:00/usr/lib/postfix/master
Postfix 2465 2453 0? 00:00:00 pickup-l-t fifo-u-c
Postfix 2468 2453 0? 00:00:00 qmgr-l-t fifo-u
108 2472 1 0? 00:00:00/usr/sbin/hald
Root 2475 1 0? 00:00:00/usr/sbin/console-kit-daemon
Root 2538 2472 0? 00:00:00 hald-runner
Root 2567 2538 0? 00:00:00 hald-addon-input: Listening on/
Root 2594 2538 0? 00:00:00 hald-addon-storage: polling/dev
Root 2595 2538 0? 00:00:00 hald-addon-storage: no polling o
108 2600 2538 0? 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? 00:00:00/usr/sbin/cron
Root 2691 1 0 00:00:00 tty1/sbin/getty 38400 tty1
Root 2994 2442 0? 00:00:00 sshd: apple [priv]
Apple 3003 2994 0? 00:00:00 sshd: apple @ pts/0
Apple 3004 3003 1 00:00:00 pts/0-bash
Apple 3024 3004 0 00:00:00 pts/0 ps-ef
Apple @ ubuntu :~ $
1. ps-p
Ps-p determines whether the process exists based on the given pid parameter. If the process Exits normally, the exit value is 0. If the process does not exit abnormally, 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 determines whether a process has this name based on the given process name. If a process with this name Exits normally, the exit value is 0. If a process without this name exits abnormally, the exit value is 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 after the process PID in/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 indexing line cwd fd latency maps mounts oom_adj personality schedstat syscall
Cgroup coredump_filter environ fdinfo limits mem mountstats oom_score root sessionid statm task
Apple @ ubuntu :~ $
Based on the above facts, you can write a bash script to determine whether a process exists. The following is a script written based on 3rd facts. Other scripts are similar.Copy codeThe Code is as follows :#! /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

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.