1

I have two binary files that I want to execute one after the other, the thing is that I want to execute both for one minute. I have the following bash code:

./file_1
./file_2

but I do not know how to only run it for a minute.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

2 Answers2

3

Here's a portable solution - run the binaries in the background, and kill them after a minute:

for file in "file_1" "file_2"; do
    "./$file" &
    file_pid=$!

    sleep 60

    kill "$file_pid"
done

The & operator starts a background job, and the special variable $! contains the PID of the last job. The loop is optional. We can use it to reduce duplicated code.

Cy Rossignol
  • 16,216
  • 4
  • 57
  • 83
0

This is a one-liner using the timeout command

$ timeout 60 binary-1; timeout 60 binary-2

In this case, the format is:

timeout duration command

Where duration defaults to seconds, or also could be used 1m (1 minutes), from the man:

duration is a floating point number followed by an optional unit:

‘s’ for seconds (the default)
‘m’ for minutes
‘h’ for hours
‘d’ for days
nbari
  • 25,603
  • 10
  • 76
  • 131