Perl学习之创建Linux进程
Perl学习之创建Linux进程
示例1:创建进程
- #!/usr/bin/perl
- #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目录下的文件
- #!/usr/bin/perl
- 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);
- }
- }
评论暂时关闭