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
How to Win an Online Auction: 5 Tips That Will Guarantee Success - Wiggins Auctioneers and Real Estate
Stolen crypto doubled to $1.38 billion in the first half of 2024, research firm says
Woodward Avenue (M-1) - Automotive Heritage Trail - National Scenic Byway Foundation
Places 5 Hours Away From Me
Ffxiv Shelfeye Reaver
How to know if a financial advisor is good?
50 Meowbahh Fun Facts: Net Worth, Age, Birthday, Face Reveal, YouTube Earnings, Girlfriend, Doxxed, Discord, Fanart, TikTok, Instagram, Etc
Encore Atlanta Cheer Competition
Magic Mike's Last Dance Showtimes Near Marcus Cedar Creek Cinema
Erskine Plus Portal
Aries Auhsd
Amateur Lesbian Spanking
Culver's Flavor Of The Day Monroe
Catsweb Tx State
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Craigslist Pets Longview Tx
24 Hour Walmart Detroit Mi
Sam's Club La Habra Gas Prices
Inter-Tech IM-2 Expander/SAMA IM01 Pro
Days Until Oct 8
Iu Spring Break 2024
Tips and Walkthrough: Candy Crush Level 9795
4 Times Rihanna Showed Solidarity for Social Movements Around the World
Hefkervelt Blog
Pensacola Tattoo Studio 2 Reviews
Dal Tadka Recipe - Punjabi Dhaba Style
Garden Grove Classlink
24 Hour Drive Thru Car Wash Near Me
Kacey King Ranch
Renfield Showtimes Near Marquee Cinemas - Wakefield 12
R3Vlimited Forum
Mkvcinemas Movies Free Download
Ourhotwifes
Tra.mypatients Folio
Justin Mckenzie Phillip Bryant
Mississippi State baseball vs Virginia score, highlights: Bulldogs crumble in the ninth, season ends in NCAA regional
Back to the Future Part III | Rotten Tomatoes
Synchrony Manage Account
Puretalkusa.com/Amac
Sept Month Weather
Tsbarbiespanishxxl
Wal-Mart 140 Supercenter Products
Ig Weekend Dow
Dwc Qme Database
Blue Beetle Showtimes Near Regal Evergreen Parkway & Rpx
3500 Orchard Place
Craigslist Sparta Nj
Dietary Extras Given Crossword Clue
The 13 best home gym equipment and machines of 2023
Worlds Hardest Game Tyrone
Emmi-Sellers
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.