我有一个linux脚本,从命令行运行完美,但拒绝作为一个cron工作

时间:2014-12-30 14:25:13

标签: linux bash perl cron

此脚本在登录时运行,但我无法让它在cron作业下工作,有什么想法吗?

如果检测到发送电子邮件时出现的图形/视觉差异,此脚本用于监控wep页面的更改。

我已经阅读了有关此类问题的其他帖子,并试图实施一些建议,但我仍然会收到错误,即使有更改,我也没有收到电子邮件。 (仅供参考:出于保密目的,电子邮件已被更改)

#!/usr/local/cpanel/3rdparty/bin/perl
`SHELL=/bin/bash`;
`PATH=/usr/local/jdk/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin`;
`cd /root/pagechange`;
`rm -f null`;
`wkhtmltoimage --no-images --height 3000 --javascript-delay 7500 http://www.google.com /root/pagechange/sys.jpg`;
`$dif=/usr/bin/compare -metric AE /root/pagechange/sys.jpg /root/pagechange/sys1.jpg null: 2>&1`;
print "1";
if ( $dif == 0 ) {
  print "They're equal\n";
} else {
   $to = 'me@domain.com';
   $from = 'you@domain.com';
   $subject = 'Page changes detected ';
   $message = "Get to work";
   print "2";
   open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
   print MAIL "To: $to\n";
   print MAIL "From: $from\n";
   print MAIL "Subject: $subject\n\n";
# Email Body
   print MAIL $message;
print "3";
   close(MAIL);
   print "Email Sent Successfully\n";
}

print "4";
`cp /root/pagechange/sys.jpg /root/pagechange/sys1.jpg`;
print "5";
#`rm /root/pagechange/index.html`;
exit

2 个答案:

答案 0 :(得分:4)

这是Perl中的样子。我只修改了最明显的问题:

#!/usr/bin/perl
use strict;
use warnings;

# Set the environment with the %ENV hash
# environment variables set in a subshell will not persist
$ENV{SHELL} = '/bin/bash'; # although you don't need this
$ENV{PATH}  = ...;

# change directory
chdir '/root/pagechange' or die "Could not change directory: $!";

# remove a file with unlink
unlink 'null';

# list argument form of system
# this prevents arguments from being treated as special by the shell
# use full path to executable so you know which one you use
system '/path/to/wkhtmltoimage', qw(
    --no-images --height 3000 --javascript-delay 7500 
    http://www.google.com /root/pagechange/sys.jpg
    );

# save the result of the backticks to get the program output
my $dif = `/usr/bin/compare -metric AE /root/pagechange/sys.jpg /root/pagechange/sys1.jpg null: 2>&1`;

print "1";
if ( $dif == 0 ) {
    print "They're equal\n";
    } 
else {
    my $to      = 'me@domain.com';
    my $from    = 'you@domain.com';
    my $subject = 'Page changes detected';
    my $message = "Get to work";

    print "2";
    open(MAIL, "|/usr/sbin/sendmail -t");
    # Email Header
    print MAIL "To: $to\n";
    print MAIL "From: $from\n";
    print MAIL "Subject: $subject\n\n";
    # Email Body
    print MAIL $message;

    print "3";
    if( close(MAIL) ){
        print "Email Sent Successfully\n";
        }
    else { # close puts the error in $? instead of $! (until 5.22!)
        my $error = $? >> 8; 
        print "Problem sending mail: $error";
        }
    }

print "4";
# list argument form of system, again
system '/bin/cp', qw(/root/pagechange/sys.jpg /root/pagechange/sys1.jpg);

print "5";
# unlink '/root/pagechange/index.html';

答案 1 :(得分:0)

除了其他任何事情,它必须是:

$dif=`/usr/bin/compare -metric AE /root/pagechange/sys.jpg /root/pagechange/sys1.jpg null: 2>&1`;

您不在代码中的脚本中将任何内容分配给$ dif作为变量。无论你在shell中做什么,它都会留在shell中。

编辑:仅此一项肯定无法解决您的问题,请查看其他评论