我正在创建的可怕结构是:
my $startTime = `date +%s`; my $startTime2 = $startTime + 10; chomp($startTime2); print "startTime2: " . "$startTime2"; my $pwd = getcwd(); #my $PWD=system("pwd"); print "Current Working Directory: " . "$pwd\n"; my $logdir = "$pwd" . "/load-logs"; print "Log Directory: " . "$logdir\n"; #chmod( 755, $logdir ) or die "Couldn't chmod $logdir: $!"; mkdir("$logdir/$startTime2", 777) || print $!; #print "start Time in epoc : "."$startTime"; #mkdir("$startTime3", 777) || print $!; #system("mkdir \$logdir/$startTime"); my $reqlogfile = "$logdir/$startTime2/req.log"; print "reqlogfile: " . "$reqlogfile\n"; my $resplogfile = "$logdir/$startTime2/resp.log"; print "resplogfile: " . "$resplogfile\n";
现在,mkdir(“$ logdir / $ startTime2”,777)||打印$!; 不会创建$ logdir
下当前值为$ startTime2的目录答案 0 :(得分:1)
问题可能是您的$logdir
未创建,因此最好确保创建它。
此外,最好确保umask
已重置,以便{j} chmod
0777
时,父shell的umask
不会干扰umask
1}}在脚本中。
Perl的mkdir
不能作为shell mkdir -p
(如果它们不存在则创建中间目录)。可以使用File :: Path中的mkpath
,或者只是确保创建中间路径,如下所示:
use strict;
use warnings;
use Cwd;
my $start_time = time; # equivalent to `date +%s`, less a shell out ;)
my $start_time_2 = $start_time + 10;
umask 000; # ensure the permissions you set are the ones you get
my $pwd = getcwd();
print "Current Working Directory: $pwd\n";
my $logdir = "$pwd/load-logs";
print "Log Directory: $logdir\n";
mkdir $logdir
or die "Cannot mkdir $logdir: $!"
unless -d $logdir ;
chmod 0755, $logdir or die "Cannot chmod 0755 $logdir: $!";
mkdir("$logdir/$start_time_2", 0777)
or die "Cannot mkdir $logdir/$start_time_2: $!"
unless -d "$logdir/$start_time_2";
my $reqlogfile = "$logdir/$start_time_2/req.log";
print "reqlogfile: $reqlogfile\n";
my $resplogfile = "$logdir/$start_time_2/resp.log";
print "resplogfile: $resplogfile\n";
以上(严格,警告等)对我有用:
~/t$ perl t.pl
Current Working Directory: /home/mf/t
Log Directory: /home/mf/t/load-logs
reqlogfile: /home/mf/t/load-logs/1293636556/req.log
resplogfile: /home/mf/t/load-logs/1293636556/resp.log
多次启动它不会死,并根据需要创建目录(并chmods)。您可以通过ls -la load-logs/
检查该内容。