0
我試圖創建一個程序,讀取ps
並輸出pid
和commandline
,但如果過程是由內核啓動它應該返回一個空行替換字符串。在文件中使用Ruby
require 'fileutils'
procs=`ps -eo pid,cmd`
o = File.open("proc","w")
f = o.write("proc")
o.close
f_in = File.open('proc', 'r')
f_out = File.open('procs', 'w')
replace = ""
f_in.each do |line|
if line =~ (/\s*\[(\w+)\]\$/)
f_out << "\n"
else
f_out << line
end
end
f_out.write("procs")
f_in.close
f_out.close
FileUtils.mv "procs", ["proc", Time.now.strftime("%Y-%m-%d")].join(".")
PS -eo PID,CMD喜歡:
PID CMD 1 /sbin/init 2 [migration/0] 3 [ksoftirqd/0] 4 [watchdog/0] 5 [events/0] 6 [khelper] 7 [kthread] 8 [xenwatch] 9 [xenbus] 17 [kblockd/0]
我想刪除所有行的括號,但保留PID是這樣的:
PID CMD 1 /sbin/init 2 3 4 5 6 7 8 9 17