【问题标题】:Cron doesn't run bash scriptCron 不运行 bash 脚本
【发布时间】:2012-10-22 00:15:10
【问题描述】:

我有定时任务

SHELL=/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

* * * * * root  /usr/bin/flock -xn /var/lock/script.lock -c '/bin/bash /root/Dropbox/1.sh'

我的 1.sh

#!/bin/bash -l 

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 

x=1
while [ $x -le 3 ] 
do 
URL=$(head -n 1 /root/Dropbox/query.txt) 
lynx -dump  $URL | egrep '^http' > /root/Dropbox/urls.txt 
x=$(( $x + 1 )) 
done

默认为 Bash

# echo $SHELL

/bin/bash

为什么 cron 不运行 1.sh ?

【问题讨论】:

    标签: linux bash


    【解决方案1】:

    删除 crontab 行中的“root”:

    * * * * * /usr/bin/flock -xn /var/lock/script.lock -c '/bin/bash /root/Dropbox/1.sh'
    

    如果您需要 root 权限,请在 root 用户的 crontab 中。
    使用“root”,您还会在 syslog 或消息日志中发现错误。

    当然要确保 cron 守护进程正在运行:ps -ef | grep cron

    添加: 我已经通过简单的触摸文件(在 ubuntu 上)对其进行了测试:
    连线:

    * * * * * /usr/bin/flock -xn /var/lock/script.lock -c '/bin/bash ~/1.sh'
    

    1.sh:

    #!/bin/bash
    touch /tmp/hallo
    

    添加:(查看 lynx 命令) 它适用于我的 1.sh 脚本版本。

    #!/bin/bash
    
    x=1
    while [ $x -le 3 ]
    do
    URL="http://www.subir.com/lynx.html"
    lynx -dump  $URL | egrep '.*\. http' > urls.txt
    x=$(( $x + 1 ))
    done
    

    我更改了 egrep 上的正则表达式。您的 lynx 输出可能不同(其他版本的 lynx)。我使用了一个固定的测试 URL(lynx 手册页)。 urls.txt 将被填充。脚本由 cron 触发。 while 将没有效果,注意循环逻辑将在下次运行时更改。

    stephan@coppi:~$ more urls.txt 
       1. http://lynx.isc.org/current/index.html
       2. http://lynx.isc.org/current/lynx2-8-8/lynx_help/lynx_help_main.html
       3. http://lynx.isc.org/current/lynx2-8-8/lynx_help/Lynx_users_guide.html
       4. http://lynx.isc.org/lynx2.8.7/index.html
       5. http://lynx.isc.org/lynx2.8.7/lynx2-8-7/lynx_help/lynx_help_main.html
       6. http://lynx.isc.org/lynx2.8.7/lynx2-8-7/lynx_help/Lynx_users_guide.html
       7. http://lynx.isc.org/mirrors.html
       8. http://lists.nongnu.org/mailman/listinfo/lynx-dev/
       9. http://lynx.isc.org/signatures.html
      10. http://validator.w3.org/check?uri=http%3A%2F%2Flynx.isc.org%2Findex.html
    

    【讨论】:

    • cron 守护进程正在运行。我输入了 1.sh cat、grep 或 wget - cron run 1.sh 我认为变量 $URL 或 $x 有问题,可能是 cron 没有正确理解它。我在没有 root 的情况下在 cron 中进行了测试 - 没有运行。
    • 如果 cron 已经启动了 1.sh 脚本,那么 cron 的工作就完成了。 1.sh中的while循环一般也可以。不确定 URL 和 lynx 行。你可以通过 cron 运行另一个脚本(简单)吗?并且您可以手动运行 1.sh 脚本而不会出错吗?
    • 是的,我在控制台中手动运行脚本没有问题。没有 lynx 也没有 $URL,$x - 它在 cron 中工作
    • 听起来很疯狂。如果你愿意的话,你能否发送一些你的 query.txt 的行 (2-4),而不是我可以调试它。
    猜你喜欢
    • 2017-05-28
    • 2011-08-10
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 2017-09-03
    • 2010-09-14
    相关资源
    最近更新 更多