cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

I just wrote a quick hack to check whether a daemon is running when it starts:

# quit if already running if (-e "/var/run/process_email.pid") { my $pid=9999999999; `cat /var/run/process_email.pid` =~ /(\d+)/ and $pid=$1; exit if (`ps aux|awk '{print \$2}' |grep $pid`); }

but I can't help thinking that there must be a more "perlish" way of doing this.

I did a quick search on CPAN, but I couldn't find a relevant module. Perhaps I was searching on the wrong term?

Anyone know if there's a standard way of doing this? Or is my hack similar to how others have approached this problem?

cLive ;-)

Replies are listed 'Best First'.
Re: checking if daemon is running
by shiza (Hermit) on Dec 12, 2005 at 19:51 UTC
      Perfect. that didn't match "process id" lol.
        Yes, a CPAN search on pidfile will give some more options. :)
Re: checking if daemon is running
by turo (Friar) on Dec 13, 2005 at 00:59 UTC
    Is not a 'perlish' (sorry to perlmonkers-purists for this hibrid), but it is a one line solution:
    perl -e '( system("test -e /var/run/process_email.pid && ps nh -p`cat +/var/run/process_email.pid` >& /dev/null") == 0 ) or print "Not Runni +ng!\n";'

    ^_^>

    perl -Te 'print map { chr((ord)-((10,20,2,7)$i++)) } split //,"turo"'