1

I have a node.js script I want to daemonize on a Funtoo system (I tagged as Gentoo, since I don't think there's anything really specific to Funtoo in here vs Gentoo), and I do this with the following init script:

#!/sbin/runscript

depend(){
        need redis
}

start(){
        ebegin "Starting myapp"
        NODE_ENV="prod" start-stop-daemon --start --background --make-pidfile -1 /var/log/myapp.log -2 /var/log/myapp.error --chdir /opt/myapp --pidfile /var/run/myapp.pid --exec /usr/bin/node -- /opt/myapp/myapp.js
        eend $?
}

stop(){
        ebegin "Stopping myapp"
        start-stop-daemon --stop --pidfile /var/run/myapp.pid --exec /usr/bin/node -- /opt/myapp/myapp.js
        eend $?
}

With this, I can start and stop the service just fine using the init script, or rc-service. Then, to ensure it stays up in the event of an error, I use monit with the following configuration:

check process myapp with pidfile "/var/run/myapp.pid"
        start program = "/sbin/rc-service myapp start"
        stop program = "/sbin/rc-service myapp stop"
        if failed port 8123 protocol HTTP
                request /myapp.client.js
                with timeout 10 seconds
                then restart

I can see that monit does successfully monitor the application, and detect when it goes down, since when it does crash, I get this filling up my log:

Apr 24 21:05:35 [monit] 'myapp' trying to restart_
Apr 24 21:05:35 [monit] 'myapp' start: /sbin/rc-service_
Apr 24 21:06:05 [monit] 'myapp' failed to start_

Running monit from the console in verbose mode doesn't give anything much more helpful:

'myapp' Error testing process id [30106] -- No such process
'myapp' process is not running
'myapp' trying to restart
'myapp' Error testing process id [30106] -- No such process
'myapp' Error testing process id [30106] -- No such process
'myapp' start: /sbin/rc-service
'myapp' Error testing process id [30106] -- No such process
'myapp' Error testing process id [30106] -- No such process
 * WARNING: myapp has already been started

What appears to be happening is it's never running the stop command, so the pid file still exists, so start-stop-daemon doesn't bring it up. However, I'm not entirely sure how to change this behavior, or if I have it misconfigured (I've used Gentoo/Funtoo for years, but this is my first occasion to write an init script, and this is my first time using monit).

So, in short, what am I missing to make monit successfully restart my service?

UPDATE:

@aseq's answer got me thinking a bit, and I don't quite know why I didn't think of this before. Simply changing the start program line in the config file to:

start program = "/sbin/rc-service myapp restart"

Makes it behave as expected. The logs are still a bit grumpy since start-stop-daemon complains about no matching processes found when trying to stop, but it still comes back up.

1 Answer 1

2

You're on the right path that monit fails to start it because the PID file still exists.

I have ran into these problems before and the solution I used was to create an init script for the particular service, using something like /etc/init.d/skeleton (that's on debian) as a template (or rc.local), making sure a PID file is being created and deleted as appropriate (using start-stop-daemon et al).

I don't think there's really another solution as monit doesn't deal with deleting or creating PID files. Whichever script you use to start and stop the service, it needs to properly create and delete the PID files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .