Sunday, May 20, 2007

A Perl Daemon Example Script

#!/usr/bin/perl -w
#$file: - a daemon example script 
use strict;

# become daemon
my $pid = fork();
print $pid,"\n";
if($pid) {
  #end parent process
  print "#parent process";
}else {
  print "#child process";

# set new process group
# setsid ? only C

while(1) {
  open ("TEST",">>/tmp/test.log");
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
  print TEST ("Now is $year-$mon-$mday $hour:$min:$sec.\n");
  close (TEST);


gabrizo said...

I had a look at the script and it might be helpful for the project I am working on. I just had a couple, I am not sure if this is the place to do it.

I want to use a similar daemon, but the while loop will be used to listen to commands executed by users outside the daemon, for example, a user sends a request to the daemon, the daemon processes the request by reading a file that only has one number and comes back with the last request_id incremented by 1.
This should also ensure that only one request is executed at a time and the rest are queued up.

Blogger Template Layout Design by [ METAMUSE ] : Code Name Gadget 1.1 Power By & Programming Blogs - BlogCatalog Blog Directory