Bash Run command for certain time

bashtimetimeout

I am making a bash script for my own use. How can I run a command for a certain time (like 20 seconds) and then terminate it? I have tried a lot of solutions but nothing works. I also tried the timeout command with no success. Please give me some solution for this.

For example: I want to run this command in a script and terminate it after 10 seconds

some command

Best Answer

Hm, that should do the trick:

xmessage "Hello World" &
pidsave=$!
sleep 10; kill $pidsave

xmessage provides a quick test case here (in your case the airodump command should go there); & puts it into background.

$! holds the PID of the last started process (see e.g. https://stackoverflow.com/a/1822042/2037712); the PID gets saved into the variable pidsave.

After some waiting (sleep), send a TERM signal to the process.

Related Question