Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
What happens if you drop the cron job shell script into the cron.hourly directory? Will it run that way? (note: make it executable) –  djangofan Feb 1 '12 at 20:00

4 Answers 4

up vote 30 down vote accepted

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 (#!)

share|improve this answer
13  
What is this...I don't even... –  TylerShads Feb 1 '12 at 20:13
    
environment variable is supported in cron. man 5 crontab An active line in a crontab will be either an environment setting or a cron command. An environ- ment setting is of the form, name = value –  honglus Feb 3 '12 at 1:18

Cron will limit the path used by cron jobs. Try /usr/sbin/tshark instead of just tshark. You can check where tshark is via which tshark

share|improve this answer

Looking at your script I see you're attempting to capture traffic for two minutes and write a file. Did you really mean to have a cronjob that runs on every Wed/Thur/Friday in February, every other hour at 1 past the hour? I'm guessing you wanted it to run every 2 minutes...

From crontab(5) (which can be read with 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".
share|improve this answer
    
no i'm trying to run it in every two hours. I'm doing some data mining to find a pattern. so i'm taking a sample every two hours. –  CodeBladeRunner Feb 2 '12 at 20:00
    
Then you want to do 0 */2 * * * –  Kyle Smith Feb 3 '12 at 1:40

It because tshark command was not found, you have two options to fix it.

  1. define path in crontab

    PATH=$PATH:/path-to-tshark

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

  2. Use full path of tshark in your script.

    option #1 is the preferred way

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.