Number of processes in Linux that count the different running states in the current system

Source: Internet
Author: User
  code is as follows copy code
/> cat > test28.sh
      #!/bin/sh
      #1. Initializes counter variables corresponding to runtime, sleep, Stop and Zombies.
      running=0
      sleeping=0
       stopped=0
      zombie=0

#2. In the/proc directory, there are many subdirectories with numbers as directory names, meaning that each number corresponds to a PID that is currently running a process that contains files that describe the information associated with the PID process. Such as 1 represents the PID of the INIT process. The stat file under its subdirectory will contain information related to the running state of the process.
#3. Cat/proc/1/stat, this way you can view the running state of the INIT process, as well as the format of the file, where the third field is the running state field of the process.
#4. The various counters are accumulated by let expression.

The code is as follows Copy Code

For PID in/proc/[1-9]*
Todo
((procs=procs+1))
Stat= ' awk ' {print $} ' $pid/stat '
Case $stat in
R) ((running=runing+1));;
S) ((sleeping=sleeping+1));;
T) ((stopped=stopped+1));;
Z) ((zombie=zombie+1));
Esac
Done
Echo-n "Process Count:"
Echo-e "Running = $runningtSleeping = $sleepingtStopped = $stoppedtZombie = $zombie."
Ctrl+d
/>./test28.sh
Process count:running = 0 sleeping = 136 Stopped = 0 Zombie = 0.

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.