Detect and restart hanging programs with Go
Recycled
Now you may be wondering what the implementation of yoyo
with its dark magic terminal trickery looks like. Setting up a pseudo-terminal pair requires some non-trivial boilerplate code, but fortunately there is already a project on GitHub named Expectre [1] that implements the Linux expect
tool in Go. The yoyo
program simply recycles Expectre's pseudo TTY code: In line 5 of Listing 3, it imports the library from GitHub (see the "Compiling Yoyo" box for more information).
Compiling Yoyo
To compile the yoyo
binary from the source code, the Go compiler needs the Expectre library used in Listing 3 from GitHub. The following three steps
$ go mod init yoyo $ go mod tidy $ go build yoyo.go
resolve the dependencies and build an executable binary from the Go code in Listing 2.
In line 24, yoyo
creates a new expectre
object, which then calls Spawn
, with the process parameters passed to yoyo
as arguments on the command line. In Go, os.Args[0]
traditionally contains the name of the called binary (yoyo
). The flag
package courtesy of Go's standard library will gobble up all command-line flags and arguments and later provides the agruments in the array slice returned by flag.Args()
. In the case at hand, the slice contains a single string, "./test.sh"
, which is the script that you want yoyo
to run and supervise. Line 25 passes it to Spawn()
.
The Spawn()
function from the expectre package then starts a process with the supervised program and lulls it into a pseudo-terminal pair. The controlled process will then assume it is running in a regular terminal and behave accordingly. As soon as the process is running, the for
loop starting in line 30 jumps into a select
statement that waits for one of four different events:
- A line of output printed by the launched process reaching the
exp.Stdout
channel - A line of output printed by the launched process reaching the
exp.Stderr
channel - The timer in line 36 expiring
- The
exp.Released
channel signalling that the process launched for monitoring has just terminated and no longer needs any supervision
A select statement
like this is typical of Go programs that wait for events. Each case waits either for messages from any number of monitored channels or for a timer to expire – and all concurrently without the computer having to do any active work.
Slow Traffic
Ultimately, yoyo
distinguishes between two cases. In the first case, the process terminates itself because it has reached the end of its instructions. This is perfect, because it means yoyo
does not have to do anything and can also terminate. But, in the second case, if the timer in line 36 elapses, yoyo
has to kill the monitored process; this is done neatly in line 39 by the Cancel()
function from the expectre package.
In this case, however, line 38 sets the triggered
variable to a true value. Once the monitored process terminates, a exp.Released()
message arrives, and line 43 uses continue restart
to resume the outer for
loop in line 22 with the restart
label. The supervised program comes back online when Spawn()
in line 25 restarts the process.
Brief Connection
Every application is different – while one application might want to be reminded to get back to work after 30 seconds, another might need a shorter timeout. You also want yoyo
to set a limit for the number of startup attempts. If something goes wrong, you wouldn't want it to indefinitely request restarts and potentially annoy the owners of the services it keeps contacting that way. The --timeout
and --maxtries
flags allow the user to set appropriate values to control this behavior, and Go's flag package takes care of fielding input from the command line and syntax checking the arguments.
Figure 4 shows a yoyo
run with a two-second timeout and at most two restarts of the test.sh
test script from Listing 2. After the second restart also times out, yoyo
terminates as instructed. In Figure 5, during a longish commit, yoyo
's monitoring of the git push
command shows that the commit falls asleep after some time because the server is slow to respond. After 30 seconds, yoyo
detects this, terminates the frozen process, and then wakes it up again. And, lo and behold, the process continues.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.