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. |