[Bash] How to kill program after n seconds

[Bash] How to kill program after n seconds

I’m sorry that I haven’t written for such a long time. It’s going to change:)

We have a program, which can suspend our terminal. We want to be sure, that program won’t be executed for more than n seconds. Here is a solution:

((sleep 10 && killall top)&); top

Explanation:
sleep 10 is executed for 10 seconds and then exit (you can use sleep n if you want sleep to be executed for n seconds

killall top terminates all processes named “top”

top – is example of program; top shows information about processes in your system

command1 && command2 – executes command1 and then, if command1 has succedeed, command2

command & – executes command at the background of terminal

command1; command2 – executes command1 and command2, even if command1 hasn’t succedeed

Leave a Reply

Your email address will not be published. Required fields are marked *