2
我bash脚本的Cron无法运行bash脚本
#!/bin/sh
DTFILE=/etc/daytime.addr
DTPORT=13
DAYTIME_ERROR=/tmp/dtm.err
function daytime_error(){
if [[ -z $1 ]]
then
exit 1
fi
if [[ -e $DAYTIME_ERROR ]]
then
echo "Error already reported"
else
logger "$1"
touch $DAYTIME_ERROR
fi
exit 1
}
if [[ -s $DTFILE ]]
then
ADDR=$(head -n1 $DTFILE)
DAYTIME=$(telnet $ADDR $DTPORT | time_conv.awk)
if [[ -z $DAYTIME ]]
then
daytime_error "Daytime client: no connection to $ADDR"
else
date -s "$DAYTIME"
hwclock -w
rm $DAYTIME_ERROR
fi
else
daytime_error "Daytime client: no daytime server address in file $DTFILE"
fi
和它的作品从命令行调用时,但当cron的调用失败。具体来说,使用telnet命令的行提供零字节的输出。 Telnet有755个掩码,所以每个用户都应该可以使用它。有任何想法吗 ?
更新time_conv.awk的,内容:
#! /usr/bin/awk -f
/[0-9]+:[0-9]+:[0-9]+/ {
if ($2~/Jan/) $2=1;
else if ($2~/Feb/) $2=2;
else if ($2~/Mar/) $2=3;
else if ($2~/Apr/) $2=4;
else if ($2~/May/) $2=5;
else if ($2~/Jun/) $2=6;
else if ($2~/Jul/) $2=7;
else if ($2~/Aug/) $2=8;
else if ($2~/Sep/) $2=9;
else if ($2~/Oct/) $2=10;
else if ($2~/Nov/) $2=11;
else if ($2~/Dec/) $2=12;
print $5 "-" $2 "-" $3 " " $4
}
你如何运行它?是'bash
你应该提到你得到的具体错误信息。无论如何,既然你说telnet行导致错误,我假设以下常见的陷阱:
/bin/bash
。看看/bin/sh
指向什么,例如与ls -l /bin/sh
/bin/bash
作为默认的shell(也许这是在/etc/passwd
为您的帐户或由可变$SHELL
手段解决方案:
SHELL=/bin/bash
#!/bin/bash
(我的建议)来源
2011-06-05 19:19:12 xebeche
好。狂猜的时间...
你使用的是
.rhosts
文件吗?这样,当你telnet时,你不必输入密码。你不能在crontab脚本中做到这一点。如果是这样的原因,你必须做三两件事:
ssh-keygen
程序,并生成公钥和私钥。为远程机器做同样的事情。现在,在远程机器上创建authorized_hosts文件并添加公钥。来源
2011-06-05 20:40:11