Cron 无法运行 bash 脚本
我有 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
}
I have bash script
#!/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
and it works when called from command line, but fails when cron calls it. Specifically the line with telnet command gives zero bytes of output. Telnet has 755 mask, so every user should be able to use it. Any ideas ?
Update, contents of 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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜有些路径丢失了...您是否尝试使用 /usr/bin/telnet 而不是 telnet ?
要查找 telnet 的路径,您可以使用
which telnet
。I'm guessing that some paths are missing... Did you try to use /usr/bin/telnet instead of telnet?
To find the path of telnet you can use
which telnet
.好的。疯狂猜测时间...
您使用的是
.rhosts
文件吗?这样,当您远程登录时,就不必输入密码。您不能在 crontab 脚本中执行此操作。如果这是原因,您必须做三件事:
Okay. Wild guess time...
Are you using a
.rhosts
file? That way, when you telnet, you don't have to enter a password. You can't do that in a crontab script.If that's the cause, three things you have to do:
ssh-keygen
program, and generate a public and private key. Do the same for the remote machine. Now, create an authorized_hosts file on the remote machine and add in your public key.您应该提及您收到的具体错误消息。不管怎样,既然你说 telnet 线路会导致错误,我假设存在以下常见陷阱:
/bin/bash
。看看/bin/sh
指向什么,例如使用ls -l /bin/sh
/bin/bash
作为默认 shell(可能是在/etc/passwd
中为您的帐户设置的,或者通过变量$SHELL
设置的。解决方案:
SHELL=/bin/bash
#!/bin/bash
(我的推荐)You should mention the specific error messages you get. Anyway, since you say that the telnet line causes an error I assume the following common pitfall:
/bin/bash
. Look at what/bin/sh
is pointing at, e.g. withls -l /bin/sh
/bin/bash
as default shell (probably this is set for your account in/etc/passwd
or by means of the variable$SHELL
.Solutions:
SHELL=/bin/bash
in the crontab.#!/bin/bash
(my recommendation)