Leveling up your bash

Leveling up your bash

One quite common use case is to wait for stuff to be completed. Suppose there are a few tasks we can execute

task1
task2        

This will execute task1 to completion and then execute task2 and run that to completion.

We can run these tasks concurrently

task1 &
task2 &        

And we can also wait for them to complete

wait        

However, did you know that you can wait for either one of these tasks?

wait -n        

This will wait for one task to complete.

Here is the great thing about this.

We can get the exit code from the task through the $? variable.

Use case. Waiting for a Kubernetes job to complete.

A Kubernetes job can complete in two ways. It can either succeed, or it can fail. kubectl allows us to wait for a job to complete, but we can only wait for it to either succeed or to fail. But not either.

Thanks to the bash shell we can work around that quite easily.

s() (
  kubectl wait --for=condition=succeeded job/myjob --timeout=300s
  exit 0
)

f() (
  kubectl wait --for=condition=failed job/myjob --timeout=300s
  exit 1
)
        

We can now wait for these two states concurrently


s &
f &
# enabling set -e will make the shell script abort with an error if the wait returns with an error, meaning f completed first.

set -e
wait -n        

Useful right?

You can also build conditional logic with the exit codes. Suppose task3 should only run if task2 completes first and task2 returns exit code 1

task1 &
task2 &
if wait -n
then
  task3
fi        

Well, there are a million use cases and things that can be built using bash process management like this. Hopefully you have learned a new tool that you can start using!

要查看或添加评论,请登录

Henrik Holst的更多文章

  • Office365 Co-pilot

    Office365 Co-pilot

    This is why I don't work with WYSIWYG applications. Life is too short.

  • Notes on haproxy.cfg and mTLS

    Notes on haproxy.cfg and mTLS

    I have two applications, Grafana and a custom app, that I wish to expose via a load balancer. In this case i choose…

  • Async generators too evil for Python?

    Async generators too evil for Python?

    Suppose we are getting data from an endpoint and these chunks needs to be handled by a worker. The output of the…

    1 条评论
  • Rohypnol i mellanmj?lken eller?

    Rohypnol i mellanmj?lken eller?

    Sveriges politikerjunta r?stade igenom DCA avtalet som ger USA okontrollerad m?jlighet att placera k?rnvapen p? svensk…

  • Job Title: Expert Technical Writer with Advanced English Writing Techniques

    Job Title: Expert Technical Writer with Advanced English Writing Techniques

    Location: Anywhere with WiFi and a reliable spellchecker About Us: We are a cutting-edge tech company dedicated to…

  • Problem Architect

    Problem Architect

    Position Title: Problem Architect Department: Research and Development Location: Remote Reports To: Chief Technology…

  • Henriks minimal Linux boot

    Henriks minimal Linux boot

    I got tired of minimalistic boot systems trying to be minimal and of not knowing what is redundant boilerplate that can…

    12 条评论
  • The most copied and sold idea

    The most copied and sold idea

    Test-Driven Development (TDD) and Objectives and Key Results (OKRs) share a structural similarity in that both…

  • Streamlining Software Testing

    Streamlining Software Testing

    Testing is essential in the fast-paced field of software development to guarantee the dependability and functionality…

  • The Agile garbage fire

    The Agile garbage fire

    I have concluded that Agile has reached the critical mass where it will self-implode under its weight of technical debt…

社区洞察

其他会员也浏览了