Get the App
SLTechnology News&Howtos  ›  Servers  › 

Using python to count the physical memory occupied by Nginx processes

Shulou Source: shulou.com Published: 2022-06-02 00:49:04 09月22日 Update

Implementation code 1:

This method is suitable for statistics of unified names, such as httpd,ngins or php-fpm, etc.

#! / usr/bin/python#coding:utf8from subprocess import Popen, PIPEimport os#. If you need to count httpd, you can change nginx to httpd. Other service statistics are the same, but some of them cannot be implemented. For example, oraclenginxpid = Popen (["pidof", "nginx"], stdout=PIPE) nginxpid = nginxpid.stdout.read () .split () memsum = 0for i in nginxpid: pidfile = os.path.join ("/ proc/", str (I) "status") with open (pidfile) as f: for mem in f: if mem.startswith ("VmRSS"): pidmem = int (mem.split () [1]) memsum + = pidmemprint ("d% s"% (memsum, "KB"))

Implementation code 2:

This method is suitable for a user to do memory usage statistics. The final result is that all the memory information used by a user, the script usage method, and the user name to be counted are added to the script when executing the script.

#! / usr/bin/python#coding:utf8 "" this script requires the system to install smem software Installation method: yum-y install smem "" from subprocess import Popen, PIPEimport sysallmeminfo = Popen (["smem", "- u"] Stdout=PIPE) allmeminfo = allmeminfo.stdout.read () .split ("\ n") for i in allmeminfo: if i.startswith (sys.argv [1]): meminfo = i.split () print ("Process Name:" + meminfo [0]) print ("Total Process:" + meminfo [1]) print ("Physics Memroy Use:" + meminfo [5] + "KB")

The implementation results are as follows:

Tags: Statistics scripts users memory code methods this method results usage information time user names systems software parts services unification physics processes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi vpn Shulou Tech Info Shulou Technology