Skip to content
Advertisement

How to fail a Linux command if it doesn’t output a certain string within a minute?

I’m using AWS Codebuild to setup a self hosted Github Actions runner. In my main build section, I am just running the command ./run.sh and this starts the self hosted runner, listens for a job, and then picks up the particular job. In some cases, these don’t actually end up picking up a job and just sit out there listening for jobs for 30 minutes until the Codebuild times out.

What I would like to do is run this command, but check that within the first minute of running, it outputs a string that contains Running job:. If it does, great, keep running the run.sh command until it finished. If it doesn’t, exit the command and finish the codebuild.

Is this possible to do? I’m not all that familiar with Linux commands.

Advertisement

Answer

I was able to add this into my buildspec by using the following:

build:
  commands:
    - |
      cat >> expect.sh <<EOL
      #!/usr/bin/expect -f
      set timeout 30
      spawn ./run.sh
      expect {
        "Running job:" { puts "Job found"; set timeout -1; exp_continue }
        timeout { puts "Timed out"; exit 1 }
        eof { puts "End of command"; exit 0 }
      }
      EOL
    - expect -f expect.sh
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement