Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
118 CHAPTER 4 Introduction to Ruby #!/usr/bin/ruby require 'etc' pid = ARGV[0].to_i cwd = File.readlink("/proc/#{pid}/cwd") owner = File.stat("/proc/#{pid}/cmdline").uid For our program, we want to collect the process owner, the current working directory (from where the program was run), and the full command line that was used to run it. We will get the process ID as an argument from the command line and store it in pid . Next, we will look in /proc to examine the cwd link for the process. The readlink method of our file class will fetch the destination of a symbolic link and return it as a string. We store that string in our cwd variable. Then we will use the stat command, which will return a large amount of information about the file including last modification, creation, and access times. More importantly to us, this will return the owner of the file. We will use the uid variable returned from stat to get the numeric user ID of the file owner and assign it to owner . file = File.new("/proc/#{pid}/cmdline") cmdline = file.read.split("\000")