01/10/2018, 12:10

Python script for Linux OS

Hi everybody,
I have a exercise like below but I am new in python and I need your idea for this exercise,
If you have any idea, please let me know.
Thanks!

+) Write one script to check status of server in real time (memory, storage, process) if any process take memory over 90% --> kill it.

For this exercise I only know ‘top’ command to check process in real time, to kill one process we use ‘kill -9 PID’ PID is process id get from top command.
But I don’t know how to perform this work in python.

Henry viết 14:20 ngày 01/10/2018

Mình nhớ trong Python có một cái hỗ trợ mấy vấn đề liên quan đến memory và dùng cũng khá đơn giản đó chính là Heapy.
Còn việc cắt process, thì mình không rành lắm.
Nếu là mình thì mình dùng system để call command line.

Truong Phi Nguyen viết 14:25 ngày 01/10/2018

Mình cũng xem 1 vài ví dụ thì có dùng thư viện subprocess để gọi command.nhưng để check trong thời gian thực và kill được process thì không có ý tưởng.
để gọi 1 command thì mình thấy có lệnh này:
subprocess.call([“du”,"-sh",phint])

Truong Phi Nguyen viết 14:16 ngày 01/10/2018

Mình có 1 ví dụ như thế này nhưng chưa clear hoạt động của nó lắm. nó có lấy log từ command line.
from subprocess import Popen,PIPE,STDOUT
cmd = “ls -alrt”
p = Popen (cmd,shell = true,stdin=PIPE,stderr=STDOUT,close_fds= True)
output = p.stdout.read ()
print output

Bài liên quan
0