![](/img/trans.png)
[英]one particular command in bash script doesn't work when run by cron, all other commands do
[英]Cron doesn't run bash script
我有克朗
外壳= / bin / bash
路径= / 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
重击默认
#echo $ SHELL
/ bin / bash
为什么cron不运行1.sh?
删除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上)进行了测试:
contab行:
* * * * * /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上更改了regEx。 您的lynx输出可能不同(其他版本的lynx)。 我使用了固定的测试URL(天猫手册页)。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.