Bash 脚本无法在 Cron 中正确执行 [英] Bash Script Not Executing in Cron Correctly

查看:26
本文介绍了Bash 脚本无法在 Cron 中正确执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个 bash 脚本,它应该每小时将 ifconfig 的输出通过管道传输到一个文本文件.好像很多人都遇到过,这个脚本被cron 使用时无法正常运行.但是,我所看到的大多数修复程序似乎不适用于我的情况.我已经明确声明了所有路径,脚本具有执行权限,并且在 cron 文件末尾有一个换行符.

So, I have a bash script that should pipe the output of ifconfig to a text file on the hour, every hour. As many people seem to have encountered, this script does not function properly when used by cron. However, most of the fixes for this that I have seen do not seem to be applicable in my case. I have all paths explicitly stated, the script has execute permissions, and there is a newline at the end of the cron file.

很有创意,我的脚本 ip.sh 包含:

creatively enough, my scrip, ip.sh, contains:

ifconfig > /home/drake/Dropbox/maintenance_scripts/ip.txt

cron 条目是:

0 * * * *   /home/drake/Dropbox/maintenance_scripts/ip.sh

(我让它每分钟运行一次以进行调试)

(i have it running every minute for debugging)

这里的大问题是,当它运行并且确实运行时,它会清除 ip.txt 中可能包含的任何内容.此外,我还有另一个脚本,它在正常运行时间方面做同样的事情,并且它没有任何问题,只是被混淆了.我也试过 >>,它似乎产生了相同的结果

The BIG issue here is, when it runs, and it does run, is that it clears ip.txt of any contents it might have. Also, I have another script which does the same exact thing with uptime, and it works w/o any issues, which just has be confused. I have also tried >>, which seemed to yield identical results

那么,有没有人知道为什么一个脚本可以按预期运行,而另一个脚本会像这样运行?

So, Does anyone have any ideas why one script might function as expected, and another would just derp-out like this?

这是在我的 Ubuntu 服务器上运行的.我正在使用 Dropbox 同步文本文件

This is running on my Ubuntu server. I am using Dropbox to sync the text-files

推荐答案

你的脚本顶部应该有一个shebang"行:

Your script should have a "shebang" line at the top:

#!/bin/sh

虽然不是绝对必需的.

脚本会覆盖 ip.txt,因为您已经告诉它了.这就是 > 重定向操作符所做的.如果要附加到文件末尾,请使用 >>>.(你说你尝试过,结果相同.我怀疑这是真的.我怀疑 cron 作业没有产生任何输出,所以它没有向文件附加任何内容.)

The script overwrites ip.txt because you told it to. That's what the > redirection operator does. If you want to append to the end of the file, use >>. (You said you tried that, with identical results. I doubt that that's true. I suspect the cron job just isn't producing any output, so it's appending nothing to the file.)

但是您不需要单独的脚本来进行重定向;您可以在 cron 作业本身中使用 >>> :

But you don't need a separate script just to do the redirection; you can use > or >> in the cron job itself:

0 * * * *    ifconfig >> /home/drake/Dropbox/maintenance_scripts/ip.txt

至少在我的系统上,cron 作业的默认 $PATH/usr/bin:/bin,但 ifconfig/sbin/ifconfig.尝试 which ifconfigtype ifconfig 以查看 ifconfig 在您的系统上的位置(我们都使用 Ubuntu,所以它可能是相同的),并在 cron 作业中使用完整路径;例如:

And at least on my system, the default $PATH for cron jobs is /usr/bin:/bin, but ifconfig is /sbin/ifconfig. Try which ifconfig or type ifconfig to see where ifconfig lives on your system (we're both using Ubuntu, so it's probably the same), and use the full path in the cron job; for example:

0 * * * *    /sbin/ifconfig >> /home/drake/Dropbox/maintenance_scripts/ip.txt

如果您想查看什么时候输出发生变化(我想这就是您要检查的内容),添加时间戳很容易:

And if you want to see when the output changed (I presume that's what you're checking for), it's easy enough to add a timestamp:

0 * * * *    ( date ; /sbin/ifconfig ) >> /home/drake/Dropbox/maintenance_scripts/ip.txt

这篇关于Bash 脚本无法在 Cron 中正确执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆