gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:57:53 25 4
gpt4 key购买 nike

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

此脚本用于监视网页的更改,如果它检测到必须发送电子邮件的任何图形/视觉差异。

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

#!/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

最佳答案

这是在 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';

关于linux - 我有一个从命令行完美运行但拒绝作为 cron 运行的 linux 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27707149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com