I'm looking for a ruby script that accesses the /proc
directory and saves the process ID and command line (cmdline
) information in a file.
Asked
Active
Viewed 323 times
1

Andrew Grimm
- 78,473
- 57
- 200
- 338

에이바바
- 1,011
- 11
- 36
- 60
-
1FYI, there is a kernel bug that prevents File.open from working properly with /proc files. We encountered this in Facter and only fixed it by shelling out to `cat` instead. – Rein Henrichs Apr 11 '11 at 05:55
2 Answers
3
you may want to call ps
instead of going to /proc
.
cmd=`ps -eo pid,cmd`
o = File.open("output","w")
o.write(cmd)
o.close

kurumi
- 25,121
- 5
- 44
- 52
-
4Nice one, though I'd advocate showing the block form of File.open so that the file handle is closed for you: `File.open('output','w'){ |f| f << \`ps -eo pid,cmd\` }` – Phrogz Apr 11 '11 at 02:47
0
you can also run below one liner bash script and redirect its output anywhere, as well as choose required argument option for head command.
ls -alR /proc/$(ls /proc/ |grep -i '[0-9]'|sort -n|head ) > /proc_open_files

linux.cnf
- 519
- 6
- 7