Using Signals to Handle Unix Commands in Golang

Learn to use in-built Go features to handle Unix commands

Arindam Roy
Oct 21 · 2 min read
Photo by Harshal Desai on Unsplash

Accepting and processing signals from the operating system is important for various use cases in applications.

While many server-side languages have complicated or tedious approaches to processing signals from the OS, with Golang applications it’s extremely intuitive. Golang’s in-built OS package provides an easy way to integrate and react to Unix signals from your Go application. Let’s see how.


The Premise

Let’s say we want to build a Golang application that when requested to shutdown prints a message saying, “Thank you for using Golang.” Let’s set up the main function that basically keeps doing some work until an exit command is provided to the application.

When you run this application and kill it by providing a kill signal from your OS (Ctrl + C or Ctrl + Z, in most cases), you may see an output similar to this one:

Now, we would like to interpret this kill signal within the Golang application and process it to print out the required exit message.


Receiving Signals

We will create a channel to receive the command from the OS. The OS package provides the Signal interface to handle signals and has OS-specific implementations.

To notify killSignal, we use the Notify function provided by the signal package. The first parameter takes a channel of a os.Signal, while the next parameters accept a list of OS signals we want to notify our channel with.

Alternatively, we can notify our signal with specific commands using the syscall package.

In order to process the signal, we’ll make our main function block wait for the interrupt signal using the killSignal channel. On receiving a command from the OS, we’ll print the exit message and kill the application.

In order to process our work loop, let’s move that into a separate goroutine using an anonymous function.

While the work function runs in a separate routine, the main function will wait for the killSignal and print the exit message before exiting.


The Code

With all the components put together, the final code is this:

On running this, it keeps executing the work loop, and upon receiving an interrupt signal from the OS, it prints the required message and then exits.


Conclusion

This simple example can be extrapolated to handle many real-life scenarios, such as gracefully shutting down servers and receiving commands in command-line applications.

Better Programming

Advice for programmers.

Arindam Roy

Written by

Software Engineer @shopee.sg. I write about a lot of things, mostly tech.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade