Bash 脚本无法在 Cron 中正确执行
因此,我有一个 bash 脚本,该脚本应该每小时将 ifconfig 的输出通过管道传输到文本文件。正如许多人似乎遇到的那样,该脚本在 cron 使用时无法正常运行。然而,我所看到的大多数修复似乎并不适用于我的情况。我已明确声明所有路径,脚本具有执行权限,并且 cron 文件末尾有一个换行符。
足够有创意的是,我的脚本 ip.sh 包含:
ifconfig > /home/drake/Dropbox/maintenance_scripts/ip.txt
cron 条目是:(
0 * * * * /home/drake/Dropbox/maintenance_scripts/ip.sh
我让它每分钟运行一次以进行调试)
这里最大的问题是,当它运行并且确实运行时,它会清除 ip.txt它可能包含的任何内容。另外,我还有另一个脚本,它在正常运行时间方面做了同样的事情,并且它的工作没有任何问题,只是令人困惑。我也尝试过>>,这似乎产生了相同的结果
所以,有谁知道为什么一个脚本可以按预期运行,而另一个脚本会像这样derp-out?
这是在我的 Ubuntu 服务器上运行的。我正在使用 Dropbox 同步文本文件
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.
creatively enough, my scrip, ip.sh, contains:
ifconfig > /home/drake/Dropbox/maintenance_scripts/ip.txt
the cron entry is:
0 * * * * /home/drake/Dropbox/maintenance_scripts/ip.sh
(i have it running every minute for debugging)
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?
This is running on my Ubuntu server. I am using Dropbox to sync the text-files
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的脚本顶部应该有一个“shebang”行:
尽管这不是绝对必需的。
该脚本会覆盖
ip.txt
,因为您告诉它这样做。这就是>
重定向运算符的作用。如果要附加到文件末尾,请使用>>
。 (你说你尝试过,结果相同。我怀疑这是真的。我怀疑 cron 作业没有产生任何输出,所以它没有向文件附加任何内容。)但是你不需要一个单独的脚本来进行重定向;您可以在 cron 作业本身中使用
>
或>>
:至少在我的系统上,cron 的默认
$PATH
jobs 是/usr/bin:/bin
,但ifconfig
是/sbin/ifconfig
。尝试which ifconfig
或type ifconfig
查看ifconfig
在您系统上的位置(我们都使用 Ubuntu,所以可能是相同的),并在 cron 作业中使用完整路径;例如:如果您想查看输出更改的时间(我想这就是您要检查的内容),那么添加时间戳就很容易了:
Your script should have a "shebang" line at the top:
though it's not absolutely required.
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.)But you don't need a separate script just to do the redirection; you can use
>
or>>
in the cron job itself:And at least on my system, the default
$PATH
for cron jobs is/usr/bin:/bin
, butifconfig
is/sbin/ifconfig
. Trywhich ifconfig
ortype ifconfig
to see whereifconfig
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: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:
ip.sh
脚本应该在每次运行时 zapip.txt
文件;然后由ifconfig
程序重新填充它。所以,你的问题是ifconfig
没有这样做。从
cron
运行时常见的问题是环境。请注意,当 cron 运行命令时,不会使用配置文件。在这种情况下,我认为
cron
下的 PATH 设置不包括ifconfig
所在的目录(因此/sbin
是不在路径上)。明显的解决方法:/sbin/ifconfig ...
export PATH=$PATH:/sbin; ifconfig ...
我认为简单地从
crontab
文件运行脚本通常是开展业务的最佳方式。在不更改crontab
设置的情况下,您可以添加调试代码、更改环境设置,或者根据自己的喜好进行修改。在 crontab 文件中摆弄复杂的命令行迟早会在某些系统或其他系统上遇到麻烦。所以我认为你的系统是明智的 - 我在我自己的 cron 运行脚本中使用了一个更复杂的系统,但有很多共同点。关键是 crontab 条目很简单,而运行的脚本隐藏了复杂性。The
ip.sh
script is supposed to zap theip.txt
file every time it is run; it is then up to theifconfig
program to repopulate it. So, your problem is thatifconfig
isn't doing that.The usual problem when running things from
cron
is environment. Note that whencron
runs a command, the profile is not used.In this case, I'd lay odds that the PATH setting under
cron
doesn't include the directory whereifconfig
lives (so/sbin
is not on PATH). Obvious workarounds:/sbin/ifconfig ...
export PATH=$PATH:/sbin; ifconfig ...
I think simply running a script from the
crontab
file is usually the best way to do business. Without changing thecrontab
settings, you can add debug code, change the environment settings, and otherwise tinker to your heart's content. Fiddling with complex command lines in thecrontab
file is liable to run into trouble, sooner or later, on some system or other. So I think your system is sensible - I use a somewhat more complex system in my owncron
-run scripts, but there is a lot of commonality. The key point is that the crontab entry is simple and the script that's run is where the complexity is hidden.试试这个,它对我来说在 cron 中有效。
请注意,ifconfig 输出可能会因 Linux 发行版而异,因此您可能需要调整 awk 选择。
Try this one, it worked from the cron for me.
Note that ifconfig output can change from Linux distribution to distribution so you may need to adjust the awk selections.