How to use timeout command to terminate a command and proceed accordingly (2024)

In other words, only if they are in VPN. It’s a bit of a leap of faith, I know. To diagnose if I am in VPN, I just use a blunt mechanism of timeout — if the program runs for > 5s, it follows that you are not in the VPN, which is another leap, I know. But hey, this is an internal and team-specific box, so it should be a good-enough measure. The abstract (therefore interesting:) question remains: how to timeout a program in bash and make a case analysis based on that, i.e. if timed out do something (exit early) and if not timed out, just run along the happy path.

Use timeout command and exit quickly if timed out, which is confirmed by exit code 124

  • Use timeout command
  • ⟹ time runs out and the command that follows is terminated; from timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
Start COMMAND, and kill it if still running after DURATION.
<!-- EXAMPLE -->
timeout 2s ping google.com
  • If any command times out, the timeout returns error code 124
EXIT status:
124
if COMMAND times out, and −−preserve−status is not specified

timeout(1) — Linux manual pages

  • If that happens, so I just explain myself and terminate
timeout 5s vault login -method=github token="${GITHUB_TOKEN}"
if [[ $? == 124 ]]; then
echo "ERROR: Authentication timed out after 5s!"
echo "~~> VPN is a prerequisite — please connect and try again"
exit
fi
  • If no, and command returned 0 (all good, it did not time out) ⟹ just go on

Just for fun, it is possible to use --preserve-status and then go with error_code 143

  • If I use --preserve-status flag, the failed command in my case generates exit code 143
  • Exit code 143 is returned by SIGTERM signal, which is basically a signal for grafeful termination

is a generic signal used to cause program termination. Unlike SIGKILL, this signal can be blocked, handled, and ignored. It is the normal way to politely ask a program to terminate. The shell command kill generates SIGTERM by default.

Termination Signals (The GNU C Library)

  • Say you want to do this with a ping command (echo $? checks for the last exit code)
~$ timeout --preserve-status 2 ping google.com
PING google.com(prg03s13-in-x0e.1e100.net (2a00:1450:4014:80f::200e)) 56 data bytes
64 bytes from prg03s13-in-x0e.1e100.net (2a00:1450:4014:80f::200e): icmp_seq=1 ttl=116 time=18.4 ms
64 bytes from prg03s13-in-x0e.1e100.net (2a00:1450:4014:80f::200e): icmp_seq=2 ttl=116 time=16.0 ms
~$ echo $?
143
  • In my case, I’d just add the flag and replace 124143
timeout --preserve-status 5 vault login -method=github token="${GITHUB_TOKEN}"
if [[ $? == 143 ]]; then
echo "ERROR: Authentication timed out after 5s!"
echo "~~> VPN is a prerequisite — please connect and try again"
exit
fi

LINKS

How to use timeout command to terminate a command and proceed accordingly (2024)
Top Articles
Investissem*nt ETF actif ou passif : quelle est la différence ? - ThePressFree
What are Index Funds - Meaning, Benefits and How to Invest
Evil Dead Movies In Order & Timeline
7 C's of Communication | The Effective Communication Checklist
Craigslist Monterrey Ca
Fat Hog Prices Today
How To Do A Springboard Attack In Wwe 2K22
25X11X10 Atv Tires Tractor Supply
Obituaries
Dityship
Planets Visible Tonight Virginia
Weekly Math Review Q4 3
Cincinnati Bearcats roll to 66-13 win over Eastern Kentucky in season-opener
Apus.edu Login
Vanessa West Tripod Jeffrey Dahmer
Ostateillustrated Com Message Boards
Praew Phat
E22 Ultipro Desktop Version
Selfservice Bright Lending
Universal Stone Llc - Slab Warehouse & Fabrication
Dr Ayad Alsaadi
Pirates Of The Caribbean 1 123Movies
Engineering Beauties Chapter 1
25 Best Things to Do in Palermo, Sicily (Italy)
Rapv Springfield Ma
Wood Chipper Rental Menards
Darrell Waltrip Off Road Center
Churchill Downs Racing Entries
O'reilly's In Monroe Georgia
His Only Son Showtimes Near Marquee Cinemas - Wakefield 12
Superhot Free Online Game Unblocked
UAE 2023 F&B Data Insights: Restaurant Population and Traffic Data
Lilpeachbutt69 Stephanie Chavez
The Creator Showtimes Near Baxter Avenue Theatres
Capital Hall 6 Base Layout
How to Destroy Rule 34
Merge Dragons Totem Grid
Sams La Habra Gas Price
Complete List of Orange County Cities + Map (2024) — Orange County Insiders | Tips for locals & visitors
Sand Castle Parents Guide
Pink Runtz Strain, The Ultimate Guide
Lawrence E. Moon Funeral Home | Flint, Michigan
Lyons Hr Prism Login
John Wick: Kapitel 4 (2023)
How to Connect Jabra Earbuds to an iPhone | Decortweaks
Conan Exiles Colored Crystal
Lesly Center Tiraj Rapid
Secrets Exposed: How to Test for Mold Exposure in Your Blood!
Coleman Funeral Home Olive Branch Ms Obituaries
Craigslist Cars And Trucks For Sale By Owner Indianapolis
Pauline Frommer's Paris 2007 (Pauline Frommer Guides) - SILO.PUB
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 5905

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.