logank9
logank9

Reputation: 1

Write a bash script to check if process is responding in x seconds?

How can I write a script to check if a process is taking a number of seconds to respond, and if over that number kill it?

I've tried the timeout command, but the problem is it is a source dedicated sever, and when i edit it's bash script:

    HL=./srcds_linux
    echo "Using default binary: $HL"

and change it to timeout 25 ./srcds_linux and run it as root it won't run the server: ERROR: Source Engine binary '' not found, exiting

So assuming that I can't edit the servers bash script, is there a way to create a script that can check if any program, not executed w/ the script is timing out in x seconds?

Upvotes: 0

Views: 680

Answers (1)

that other guy
that other guy

Reputation: 123700

It sounds like the problem is that you're changing the script wrong.

If you're looking at this script, the logic basically goes:

HL=./srcds_linux
if ! test -f "$HL"
then
  echo "Command not found"
fi
$HL

It sounds like you're trying to set HL="timeout 25 ./srcds_linux". This will cause the file check to fail.

The somewhat more correct way is to change the invocation, not the file to invoke:

HL=./srcds_linux
if ! test -f "$HL
then
  echo "Command not found"
fi
timeout 25 $HL

timeout kills the program if it takes too long, though. It doesn't care whether the program is responding to anything, just that it takes longer than 25 seconds doing it.

If the program appears to hang, you could e.g. check whether it stops outputting data for 25 seconds:

your_command_to_start_your_server | while read -t 25 foo; do echo "$foo"; done
echo "The command hasn't said anything for 25 seconds, killing it!"
pkill ./srcds_linux

Upvotes: 1

Related Questions