【问题标题】:shell scripting help cron job not executingshell脚本帮助cron作业未执行
【发布时间】:2011-12-12 11:01:59
【问题描述】:
#!/bin/bash

#!/bin/sh

# Need help

__help() { echo "$0 [ stop|start ]" 1>&2; exit 1; }

# Not enough args to run properly

[ $# -ne 1 ] && __help

# See what we're called with

case "$1" in

start) # Start sniffer as root, under a different argv[0] and make it drop rights

s=$(/usr/local/sbin/tcpdump -n -nn -f -q -i lo | awk 'END {print NR}')
echo "$s" > eppps_$(/bin/date +'%Y%m%d%H%M')

;;

stop) # End run, first "friendly", then strict:

/usr/bin/pkill -15 -f /usr/local/sbin/tcpdump >/dev/null 2>&1|| { sleep 3s; /usr/bin/pkill -9 -f /usr/local/sbin/tc$

;;

*) # Superfluous but show we only accept these args

__help

;;
esac
exit 0

此代码在手动测试中完美运行。但是当我将它与 cron 结合起来时,它什么也没做。没有创建输出文件。

我的脚本的 cron 条目看起来像

http://postimage.org/image/1pztgd6xw/

【问题讨论】:

标签: shell scripting cron


【解决方案1】:

您似乎没有设置工作目录,因此您可能需要为输出文件提供绝对路径

【讨论】:

  • 谢谢 Max,您对绝对路径问题的看法是正确的。代码现在工作正常
  • 为 awk 命令提供绝对路径也比依赖 crontab 中设置的路径更安全
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-24
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 2014-09-06
  • 1970-01-01
相关资源
最近更新 更多