我有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
}
答案 0 :(得分:1)
我猜测有些路径丢失了......您是否尝试使用/ usr / bin / telnet而不是telnet?
要查找telnet的路径,您可以使用which telnet
。
答案 1 :(得分:1)
好。疯狂的猜测时间......
您使用的是.rhosts
文件吗?这样,当你telnet时,你不必输入密码。你不能在crontab脚本中这样做。
如果这是原因,你需要做三件事:
ssh-keygen
程序,并生成公钥和私钥。对远程机器执行相同操作。现在,在远程计算机上创建authorized_hosts文件并添加公钥。答案 2 :(得分:0)
您应该提及您获得的特定错误消息。无论如何,既然你说telnet行会导致错误,我会假设以下常见的陷阱:
/bin/bash
。查看/bin/sh
指向的内容,例如ls -l /bin/sh
/bin/bash
作为默认shell(可能会在/etc/passwd
中为您的帐户设置,或者通过变量$SHELL
设置。解决方案:
SHELL=/bin/bash
。#!/bin/bash
(我的推荐)