Perl编写Daemon进程

(0 comments)

编写Daemon进程,每隔30秒在日志文件中插入一条信息。

代码如下:

#!/usr/bin/perl -w
# a daemon simple
use strict;
 
# become daemon
my $pid = fork();
print $pid,"\n";
if($pid) {
  #end parent process
  print "#parent process";
  exit(0);
}else {
  print "#child process";
}
 
# set new process group
setpgrp;
 
while(1) {
  sleep(30);
  open ("TEST",">>/tmp/test.log");
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
  $year+=1900;$mon++;
  print TEST ("Now is $year-$mon-$mday $hour:$min:$sec.\n");
  close (TEST);
}
Currently unrated

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required