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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.