0

I have tried a method using timeout to identify and exit server comes to RUNNING state or else exit watching for RUNNING state at the timeout value, unfortunately one of the server has bash version 3 and doesn't support time out.

timeout 300s grep -q 'Server state changed to RUNNING' <(tail -f AdminServer.out)
if [ $? != 0];then
  printf "\n===> unable to bring up the server.please check\n"
else
  printf "\n===> server came to RUNNING state\n"
fi

I have tried the following but wasn't able to exit when the log is appeared

( cmdpid=$BASHPID; (sleep 60; kill $cmdpid) & exec `grep -q 'Server state changed to RUNNING' <(tail -f AdminServer.out)`)

but it first give command line substitution error for grep -q 'Server state changed to RUNNING' <(tail -f AdminServer.out) and then gives error for kill usage like below(seems the kill is not working) , kill: usage: kill [-s sigspec | etc....

  • I'm not in a position to upgrade the bash version of the server unfortunately, any guidance for this highly appreciated .Thank you!!

*server: suse distribution 2.6.32.12-0.7-default

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
user1672382
  • 87
  • 1
  • 10
  • 2
    Bash doesn't support timeout because it is an external command. It's part of coreutils in both ubuntu and centos. I don't have a suse system to check, but if it is missing, you could probably get the source and compile it. – user10489 Aug 26 '21 at 04:32
  • @user1672382 : This is not bash-related, since `timeout` is not an internal bash command anyway. However, I would be surprised if Suse 2.6 would not include the timeout command; maybe they have it in a non-standard directory? I would ask this at [Suse|(https://forums.opensuse.org/). What other languages are available, aside from bash? Many programming languages (Perl, Tcl,...) have a function to do timeout control, so you could write a wrapper in one of those. – user1934428 Aug 26 '21 at 07:31
  • ``exec `grep -q 'Server state changed to RUNNING' <(tail -f AdminServer.out)` `` is not well-defined; `grep -q` will never output anything, so there is nothing for `exec` to execute. – tripleee Aug 26 '21 at 07:47
  • See also [Why is testing “$?” to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee Aug 26 '21 at 07:49
  • https://unix.stackexchange.com/questions/156131/can-i-trap-a-clock-signal-in-my-bash-script has some examples of correct ways to send a signal after a given time. – tripleee Aug 26 '21 at 08:13

3 Answers3

0

Consider to try something like this:

data=$(
    tail -f AdminServer.out & pid=$!
    
    till=$[SECONDS+300]
    while ((SECONDS<till)); do :; done
    kill -9 $pid
)

grep -q 'Server state changed to RUNNING' <<< "$data" \
     && printf "\n===> server came to RUNNING state\n" \
     || printf "\n===> unable to bring up the server.please check\n"
Ivan
  • 6,188
  • 1
  • 16
  • 23
  • You should never use `kill -9` routinely, and certainly not recommend it to others. See also [useless use of `kill -9`.](http://www.iki.fi/era/unix/award.html#kill) – tripleee Aug 26 '21 at 07:50
  • Even for Bash v3, probably prefer POSIX `$((arithmetic expression))` syntax over the obsolete Bash-only and frankly unattractive `$[expression]` syntax, especially since you already use it elsewhere in your script. – tripleee Aug 26 '21 at 08:05
  • This will necessarily wait for five minutes even in the success scenario, even if it succeeds immediately. – tripleee Aug 26 '21 at 08:07
  • I gave the idea, it has its drawbacks so use or leave it) – Ivan Aug 26 '21 at 10:29
0

When I was young... on OSF1, DEC Alpha, SPARC... without timeout command, without <(...) or <<< operators... I'm actively waiting...

File: timeout.sh

#! /bin/bash

PID="$1"
SEC="$2"
FILE="$3"

DATE_REF=$(date +%s)
DATE_MAX=$(( DATE_REF + ${SEC} ))

while ps -p "${PID}" >/dev/null 2>&1 && [[ $(date +%s) -lt ${DATE_MAX} ]] && ! grep OK "${FILE}" >/dev/null 2>&1; do
    sleep 1
done

if grep OK "${FILE}" >/dev/null 2>&1; then
    RET=0
else
    RET=1
fi

if ps -p "${PID}" >/dev/null 2>&1; then
    kill -9 "${PID}"
fi

rm -f "${FILE}"

exit $RET

Used like that:

nb_seconds_to_wait=5
tail -f my_log_file | grep -m 1 my_token_to_search && echo "OK" > my_flag_file &
timeout.sh $! nb_seconds_to_wait my_flag_file
if [[ $? -eq 0 ]]; then
    echo "Found"
else
    echo "Not found"
fi
Arnaud Valmary
  • 2,039
  • 9
  • 14
-1

I have tried again and came up with the following, am I doing anything harmful here?

( cmdpid=$BASHPID; ( sleep 10; if ps -p $cmdpid > /dev/null; then kill $cmdpid > /dev/null;fi ) & while `tail -f -n0 AdminServer.out | grep -q RUNNING`; do exit 0;done )
echo "proceeding with the rest of commands the previous command status is $?"
user1672382
  • 87
  • 1
  • 10
  • The command substitution still doesn't print anything; why do you think you need backticks around those commands? – tripleee Aug 26 '21 at 11:07