bash脚本cron中执行不正确 [英] Bash Script Not Executing in Cron Correctly

查看:180
本文介绍了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项是:

the cron entry is:

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

(我有它运行的每分钟用于调试)

(i have it running every minute for debugging)

这里的大问题是,当它运行时,它不运行,是它清除它可能拥有的任何内容ip.txt。另外,我还有一个脚本,做同样的事情与正常运行时间,和它的作品W / O型的任何问题,刚刚已经混淆。我也试过>>,这似乎产生相同的结果。

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

所以,没有人有任何想法,为什么预期一个脚本,可能会发挥作用,另一个只想DERP出这样?

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

推荐答案

试试这个,它从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.

# Cron ifconfig piping is broken
eth0ipIC=`/sbin/ifconfig eth0`
eth0ip=`echo $eth0ipIC|awk '{print $7}'|awk -F: '{print $2}'`

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

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