Henley Wing Chiu
Henley Wing Chiu

Reputation: 22535

How do I kill a process whose PID is in a PID file?

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

Answers (1)

DigitalRoss
DigitalRoss

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

Related Questions