perl脚本在命令行上运行但不在crontab中运行

时间:2015-03-11 01:54:07

标签: linux perl scripting cron rrdtool

非常奇怪,我有很多脚本如下所示,所有脚本都在crontab中运行,但是后面的脚本在命令(./load.pl)行上运行但不在crontab内部运行

的crontab:

0-59/5 * * * * /home/spatel/rrd/load.pl >> /tmp/load.out

注意: 我也试过以下方法

0-59/5 * * * * /usr/bin/perl /home/spatel/rrd/load.pl >> /tmp/load.out
0-59/5 * * * * root /usr/bin/perl /home/spatel/rrd/load.pl >> /tmp/load.out

某处我读过cron忽略了脚本的换行结束,所以我也很关心

我已将print放入脚本并重定向到/tmp/load.out我可以在cron执行时看到load.out中的输出,但不知何故它不会更新load.rrd个文件中的数据。

如果我在像./load.pl这样的命令上运行脚本就行了!但不是在crontab里面。

无论PATH使用了什么,我都设置了crontab root。我尝试了所有可能的调试方法,但它没有在cron中运行。这里是我获取此脚本的地方,crontab中的所有其他脚本工作文件只有一个有问题:(https://github.com/mmitch/rrd

脚本:

#!/usr/bin/env perl
#
# RRD script to display system load
# 2003,2011 (c) by Christian Garbs <mitch@cgarbs.de>
# Licensed under GNU GPL.
#
# This script should be run every 5 minutes.
#
# *ADDITIONALLY* data aquisition is done externally every minute:
# rrdtool update $datafile N:$( PROCS=`echo /proc/[0-9]*|wc -w|tr -d ' '`; read L1 L2 L3 DUMMY < /proc/loadavg ; echo ${L1}:${L2}:${L3}:${PROCS} )
#
use strict;
use warnings;
#use 5.010;
use RRDs;

# parse configuration file
my %conf;
eval(`/bin/cat /home/spatel/rrd/rrd-conf.pl`);
die $@ if $@;

# set variables
my $datafile = "/home/spatel/rrd/db/load.rrd";
my $picbase  = "/var/www/mrtg/rrd/load-";

# global error variable
my $ERR;

# whoami?
my $hostname = `/bin/hostname`;
chomp $hostname;

# generate database if absent
if ( ! -e $datafile ) {
    # max 70000 for all values
    RRDs::create($datafile,
         "--step=60",
         "DS:load1:GAUGE:120:0:70000",
         "DS:load2:GAUGE:120:0:70000",
         "DS:load3:GAUGE:120:0:70000",
         "DS:procs:GAUGE:120:0:70000",
             "RRA:AVERAGE:0.5:1:120",
         "RRA:AVERAGE:0.5:5:600",
         "RRA:AVERAGE:0.5:30:700",
         "RRA:AVERAGE:0.5:120:775",
         "RRA:AVERAGE:0.5:1440:797",
         "RRA:MAX:0.5:1:120",
         "RRA:MAX:0.5:5:600",
         "RRA:MAX:0.5:6:700",
         "RRA:MAX:0.5:120:775",
         "RRA:MAX:0.5:1440:797",
         "RRA:MIN:0.5:1:120",
         "RRA:MIN:0.5:5:600",
         "RRA:MIN:0.5:6:700",
         "RRA:MIN:0.5:120:775",
         "RRA:MIN:0.5:1440:797"
         );
      $ERR=RRDs::error;
      die "ERROR while creating $datafile: $ERR\n" if $ERR;
      print "created $datafile\n";
  }

# data aquisition is done externally every minute:
my @procs = glob '/proc/[0-9]*';

my $file = '/proc/loadavg';
open my $fh, '<', $file or die "Failed to open '$file': $!";

my $load = <$fh>;

my $p = (scalar @procs);
my $l = (join ':', (split ' ', $load)[0..2]);
print "${l}:${p}";

# update rrd
RRDs::update($datafile,
            "N:${l}:${p}"
            );
$ERR=RRDs::error;
die "ERROR while updating $datafile: $ERR\n" if $ERR;

# draw pictures
foreach ( [3600, "hour"], [86400, "day"], [604800, "week"], [31536000, "year"] ) {
    my ($time, $scale) = @{$_};
    RRDs::graph($picbase . $scale . ".png",
        "--start=-${time}",
        '--lazy',
        '--imgformat=PNG',
        "--title=${hostname} system load (last $scale)",
        "--width=$conf{GRAPH_WIDTH}",
        "--height=$conf{GRAPH_HEIGHT}",
        '--slope-mode',
        '--alt-autoscale',

        "DEF:load1=${datafile}:load1:AVERAGE",
        "DEF:load2=${datafile}:load2:AVERAGE",
        "DEF:load3=${datafile}:load3:AVERAGE",
        "DEF:procsx=${datafile}:procs:AVERAGE",
        "DEF:procminx=${datafile}:procs:MIN",
        "DEF:procmaxx=${datafile}:procs:MAX",

        'CDEF:procs=procsx,100,/',
        'CDEF:procmin=procminx,100,/',
        'CDEF:procrange=procmaxx,procminx,-,100,/',

        'AREA:procmin',
        'STACK:procrange#E0E0E0',
        'AREA:load3#000099:loadavg3',
        'LINE2:load2#0000FF:loadavg2',
        'LINE1:load1#9999FF:loadavg1',
        'COMMENT:\n',
        'LINE1:procs#000000:processes/100',
        );
    $ERR=RRDs::error;
    die "ERROR while drawing $datafile $time: $ERR\n" if $ERR;
}

更新

以下是脚本的输出:

[root@spatel tmp]# pwd
/tmp
[root@spatel tmp]# /home/spatel/rrd/load.pl 
0.15:0.06:0.01:664

2 个答案:

答案 0 :(得分:0)

最有可能是环境问题。

“cron”脚本使用默认系统环境(默认路径,LANG等)启动.profile,.rc不会执行。

因此,您需要在脚本中提供程序所需的所有环境变量PATH等。这对纯粹的perl脚本来说有点痛苦,所以最好将它包装在一个shell脚本中,该脚本设置你的“.profile”脚本设置的内容。

答案 1 :(得分:0)

解决:

您不会相信问题是如何解决的,我在perl中使用/usr/bin/perl代替crontab,我尽管使用完整PATH的最佳做法是crontab,但它证明是错的,我还不知道为什么和如何?

0-59/5 * * * * perl /home/spatel/rrd/load.pl