Thursday, July 5, 2007

the pidof command

Pidof finds the process id’s (pids) of the named programs. It prints those id’s on the standard output.

In rpm-based linux distros, executing this combined with a daemonized service would give you a result in numeric form, which is the process id of the named program.


This could be handy trying to find a specific PID ID of a multihomed full blown production server currently serving a lot of daemonized service besides from doing the grep linux command.

Another way is integrating and calling it from shell script that regularly checks a particular daemon service on regular cron basis say every 5 minutes making sure the service is alive.

I heard stories of daemon service being killed and died due to brute force attack.

One reason is, you might have a newly recruit junior sysad that accidentally for whatever reason killed and tampered that PIDs and suddenly got confused on how to fire it up again. This entry can be an extra linux reminder for that.

And lastly, this keeps you away from midnight phone calls and from standing up late at night just to start a servoce from a newly configured production server for yet an unknown cause. One of my buddies serving almost 2,000 servers had crash issues with a daemons and currently still and *unresolved ticket number from their commercial distro provider.

Here, a basic example of integrating it from a bash script which then checks for dovecot service.

#! /bin/bash
export PATH
date=`date "+%A %m-%d-%Y %r"`

# script to check dovecot

ayos=`/sbin/pidof dovecot`
if [ $dovecotid > 0 ]; then
exit 0
if [ $num = 1 ]; then
echo Dead...Starting Dovecot...
/sbin/service dovecot restart
echo "Restarted at $date" | mail -s "MyServer Dovecot RESTARTED"
echo Dovecot is alive!
exit 0
combined with cronjob as

5,10,15,20,25,30,35,40,45,50,55 * * * * YOURPATH/check_dovecot >/dev/null 2>&1


/5 * * * * YOURPATH/check_dovecot >/dev/null 2>&1

which does the same cron job of executing the script every 5 minutes.

FYI, expanding more of this script in parallel with another clone server would create you a this known heartbeat opensource program that deals basically with waking up backup service/servers.

hope this helps.


Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy