look out honey 'cause I'm using technology

HOWTO: create a pidfile for a startup script

On the monit mailing list today someone asked how they could monitor a process that didn’t have a pidfile associated with it.  Without thinking I jotted this down, there’s likely a better way, but this should work and may be all I need for some init.d scripts for a couple of apps on ramon (the home server).  In the the beginning of the startup script, define the PIDFILE with the path and the cmd followed by the pid suffix and then just dump the PID number from the ps output into it:

export PIDFILE=/var/run/${1}.pid
ps -fe | grep ${1} | head -n1 | cut -d" " -f 6 > ${PIDFILE}

Once this is done, monit can monitor it just like it monitors any other process with a PID.  Later, for a shutdown hook, nuke the PIDFILE on the way out.

if [ -f ${PIDFILE} ]; then
rm ${PIDFILE}
fi
### rest of shutdown ###
exit 0

I think that should do it, anyone see a problem with that / a better way?

    

View Comments to “HOWTO: create a pidfile for a startup script”

  1. stentor142 says:

    This is not professional. Also this way script doesn’t work every time because of sort of columns and the spaces before the PID numeber will not be every time 2 for your case.

    root 2783 1 0 14:23 ?
    root 32528 2940 0 21:31 ?
    root 32536 32528 0 21:31 pts/2

  2. stentor142 says:

    Sorry for the bad example, but it seems the field for the comments doesn’t sorting anything. So, look at your console and see when you have 3 number PID, 4 and even 5 number PID how they are sorted.

  3. parera10 says:

    Hi,
    I had the same problem. I solved it doing this:

    In your init script, after launch the daemon in the “start” function you can use this:
    pidof $prog > $pidfile

  4. shell says:


    #!/bin/sh

    vi newfile &
    PID_OF_VI=$!
    echo $PID_OF_VI>/var/run/.vipid

  5. Mike Wyant Jr says:

    Even two years out that works for me. Thanks.

  6. Jens says:

    to find out the process id, use pgrep instead of your “ps -fe | grep ${1} | head -n1 | cut -d” ” -f 6 > ${PIDFILE}”

    Regards,
    Jens

  7. fak3r says:

    Jens, this works great, thanks, I hadn't heard of pgrep:

    $ pgrep -u phil firefox
    10812

  8. BriggsHorn7875 says:

    my inernet not working properly I have bookmarked ur story i'll read it latter

    respect
    katiz slop
    ______________________________________________
    passing drug test | Medical alarm systems | Online Stock Brokerage

  9. Tom678 says:

    I subscribed to your blog when is the next post

    Thanks
    poly banger
    ______________________________________________
    small business cash advance | Medical alert systems | foundation repair dallas texas

  10. This is already dude great work

    regards
    Maner meshal
    ______________________________________________

  11. Where can i have more info on this ?

    regards
    Phillips
    ______________________________________________

  12. fak3r says:

    perfect! I've never heard of that, thanks

  13. fak3r says:

    perfect! I've never heard of that, thanks

Leave a Reply

blog comments powered by Disqus