perl实例之创建linux进程
本节内容:
在perl中创建linux进程的方法。
例1:创建进程
#a simple demon
use strict;
my $pid = fork();
print $pid."\n";
if($pid)
{
print "processing...\n";
exit(0);
}
else
{
print "child processing...\n";
}
setpgrp();
while(1)
{
sleep(10);
open(MYFILE,">>/tmp/test.log");
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$year+=1900;$mon++;
print MYFILE ("Now is $year-$mon-$mday $hour:$min:$sec.\n");
close(MYFILE);
}
例2:定时删除/tmp目录下的文件
use strict;
our $DIR_PATH="/tmp";
&main();
exit;
sub main
{
die( "Can't fork") unless defined (my $pid = fork());
print "Process...\n$pid\n";
setpgrp();
while(1)
{
opendir DIR, ${DIR_PATH} or die "Can not open \"$DIR_PATH\"\n";
my @filelist = readdir DIR;
my @res;
my $a;
my $file;
open(MYFILE,">>/tmp/delfile.log");
foreach $file (@filelist)
{
($file eq "..") and next;
($file eq ".") and next;
@res = stat($DIR_PATH."/".$file);
$a = time() - @res[10];
if ($a > 258200 )
{
system("rm -rf ".$DIR_PATH."/".$file);
print MYFILE $DIR_PATH."/".$file."\n";
}
}
close(MYFILE);
sleep(86400);
}
}
您可能感兴趣的文章:
perl实现linux进程间的通信 perl FIFO的例子
perl实例之子程序
Perl 简介
perl实例之创建linux进程
perl实例之http请求的小例子
Perl 教程
SQLite – Perl
Perl 包和模块
Linux ed命令
《Perl编程24学时教程》笔记第17课 perl的CGI概述