Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Shell script to monitor the usage of swap space and view the processes that occupy swap

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use Shell scripts to monitor swap space usage and view the process that takes up swap", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Shell scripts to monitor swap space usage and view the processes that take up swap.

The code is as follows:

#! / bin/bash

Echo-e `date +% y% m% d% H% M`

Echo-e "PID\ t\ tSwap\ t\ tProc_Name"

# take out all the directories named by numbers under the / proc directory (the process name is the number is the process, and other information such as sys,net is stored)

For pid in `ls-l / proc | grep ^ d | awk'{print $9}'| grep-v [^ 0-9] `

Do

# there is only one way for a process to release swap: restart the process. Or wait for it to be released automatically. Put

# if the process will be automatically released, then we will not write a script to find him, all because he did not automatically release.

# so we want to list the processes that take up swap and need to be restarted, but init is the ancestor of all processes in the system

# restarting the init process means restarting the system, which is absolutely impossible, so there is no need to test it so as not to affect the system.

If [$pid-eq 1]; then continue;fi

Grep-Q "Swap" / proc/$pid/smaps 2 > / dev/null

If [$?-eq 0]; then

Swap=$ (grep Swap / proc/$pid/smaps\

| | gawk'{sum+=$2;} END {print sum}') |

Proc_name=$ (ps aux | grep-w "$pid" | grep-v grep\

| | awk'{for (iTun11witi |

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report