2
I’m writing a script on python who captures the TOP 5 ram and CPU memory consumption processes of the machine, so far the script is functional only for LINUX, goes below:
Script:
#!/usr/bin/env python
# Desenvolvimento Aberto
# shell.py
# Importar modulos do sistema operacional
import os
import subprocess
import commands
# passando comando em uma variavel para pegar top 5 consumo de memoria
command_mem = '''ps aux --sort=-%mem | head -n 5 | jq -cR '[splits(" +")]' '''
# criando array do comando
value_mem = commands.getoutput(command_mem)
# exibir resultado
print("MEMORIA:\n"+value_mem)
Output:
["user+","10340","13.2","6.9","5119880","553860","?","Sl","ago12","4:46","/usr/lib/jvm/jdk1.8.0_211/bin/java","-XX:+IgnoreUnrecognizedVMOptions","-Xms64m","-Xmx1024m","-jar","/usr/share/dbeaver//plugins/org.eclipse.equinox.launcher_1.4.0.v20161219-1356.jar","-os","linux","-ws","gtk","-arch","x86_64","-showsplash","-launcher","/usr/share/dbeaver/dbeaver","-name","Dbeaver","--launcher.library","/usr/share/dbeaver//plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.551.v20171108-1834/eclipse_1630.so","-startup","/usr/share/dbeaver//plugins/org.eclipse.equinox.launcher_1.4.0.v20161219-1356.jar","--launcher.overrideVmargs","-exitdata","5ee801c","-vm","/usr/bin/java","-vmargs","-XX:+IgnoreUnrecognizedVMOptions","-Xms64m","-Xmx1024m","-jar","/usr/share/dbeaver//plugins/org.eclipse.equinox.launcher_1.4.0.v20161219-1356.jar"]
["user+","12548","1.1","4.1","1226988","332960","?","Sl","00:12","0:02","/opt/kingsoft/wps-office/office6/wps","/home/user/Documents/script/shell-bash/pega-memoria-linux.txt"]
["user+","10062","3.2","3.6","1891904","286776","?","Sl","ago12","1:14","/usr/share/code/code","--type=renderer","--no-sandbox","--service-pipe-token=4B72CA3F1395A79FC84737040282E34F","--lang=pt-BR","--app-path=/usr/share/code/resources/app","--node-integration=true","--webview-tag=true","--no-sandbox","--background-color=#1e1e1e","--num-raster-threads=2","--enable-main-frame-before-activation","--enable-compositor-image-animations","--service-request-channel-token=4B72CA3F1395A79FC84737040282E34F","--renderer-client-id=9","--shared-files=v8_context_snapshot_data:100,v8_natives_data:101"]
["user+","9494","0.1","2.1","1076224","169484","?","SLl","ago12","0:03","/opt/google/chrome/chrome"]
I’d like that in the field COMMAND which is the last field of command PS AUX
it return me only the name of the service/program that is running, because this way that is coming is making it difficult to upload the data to the database because the number of columns becomes relative because of the field COMMAND
I did not know this lib of py, thank you very much for your reply, I will study more her, hug!
– Luis Henrique