Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在cron中运行bash脚本_Linux_Bash_Ubuntu_Cron - Fatal编程技术网

Linux 在cron中运行bash脚本

Linux 在cron中运行bash脚本,linux,bash,ubuntu,cron,Linux,Bash,Ubuntu,Cron,我有以下bash脚本: !/bin/bash # script to send simple email #Wait 5 seconds #sleep 05 # email subject SUBJECT="SUBJECT" # Email To ? EMAIL="EMAIL" # Email text/message MYIP=$(curl http://ipecho.net/plain) LASTIP=$(head -n 1 /home/ubuntu/myip.txt) echo "Las

我有以下bash脚本:

!/bin/bash
# script to send simple email 
#Wait 5 seconds
#sleep 05
# email subject
SUBJECT="SUBJECT"
# Email To ?
EMAIL="EMAIL"
# Email text/message
MYIP=$(curl http://ipecho.net/plain)
LASTIP=$(head -n 1 /home/ubuntu/myip.txt)
echo "Last IP:" $LASTIP
echo "Current IP:" $MYIP
#Check if IPs are the same
if [[ "$MYIP" == "$LASTIP" ]]; then
        echo "IP hasn't changed. Do nothing."
else
        sendemail -f $EMAIL -t $EMAIL -u $SUBJECT -m $MYIP -s smtp.gmail.com -o tls=yes -xu username -xp password
        echo $MYIP > myip.txt
fi
当我尝试在命令行中运行它时,它工作得非常好。当我将它包含在“crontab-e”中时,问题就开始了,比如:“***/home/ubuntu/myip.sh”

那么它就不起作用了。似乎sendmail功能不正常。 当我执行以下操作时:tail-f/var/log/syslog

Sep 18 21:48:02 gpuserver sendmail[18665]: r8J1m1gO018665: from=ubuntu, size=314, class=0, nrcpts=1, msgid=<201309190148.r8J1m1gO018665@gpuserver>, relay=ubuntu@localhost
Sep 18 21:48:02 gpuserver sendmail[18665]: r8J1m1gO018665: to=ubuntu, ctladdr=ubuntu (1000/1000), delay=00:00:01, xdelay=00:00:00, mailer=relay, pri=30314, relay=[127.0.0.1] [127.0.0.1], dsn=4.0.0, stat=Deferred: Connection refused by [127.0.0.1]
Sep 18 21:48:02 gpuserver sendmail[18665]:r8J1m1gO018665:from=ubuntu,size=314,class=0,nrcpts=1,msgid=,中继=ubuntu@localhost
9月18日21:48:02 gpuserver sendmail[18665]:r8J1m1gO018665:to=ubuntu,ctladdr=ubuntu(1000/1000),delay=00:00:01,xdelay=00:00:00,mailer=relay,pri=30314,relay=[127.0.0.1][127.0.0.1],dsn=4.0.0,stat=Deferred:连接被[127.0.0.0.1]拒绝

有什么想法吗?

从命令行运行脚本时,您正在利用当前环境。克朗没有那个信息。。。因此,您应该从命令行执行以下操作:

env > me

然后编辑脚本,并在脚本开头包含./me。这样,您的cron任务将在相同的环境下运行。

当cron将某些内容打印到stdout或stderr时,cron本身会尝试通过电子邮件发送报告。由于您可能没有配置系统范围的电子邮件,这可能就是syslog中错误消息的来源

为了防止cron发送任何邮件,请确保您的脚本及其生成的任何命令没有向stdout或stderr输出任何内容。一种简单的方法是在cron行中添加“&>/dev/null”

您还可以在crontab的开头添加“MAILTO=”“”


所有这些都记录在cron/crontab(man-k cron)的手册页中。

您使用的sendmail是什么?sendmail中的
e
是真实脚本中的输入错误,还是这里的复制错误?cron是否配置为发送cron作业的输出?您在
配置文件中设置了任何环境变量,
.bash\u配置文件
.bashrc
会影响
发送邮件
?Cron作业不运行您的配置文件。sendemail中的e是正确的!(通过apt get install sendemail安装)我想你的意思是
/我
。如果不使用
命令,脚本将在子shell中运行,并且不会在脚本中设置变量。此外,您还需要向所有分配添加
export
命令。这还有一个潜在问题:
env>me
如果值包含空格,则不引用这些值。不起作用。将./me添加到文件的开头,并运行env>me,仍然显示相同的问题。我感觉它与root的sendmail配置文件有关。尝试了几种解决方案但没有任何效果(特别是因为sendmail,它只是sendmail的一个脚本)我想你误解了-你需要做的是首先运行env命令,在登录时获取shell环境变量当前设置的副本并保存到文件中。/me然后使用vim或你最喜欢的文本编辑器将me的内容读入你的cron脚本中,这样所有的环境变量都会在你之前在脚本中设置好尝试运行sendmail