易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
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);
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图