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
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.