2011-10-21 47 views
2
#!/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耦合时,它并没有做任何事情。没有输出文件被创建。shell脚本帮助cron作业未执行

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

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

+0

见错误在这里解释http://linux-junky.blogspot.com/2010/10/debugging-cronjobs.html –

回答

1

看起来你不设置工作目录,所以你可能需要给一个绝对路径输出文件

+0

谢谢最大你是对的绝对路径问题。代码现在工作正常 – asadz

+0

为awk命令提供绝对路径也更安全,而不是依赖crontab中设置的路径 – Max