Reputation: 22535
I have a variable pidfile
, that stores the PID of a process.
How can I kill the pid in pidfile
programmatically using Ruby, assuming I just know the filename, and not the actual PID in it.
Upvotes: 1
Views: 2432
Reputation: 146231
Process.kill(15, File.read('pidfile').to_i)
or even
Process.kill 15, File.read('pidfile').to_i
Now, you could also do something like:
system "kill `cat pidfile`" # or `kill \`cat pidfile\``
However, that approach has more overhead, is vulnerable to code injection exploits, is less portable, and is generally just more of a shell script wrapped in Ruby as opposed to actual Ruby code.
Upvotes: 6