Linux – Bash script is not working as cron job

bashcronlinuxunix

I have the following shell script

$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 

it runs fine when i execute it from shell.

but when i add it to the cron tab nothing happens.

my crontab entry :
1 */2 * 2 3,4,5 sh /root/capture.sh

tail /var/log/cron 

shows that the command has executed .

but nothing happens. i have set executable permission for "all" for capture.sh and write permission for "all" for /root/captures directory.

Thanks in advance

Best Answer

Your PATH variable probably isn't what you expect it to be inside cron.

Use full paths to each executable in your script or set the path manually in your crontab or the script.

Also, a better way of stopping your tshark would be using the built-in functionality:

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

Also #2: add a shebang line (#!)