Bash脚本不能用作cron作业

我有以下的shell脚本

$cat capture.sh TIME=$(date +"%H-%M-%d-%m-%y") IP="203.208.198.29" PREFIX=$TIME$IP tshark -f "udp" -i eth0 -w /root/captures/$PREFIX.cap& pid=$! sleep 2m kill $pid 

它运行良好,当我从壳执行它。

但是当我把它添加到cron选项卡没有任何反应。

我的crontab项:1 * / 2 * 2 3,4,5 sh /root/capture.sh

 tail /var/log/cron 

显示该命令已执行。

但没有任何反应。 我为capture.sh设置了“all”的可执行权限,为/ root / captures目录设置了“all”的权限。

提前致谢

你的PATHvariables可能不是你期望它在cron里面。

使用脚本中每个可执行文件的完整path,或者在您的crontab或脚本中手动设置path。

另外,更好的停止你的tshark方法是使用内置的function:

  -a <capture autostop condition> duration:value Stop writing to a capture file after value seconds have elapsed. 

另外#2:添加一个shebang行( #!

Cron将限制cron作业使用的path。 尝试/ usr / sbin / tshark而不是只是tshark。 你可以通过which tshark检查which tshark

看看你的脚本,我看到你正在尝试捕获两分钟的stream量,并写一个文件。 你真的有意要在二月份的每周三/周四/周五有一个cronjob,每隔一小时一小时? 我猜你希望它每2分钟运行一次…

crontab(5) (可以用man 5 crontab读取)

  cron(8) examines cron entries once every minute. The time and date fields are: field allowed values ----- -------------- minute 0-59 hour 0-23 day of month 1-31 month 1-12 (or names, see below) day of week 0-7 (0 or 7 is Sun, or use names) A field may be an asterisk (*), which always stands for "first-last". 

这是因为没有findtshark命令,你有两个select来解决它。

  1. 在crontab中定义path

    PATH = $ PATH:/path对tshark的

    * / 2 * 2 3,4,5 sh /root/capture.sh

  2. 在脚本中使用tshark的完整path。

    选项#1是首选的方法