Cron 无法运行 bash 脚本

发布于 2024-11-02 16:13:17 字数 1283 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗弃M 2024-11-09 16:13:17

我猜有些路径丢失了...您是否尝试使用 /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.

烟─花易冷 2024-11-09 16:13:17

好的。疯狂猜测时间...

您使用的是 .rhosts 文件吗?这样,当您远程登录时,就不必输入密码。您不能在 crontab 脚本中执行此操作。

如果这是原因,您必须做三件事:

  • 找出 crontab 正在哪个用户下运行。
  • 对于该用户,运行 ssh-keygen 程序,并生成公钥和私钥。对远程计算机执行相同的操作。现在,在远程计算机上创建一个authorized_hosts 文件并添加您的公钥。
  • 完成此操作后,从 telnet 切换到 ssh。无论如何,SSH 更安全。

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:

  • Find out what user that crontab is running under.
  • For that user, run the 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.
  • Once this is done, switch from telnet to ssh. SSH is more secure anyway.
蒲公英的约定 2024-11-09 16:13:17

您应该提及您收到的具体错误消息。不管怎样,既然你说 telnet 线路会导致错误,我假设存在以下常见陷阱:

  • 你的脚本需要 Bash shell 才能正确运行。
  • 您系统的默认 shell 不是 /bin/bash。看看 /bin/sh 指向什么,例如使用 ls -l /bin/sh
  • 但是,您的帐户设置为具有 /bin/bash 作为默认 shell(可能是在 /etc/passwd 中为您的帐户设置的,或者通过变量 $SHELL 设置的。

解决方案:

  • 更改默认 shell你的系统(取决于你的系统,而不是我推荐的)。
  • 让你的 cronjobs 使用 Bash 作为默认值:在 crontab 中设置 SHELL=/bin/bash
  • 更改脚本的第一行以提及 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:

  • Your script requires the Bash shell to run properly.
  • The default shell of your system is not /bin/bash. Look at what /bin/sh is pointing at, e.g. with ls -l /bin/sh
  • Your account, though, is set to have /bin/bash as default shell (probably this is set for your account in /etc/passwd or by means of the variable $SHELL.

Solutions:

  • Change the default shell of your system (depends on your system, not what I recommend).
  • Make your cronjobs use Bash as default: Set SHELL=/bin/bash in the crontab.
  • Change the 1st line of your script to mention Bash explicitly: #!/bin/bash (my recommendation)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文