开发者

how do i create a directory in perl which includes current timestamp as EPOCH

The 开发者_运维百科direcrory structure that i am creating is thus:

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";

now, mkdir("$logdir/$startTime2", 777) || print $!; does not create a directory which has the current value of $startTime2 under $logdir


A problem may be that your $logdir wasn't created, so it's best to ensure it is created.

Also, it's best to make sure the umask is reset so that if you chmod to 0777, the parent shell's umask doesn't interfere with the umask in the script.

Perl's mkdir doesn't work as the shell's mkdir -p (which creates intermediate directories if they do not exist). One could use mkpath from File::Path, or simply ensure the intermediate path is created, as I show below:

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";

The above (with strict, warnings, etc) works for me:

~/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

Launching it multiple times doesn't die, and creates the directories (and chmods them) if needed. You can check that via ls -la load-logs/.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜