0

I've got a shell script with the following code;

#!/bin/bash
nfcapd -z -w -t30 -p 2055 -l /home/shane/Documents/nfdump

My problem is that when I execute the shell script, the command above executes over and over (it captures network traffic from a router), but I'd like for it to stop after 30 seconds. In the terminal I would just press Ctrl+c, is there a way of executing this command (Ctrl+c) after a certain time t?

Mwiza
  • 7,780
  • 3
  • 46
  • 42
Shane
  • 461
  • 2
  • 8
  • 23
  • take a look at this-: http://stackoverflow.com/questions/5161193/bash-script-that-kills-a-child-process-after-a-given-timeout – suspectus Mar 01 '13 at 11:26

3 Answers3

6

Use timeout

timeout 30 nfcapd -z -w -t30 -p 2055 -l /home/shane/Documents/nfdump

More info here

user000001
  • 32,226
  • 12
  • 81
  • 108
2

Put this statement after #!/bin/bash

(sleep 30 ; kill -9 $$ )&

See if it works in your case.

Kaunteya
  • 3,107
  • 1
  • 35
  • 66
  • Almost -1: You must not recommend `-9` here. Especially given that the question specifically mentions ctrl-C, which sends `SIGINT`. – William Pursell Mar 01 '13 at 13:59
2

You may also kill the last job:

#! /bin/bash
nfcapd -z -w -t30 -p 2055 -l /home/shane/Documents/nfdump &
sleep 30 ; kill %%
echo "nfcapd terminated"
exit 0
Edouard Thiel
  • 5,878
  • 25
  • 33