0

I've got a really simple wrapper script to start a ruby program that monitors a network connection. The script's on router - hence we're using ash, not bash.

Since we're using monit to monitor the status, I need a PID file. The problem is, the process id set by the wrapper is one less than the ruby program.

Monit therefore spans hundreds of processes. How can I get the wrapper to start the ruby program and create the correct pidfile?

My wrapper looks like this:

#!/bin/sh /etc/rc.common
  start(){
    echo $$ > /var/run/ping.pid
    ruby /etc/scripts/ping.rb & > /dev/null 2>&1
  }
  stop(){
    kill `cat /var/run/ping.pid`
}
simonmorley
  • 2,810
  • 4
  • 30
  • 61

1 Answers1

2

I think you'll have to do:

#!/bin/sh /etc/rc.common
  start(){
    ruby /etc/scripts/ping.rb & > /dev/null 2>&1
    echo $! > /var/run/ping.pid
  }
  stop(){
    kill `cat /var/run/ping.pid`
}

In POSIX shells (like sh), the $$ contains the current process ID of the shell, while $! contains the process ID of the most recently spawned asynchronous sub-process. In this case, $! contains ruby's PID.