Perl编写Daemon进程

  | 转载时请务必以超链接形式标明文章原文链接和作者信息及本版权声明。
原文链接:http://www.liaojl.com/archives/2008/06/perl-daemon.html

编写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);
}

Leave a comment

Archives

Creative Commons License
This blog is licensed under a Creative Commons License.