0

As a beginner Linux user I'm facing with the little problem. I have 1 command in terminal like in the picture: enter image description here

When I run this command it will generate some lines, but the problem is I need to make this command stops after 5 seconds without actually pressing ctrl + c by human :

ctrl + c stops command

What methods I need to use to make this done by computer without actually pressing ctrl + c because I need to repeat this process over 1000 times.

  • run command
  • stop command
    • (pause xx seconds)
  • run command
  • stop command
    • (pause xx seconds)
  • etc.

Do I need to install some special programs or it can be done using bash scripting?

1
  • 1
    Don't post images of text please.
    – user147505
    Mar 26, 2017 at 14:54

2 Answers 2

2

You can run the command in the background and then sleep for 5 seconds in the foreground and then kill the background command.

  • Run the command in the background:

    command &
    
  • Save the command PID in a variable:

    command_pid=$!
    
  • Sleep for 5 secs:

    sleep 5
    
  • Kill the background process:

    kill "$command_pid"
    

Now you can add a pause and put the whole in a loop, etc.

Loop:

for ((i=0; i<1000; i++)); do
    crunch 7 7 abcdefghijklm &
    command_pid=$!
    sleep 5
    kill "$command_pid"
    sleep 5 #pause
done
6
  • I'm sorry because I don't understand how to use your suggested commands. Do I need to type one by one in terminal and press Enter or this is assumed to be in one executable file or something else? I don't quite understand the concept because of poor knowledge.
    – user219258
    Mar 26, 2017 at 15:40
  • 1
    @Vcore9 I added an example. Put it in a file and then do . file_name. It will go 1000 times.
    – user147505
    Mar 26, 2017 at 15:47
  • this works but can you suggest me how I can add an option to this loop which simulate keyboard presses, for example: after line sleep 5 if I press button K and then Enter on keyboard it will show that on screen and press Enter to jump in new line. What should I use to accomplish that?
    – user219258
    Mar 26, 2017 at 16:18
  • 1
    @Vcore9 I'm not sure what you want to achieve. Can you give an example?
    – user147505
    Mar 26, 2017 at 16:40
  • 1
    @Vcore9 Some commands have an option like -y that assumes yes to all questions. You can also use yes with some pipes or redirections. Start a new question for this with details of what you want.
    – user147505
    Mar 26, 2017 at 17:03
2

Assuming timeout(1) is available, the shell command

timeout 5s whateverthatcommandis; sleep 99

should kill the program after five seconds (assuming the program is well behaved...) and then sleep for whatever your xx seconds are. These commands could then be wrapped in some sort of loop as necessary; assuming seq is available (on BSD one might instead use jot) a typical loop might run along the lines of:

for n in `seq 1 1000`; do timeout 5s whateverthatcommandis; sleep 99; done

The shell loop, by the way, may be rendered somewhat more legible under ZSH:

repeat 1000 { timeout 5s whateverthatcommandis; sleep 99 }
1
  • for n in `seq 1 1000`; do timeout 5s whateverthatcommandis; sleep 99; done only works first time, it stops after 5 seconds, then pause for xx seconds but it don't want to start again, just stays on root@kali:~# and waits something to be typed in. Command repeat 1000 { timeout 5s whateverthatcommandis; sleep 99 } didn't work in terminal.
    – user219258
    Mar 26, 2017 at 15:36

You must log in to answer this question.