首页 / 操作系统 / Linux / Perl学习之创建Linux进程
示例1:创建进程
- #!/usr/bin/perl
- #a simple demon
- use strict;
-
- my $pid = fork();
- print $pid."
";
- if($pid)
- {
- print "processing...
";
- exit(0);
- }
- else
- {
- print "child processing...
";
- }
- 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.
");
- 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...
$pid
";
- setpgrp();
- while(1)
- {
- opendir DIR, ${DIR_PATH} or die "Can not open "$DIR_PATH"
";
- 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."
";
- }
- }
-
- close(MYFILE);
- sleep(86400);
- }
- }