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?




  • stentor142

    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

  • stentor142

    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

  • stentor142

    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.

  • stentor142

    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.

  • parera10

    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

  • parera10

    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

  • shell


    #!/bin/sh

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

  • shell


    #!/bin/sh

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

  • Mike Wyant Jr

    Even two years out that works for me. Thanks.

  • Mike Wyant Jr

    Even two years out that works for me. Thanks.

  • Jens

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

    Regards,
    Jens

    • http://fak3r.com fak3r

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

      $ pgrep -u phil firefox
      10812

  • Jens

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

  • http://fak3r.com fak3r

    Jens, this works great, thanks, I hadn't heard of pgrep:$ pgrep -u phil firefox10812

  • BriggsHorn7875

    my inernet not working properly I have bookmarked ur story i'll read it latterrespectkatiz slop______________________________________________passing drug test | Medical alarm systems | Online Stock Brokerage

  • Tom678

    I subscribed to your blog when is the next postThankspoly banger______________________________________________small business cash advance | Medical alert systems | foundation repair dallas texas

  • http://www.no1golfshop.co.uk vokey wedges

    This is already dude great workregardsManer meshal______________________________________________

  • http://www.oksmokey.com electric cigs

    Where can i have more info on this ?regardsPhillips______________________________________________

  • Luke
    • http://fak3r.com fak3r

      perfect! I’ve never heard of that, thanks

  • Luke
  • http://fak3r.com fak3r

    perfect! I've never heard of that, thanks

  • Luke
  • http://fak3r.com fak3r

    perfect! I've never heard of that, thanks

  • Dev

    Write a C/C++ program that finds the MySQL PID file and prints its contents. Assume that your application will run on the same system running MySQL.

  • http://www.archivems.co.uk/DocumentStorage.asp Archive Storage

    Ya !I am really glad I have found this information. A good blog with exciting content, that’s what I need. Thank you for keeping this site, I will be visiting it. Cant find it.

  • Nik

    well…
    cut -d” ” -f6 works just for usernames containing 4 characters, because the field-number depends on the amount of blanks between username and pid. ist there a way to cut the fields with delimiters containing on or more of the same characters ? maby RegEx ?

  • http://www.mylawtutor.com.au law tutor Perth

    hey i thought this was a great read so i added this to facebook.

  • http://www.therapies4all.com/Alexander-technique.html alexander technique

    Wow!Excellent feedback with have goal theam.I like it & will try to keep it mind.

  • http://www.chesterneurology.com/ Neurologist in Rye Brook NY

    Hi,
    Generally I do not post on blogs, but I would like to say that this post really forced me to do so! really nice post.

  • http://www.constant-content.com/ buy articles

    Hey I love your style I will subscribe for your feed please keep posting!Well done.

  • B1519812

    better this way:

    export PIDFILE=/var/run/${1}.pid
    echo “$$” > “$PIDFILE”

    anyway I guess the start script has to be a bit more complicated.
    In fact, what happen if:

    1) you starting twice the service? the script has to detect that the service is already started
    2) what happen if the service crash or the script is killed? you have an unclean pidfile

    this can be an alternative approach for the start

    if ( set -o noclobber; echo “$$” > “$PIDFILE”) 2> /dev/null;
    then
    trap ‘rm -f “$PIDFILE”; exit $?’ INT TERM EXIT 
    # starting the service in foreground here
     
    rm -f “$PIDFILE” trap – INT TERM EXIT
    else
    echo “The service is already running with PID: $(cat $PIDFILE)”fi In this case if the process crash or the script is killed, the PIDFILE will be removed.
    bye
    Bruno

Read previous post:
Allow Varnish to reuse its shared object

BACKGROUND:  The following is a proposal I submitted to the Varnish developers in order to make it simpler to integrate [...]

Close