bash Shell 脚本未通过 crontab 运行,但手动运行良好
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20582224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell script not running via crontab, but runs fine manually
提问by user3101956
I have a script that checks if pptp vpn is running , and if not it reconnect the pptp vpn , when i run the script manually it execute fine , when i do set a cron job , is not running
我有一个脚本来检查 pptp vpn 是否正在运行,如果没有,它重新连接 pptp vpn,当我手动运行脚本时,它执行正常,当我设置 cron 作业时,它没有运行
* * * * * /bin/bash /var/scripts/vpn-check.sh
here comes the script:
脚本来了:
#!/bin/sh
/bin/ping -c3 192.168.17.27 > /tmp/pingreport
result=`grep "0 received" /tmp/pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$$'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
/usr/sbin/pppd call home
fi
回答by user3101956
finally i found a solution ... instead of entering the cronjob with
最后我找到了一个解决方案......而不是进入cronjob
crontab -e
i needed to edit the crontab file directly
我需要直接编辑 crontab 文件
nano /etc/crontab
adding e.g. something like
添加例如类似的东西
*/5 * * * * root /bin/bash /var/scripts/vpn-check.sh
and its fine now!
现在好了!
Thank you all for your help ... hope my solution will help other people as well.
谢谢大家的帮助......希望我的解决方案也能帮助其他人。
回答by janos
Your script can be corrected and simplified like this:
您的脚本可以像这样更正和简化:
#!/bin/sh
log=/tmp/vpn-check.log
{ date; ping -c3 192.168.17.27; } > $log
if grep -q '0 received' $log; then
/usr/sbin/pppd call home >>$log 2>&1
fi
Through our discussion in comments we confirmed that the script itself works, but pppd
doesn't, when running from cron
. This is because something must be different in an interactive shell like your terminal window, and in cron
. This kind of problem is very common by the way.
通过我们在评论中的讨论,我们确认脚本本身可以pppd
运行,但从cron
. 这是因为在像终端窗口这样的交互式 shell 和cron
. 顺便说一下,这种问题很常见。
The first thing to do is try to remember what configuration is necessary for pppd
. I don't use it so I don't know. Maybe you need to set some environment variables? In which case most probably you set something in a startup file, like .bashrc
, which is usually not used in a non-interactive shell, and would explain why pppd
doesn't work.
要做的第一件事是尝试记住pppd
. 我不使用它所以我不知道。也许您需要设置一些环境变量?在这种情况下,您很可能在启动文件中设置了一些东西,比如.bashrc
,它通常不会在非交互式 shell 中使用,并且会解释为什么pppd
不起作用。
The second thing is to check the logs of pppd
. If you cannot find the logs easily, look into its man
page, and it's configuration files, and try to find the logs, or how to make it log. Based on the logs, you should be able to find what is missing when running in cron
, and resolve your problem.
第二件事是检查pppd
. 如果您无法轻松找到日志,请查看其man
页面和配置文件,并尝试查找日志或如何使其成为日志。根据日志,您应该能够找到运行时缺少的内容cron
,并解决您的问题。
回答by Reinstate Monica Please
If you're positive the script runs outside of cron, execute
如果您确定脚本在 cron 之外运行,请执行
printf "SHELL=$SHELL\nPATH=$PATH\n* * * * * /bin/bash /var/scripts/vpn-check.sh\n"
Do crontab -e for whichever crontab you're using and replace it with output of the above command. This should mirror most of your environment in case there is some missing path issue or something else. Also check logs for any errors it's getting.
对您使用的任何 crontab 执行 crontab -e 并将其替换为上述命令的输出。这应该反映您的大部分环境,以防万一缺少路径问题或其他问题。还要检查日志是否有任何错误。
Though it definitly looks like the script has an error or you messed something up when copying it here
虽然它肯定看起来像脚本有错误或者你在这里复制它时搞砸了
sed: -e expression #1, char 44: unterminated `s' command
./bad.sh: 5: ./bad.sh: [[: not found
Simple alternate script
简单的替代脚本
#!/bin/bash
if [[ $(ping -c3 192.168.17.27) == *"0 received"* ]]; then
/usr/sbin/pppd call home
fi
回答by AndyFaizan
In my case, the issue was that the script wasn't marked as executable. To make sure it is, run the following command:
就我而言,问题在于脚本未标记为可执行文件。要确保它是,请运行以下命令:
chmod +x your_script.sh
chmod +x your_script.sh
回答by Joao Vitor Deon
After a long time getting errors, I just did this:
经过很长时间的错误,我只是这样做了:
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /bin/bash /home/joaovitordeon/Documentos/test.sh
Where test.sh
contains:
其中test.sh
包含:
#!/bin/bash
/usr/bin/python3 /home/joaovitordeon/Documentos/test.py;
回答by smile
try this /home/your site folder name/public_html/gistfile1.shset cron path like above
试试这个 /home/your site folder name/public_html/gistfile1.sh像上面一样设置 cron 路径
回答by user2954660
Was having a similar problem that was resolved when a sh
was put before the command in crontab
有一个类似的问题,当 ash
在命令之前被解决时crontab
This did not work :
这不起作用:
@reboot ~myhome/mycommand >/tmp/logfile 2>&1
This did :
这确实:
@reboot sh ~myhome/mycommand >/tmp/logfile 2>&1
回答by Thibault
As a complement of other's answers, didn't you forget the username in your crontab script ?
作为其他答案的补充,您是否忘记了 crontab 脚本中的用户名?
Try this :
尝试这个 :
* * * * * root /bin/bash /var/scripts/vpn-check.sh
EDIT
编辑
Here is a patch of your code
这是你的代码补丁
#!/bin/sh
/bin/ping -c3 192.168.17.27 > /tmp/pingreport
result=`grep "0 received" /tmp/pingreport`
truncresult=`echo "$result" | /bin/sed 's/^\(.................................\).*$//'`
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
/usr/sbin/pppd call home
fi