1

I have this command:

awk 'BEGIN {system ("mplayer mms :// xx.xx.xx -dumpstream -dumpfile xxx.wmv")}'

How to end this command after 60 seconds?

Martin G
  • 17,357
  • 9
  • 82
  • 98
Tedee12345
  • 1,182
  • 4
  • 16
  • 26
  • possible duplicate of [Timeout a command in bash without unnecessary delay](http://stackoverflow.com/questions/687948/timeout-a-command-in-bash-without-unnecessary-delay) – miku Apr 16 '12 at 20:05
  • Why are you running that in an AWK `system` command instead of directly? Is it part of a larger AWK script? – Dennis Williamson Apr 16 '12 at 21:29
  • `miku` Thank you for the link. `Dennis Williamson` Yes. This will be part of a larger awk script. – Tedee12345 Apr 17 '12 at 05:15

1 Answers1

3

You can get the pid of a bash subprocess by querying $!. Store the value of the awk process, sleep for 60 seconds and the kill the awk process.

awk 'BEGIN {system ("mplayer mms :// xx.xx.xx -dumpstream -dumpfile xxx.wmv")}' &
pid=$!
sleep 60
kill $pid

This requires that you run the awk process as a background process & so that the parent control continues.

cmh
  • 10,612
  • 5
  • 30
  • 40
  • Thank you for your response. But how to end after 60 seconds to download a file by mplayer? – Tedee12345 Apr 16 '12 at 20:34
  • If you want to end 60 seconds after the command finishes, leave off the `&`. That way the sleep won't be executed until the command has finished. – cmh Apr 16 '12 at 20:42
  • My mistake. I want to download the file only 60 seconds. – Tedee12345 Apr 16 '12 at 20:59
  • I'm still not quite sure what you're asking. Do you want the download to take only 60 seconds (I'm afraid I can't help you with this). Or do you want the download to only proceed for 60 seconds (in which case see my original response). – cmh Apr 16 '12 at 21:15