【问题标题】:Crontab not giving resultscrontab 没有给出结果
【发布时间】:2018-11-30 11:59:10
【问题描述】:

我不在根里, 我在 crontab 中输入了以下命令:

*/1 * * * * /home/ajain/testscript.sh

文件 testscript.sh 有以下命令:

#!/bin/bash
echo "The script begins now"
ping -c 2 live.com
echo The script has been run on `date` >> /home/ajain/testscript.log
echo "The script ends now"
exit

crontab 没有给出结果,但是,以下命令在 testscript.log 文件中正确给出了结果,显示了 ping 日期。

bash testscript.sh

为什么 crontab 不工作?

【问题讨论】:

  • chmod 755 /home/ajain/testscript.sh 已经给出。
  • 您没有收到来自 cron 的任何电子邮件吗?
  • 你应该在 /home/ajain/testscript.log 中得到“脚本已在 date 上运行”。
  • @WanmingZhang ,如果脚本是手动运行的,我会得到这个结果,但不是通过 crontab。
  • @TobySpeight 如何检查来自 cron 的电子邮件?我必须以 root 身份登录吗?

标签: linux bash cron sh


【解决方案1】:

您可以通过两种不同的方式修复它。

  1. 提供脚本 /home/ajain/testscript.sh 的完整路径。在这里您甚至不需要添加bash,因为您已经清楚地提到了您的脚本应该在哪个shell 中运行,即脚本的第一行#!/bin/bash

  2. 在执行脚本之前添加这一行

     set path=$path:/home/ajain/
    
     testscript.sh # no need to use bash in front of it
    

还为脚本提供执行权限是不够的。您需要检查将要执行脚本的用户是否有权访问脚本的位置。这意味着用户是否可以使用cd /home/ajain/

希望这会对你有所帮助。

【讨论】:

  • 您的回答没有说​​明 crontab 无法正常工作的原因。您提供的两种解决方案都适用于手动执行脚本,但 crontab 尚未工作。请多多指教。
  • 我已经明确提到您的 cron-tab 用户是否有权定位 /home/ajain/ ?文件权限不够。您的 cron 选项卡帐户必须添加到文件系统 /home/ajain/ 的组所有者中。只需执行touch /home/ajain/testfile.txt 并查看文件是否已创建。同时在home/ajain/ 中执行ls -ltr 并检查文件系统的组和所有者,并将您的crontab 用户添加到该组。
  • 我可以访问 /home/ajain 并执行 touch /home/ajain/testfile.txt。
  • 只需从您的 crontab 执行以下命令并检查是否工作。 echo "Hello World!" > /tmp/helloworld.txt
  • 终端在执行此代码时出现错误提示“/temp/crontab.RqQQSt”:2 Bad Minute。
【解决方案2】:

删除>> /home/ajain/script.log,只需在文件顶部添加一行/home/ajain/testscript.sh

#!/bin/bash

exec >> /home/ajain/script.log 2>&1

echo "The script begins now"
ping -c 2 live.com
echo "The script has been run on `date`"
echo "The script ends now"
exit

从 crontab 中删除 >> /home/ajain/script.log,只需使用:

*/1 * * * * /home/ajain/testscript.sh

【讨论】:

    【解决方案3】:

    您需要定义脚本输出。

    由于 cron 手册页:

    执行命令时,任何输出都会邮寄给 crontab(或在 MAILTO 环境变量中命名的用户) crontab(如果存在)。 cron 运行的子副本 这些进程的名称被强制为大写,如下所示 在 syslog 和 ps 输出中。

    除了echo The script has been run on 'date' >> /home/ajain/testscript.log 之外,您应该检查您/root 的邮件或系统日志(例如/var/log/syslog)。

    我可以推荐这样的东西:

    */1 * * * * /home/ajain/testscript.sh >> /home/ajain/script.log
    

    @编辑

    我的用户的 crontab 文件:

    $crontab -l
    # test
    */1 * * * * /home/3sky/tests/test.sh >> /home/3sky/tests/log.log
    

    脚本如下所示:

    #!/bin/bash
    echo "The script begins now"
    ping -c 2 live.com
    echo The script has been run on `date`
    echo "The script ends now"
    exit
    

    文件权限:

    test.sh - 0755/-rwxr-xr-x
    log.log - 0644/-rw-r--r--
    

    日志文件中的输出:

    $tail -20 log.log
    2 packets transmitted, 0 received, 100% packet loss, time 10999ms
    
    The script has been run on Thu Jun 21 12:18:12 CEST 2018
    The script ends now
    The script begins now
    PING live.com (204.79.197.212) 56(84) bytes of data.
    
    --- live.com ping statistics ---
    2 packets transmitted, 0 received, 100% packet loss, time 10999ms
    
    The script has been run on Thu Jun 21 12:19:12 CEST 2018
    The script ends now
    The script begins now
    PING live.com (204.79.197.212) 56(84) bytes of data.
    
    --- live.com ping statistics ---
    2 packets transmitted, 0 received, 100% packet loss, time 10999ms
    
    The script has been run on Thu Jun 21 12:20:12 CEST 2018
    The script ends now
    

    【讨论】:

    • */1 * * * * /home/ajain/testscript.sh >> /home/ajain/script.log,此代码尚未在日志文件中给出结果。
    • 编辑后的回复是手动给出结果,但不是通过 crontab
    • 此外,根目录中的错误电子邮件表明 testscript.sh 不是目录,请求错误。
    • 我不知道你的问题是什么。我在这里通过了完整的“工作流程”,获得了许可、脚本的完整内容、来自 crontab 的列表和tail 日志文件。一切都在 crontab 中运行。
    • 正确的答案是在 crontab 中提及以下命令: */1 * * * * bash /home/ajain/testscript.sh 缺少 bash 这个词,因此脚本没有被执行
    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2023-03-16
    相关资源
    最近更新 更多