The Go language with its simplicity, concurrency support, rich package ecosystem, and ability to compile down to a single binary is an attractive solution for writing services on Ubuntu.
Ubuntu 19.04, codename Disco Dingo, non-LTS, has finally been released for Desktops, Servers, Cloud and other instances and flavours.This version comes with nine months support and some interesting changes, the most notable ones being the polished and improved Yaru theme, GNOME 3.32, Mesa 19.0, Linux Kernel 5.0, and a number of packages upgraded to their latest version. In this section, you will get SQL Server 2017 on your Ubuntu machine and then you will install the necessary dependencies to run GoLang. Step 1.1 Install SQL Server. Note: To ensure optimal performance of SQL Server, your machine should have at least 4 GB of memory. If you need to get Ubuntu, check out the Ubuntu Downloads website.
However, the Go language does not natively provide a reliable way to daemonize itself. In this article I will describe how to take a couple of simple Go language programs and run them using a systemd service file that starts them at boot time on Ubuntu 16.04.
If you have not installed Go on Ubuntu, first read my article here.
If you are on Ubuntu 14.04 and want to use sysV init scripts instead, read my article here.
Service Considerations
Before we start, let's consider the issues we must address when going from running a foreground task versus a daemon.
First, the application needs to run in the background. Because of complex interactions with the Go thread pool and forks/dropping permissions [1,2,3,4], running a simple nohup or double fork of the program is not an option – but truthfully it should not be anyway given the rich set of alternatives available today.
There are many process control systems such as Supervisor and monit, but with Ubuntu 16.04 we can use the systemd which is the default init system.
Background processes are detached from the terminal, but can still receive signals, so we would like a way to catch those so we can gracefully exit if required.
For security, we should have the daemon run as its own user so that we can control exactly what privileges and file permissions are accessed.
Then we need to ensure that logging is available. While ‘journalctl' does provide the logs, what we really want is to have the logs available in the standard '/var/log/' location. So we will tell systemd to send to syslog, and then have syslog write our files out to disk.
Finally, the service should be part of the boot process, so that it automatically starts after reboot.
SleepService in foreground
Let's start with a simple Go program that goes into an infinite loop, printing 'hello world' to the terminal with a random sleep delay in between. The real program logic is highlighted below, the rest is setup to catch any signals that are received.
First we will run it in the foreground as our current user. Below are the commands for Linux:
Which should produce output that looks something like below that exits when you Control-C out the execution:
Notice that the application did not just halt abruptly. It sensed the Control-C (SIGINT signal), performed custom cleanup of the application, then exited. https://trueofil223.weebly.com/google-chrome-34-mac.html.
If you were to start sleepservice in one terminal, then go to a different terminal and send various signals to the process with killall: Win at casino slots.
You would see the application reflect those different signals, like below where a SIGTRAP was sent:
SleepService as systemd service
Turning this into a service for systemd requires that we create a unit service file at '/lib/systemd/system/sleepservice.service' like below:
The absolute paths in ‘ConditionPathExists', ‘WorkingDirectory', and ‘ExecStart' all need to be modified per your environment. Notice that we have instructed systemd to run the process as the user ‘sleepservice', so we need to create that user as well.
Below are instructions for creating the user and moving the systemd unit service file to the correct location:
Now, you should be able to enable the service, start it, then monitor the logs by tailing the systemd journal:
The journal is stored as a binary file, so it cannot be tailed directly. But we have syslog forwarding enabled on the systemd side, so now it is just a matter of configuring our syslog server.
For full instructions on configuring syslog on Ubuntu, read my article here. But here are quick instructions for Ubuntu 16.04.
First modify '/etc/rsyslog.conf' and uncomment the lines below which tell the server to listen for syslog messages on port 514/TCP.
Then, create '/etc/rsyslog.d/30-sleepservice.conf' with the following content:
Now restart the rsyslog service and you should see the syslog listener on port 514, restart the sleepservice, and now you should see log events being sent to the file every few seconds.
Listing the running processes shows that the process is running as the 'sleepservice' user.
Golang Ubuntu 19 04 19
Stopping the service will show that the SIGTERM signal was sent to the application and it cleaned up before stopping.
But, if you were to send a SIGINT signal (interrupt), notice that the service restarts because of the 'Restart=on-failure' we indicated in the service file (see Table1).
By default, the service will be run at boot time by the 'WantedBy=multi-user.target' setting, and there is a link under '/etc/systemd/system/multi-user.target.wants/'.
EchoService in foreground
Now let's move on to building a simple REST service that listens on port 8080 and responds to HTTP requests. Picsart update 2018. Below is a snippet of the main functionality which configures a router and handler:
Here is an example of building and running the service:
Which should produce output on the server that looks something like:
We can see that the server is listening on port 8080. So now moving over to a client host, we run curl against the '/hello' service (or use a browser), sending a parameter of 'foo'.
And on the server side the output looks like:
Golang Ubuntu 19 04 17
EchoService as systemd service
Turning this into a service for systemd requires that we create a unit service file at '/lib/systemd/system/echoservice.service' like below:
The absolute paths in ‘ConditionPathExists', ‘WorkingDirectory', and ‘ExecStart' all need to be modified per your environment. Notice that we have instructed systemd to run the process as the user ‘echoservice', so we need to create that user as well.
Finally, the service should be part of the boot process, so that it automatically starts after reboot.
SleepService in foreground
Let's start with a simple Go program that goes into an infinite loop, printing 'hello world' to the terminal with a random sleep delay in between. The real program logic is highlighted below, the rest is setup to catch any signals that are received.
First we will run it in the foreground as our current user. Below are the commands for Linux:
Which should produce output that looks something like below that exits when you Control-C out the execution:
Notice that the application did not just halt abruptly. It sensed the Control-C (SIGINT signal), performed custom cleanup of the application, then exited. https://trueofil223.weebly.com/google-chrome-34-mac.html.
If you were to start sleepservice in one terminal, then go to a different terminal and send various signals to the process with killall: Win at casino slots.
You would see the application reflect those different signals, like below where a SIGTRAP was sent:
SleepService as systemd service
Turning this into a service for systemd requires that we create a unit service file at '/lib/systemd/system/sleepservice.service' like below:
The absolute paths in ‘ConditionPathExists', ‘WorkingDirectory', and ‘ExecStart' all need to be modified per your environment. Notice that we have instructed systemd to run the process as the user ‘sleepservice', so we need to create that user as well.
Below are instructions for creating the user and moving the systemd unit service file to the correct location:
Now, you should be able to enable the service, start it, then monitor the logs by tailing the systemd journal:
The journal is stored as a binary file, so it cannot be tailed directly. But we have syslog forwarding enabled on the systemd side, so now it is just a matter of configuring our syslog server.
For full instructions on configuring syslog on Ubuntu, read my article here. But here are quick instructions for Ubuntu 16.04.
First modify '/etc/rsyslog.conf' and uncomment the lines below which tell the server to listen for syslog messages on port 514/TCP.
Then, create '/etc/rsyslog.d/30-sleepservice.conf' with the following content:
Now restart the rsyslog service and you should see the syslog listener on port 514, restart the sleepservice, and now you should see log events being sent to the file every few seconds.
Listing the running processes shows that the process is running as the 'sleepservice' user.
Golang Ubuntu 19 04 19
Stopping the service will show that the SIGTERM signal was sent to the application and it cleaned up before stopping.
But, if you were to send a SIGINT signal (interrupt), notice that the service restarts because of the 'Restart=on-failure' we indicated in the service file (see Table1).
By default, the service will be run at boot time by the 'WantedBy=multi-user.target' setting, and there is a link under '/etc/systemd/system/multi-user.target.wants/'.
EchoService in foreground
Now let's move on to building a simple REST service that listens on port 8080 and responds to HTTP requests. Picsart update 2018. Below is a snippet of the main functionality which configures a router and handler:
Here is an example of building and running the service:
Which should produce output on the server that looks something like:
We can see that the server is listening on port 8080. So now moving over to a client host, we run curl against the '/hello' service (or use a browser), sending a parameter of 'foo'.
And on the server side the output looks like:
Golang Ubuntu 19 04 17
EchoService as systemd service
Turning this into a service for systemd requires that we create a unit service file at '/lib/systemd/system/echoservice.service' like below:
The absolute paths in ‘ConditionPathExists', ‘WorkingDirectory', and ‘ExecStart' all need to be modified per your environment. Notice that we have instructed systemd to run the process as the user ‘echoservice', so we need to create that user as well.
Golang Ubuntu 19 04 2017
Below are instructions for creating the user and moving the systemd unit service file to the correct location:
Now, you should be able to enable the service, start it, then monitor the logs by tailing the systemd journal:
The journal is stored as a binary file, so it cannot be tailed directly. But if we configure syslog, we have syslog forwarding enabled so that we can have our log sent to '/var/log/echoservice/echoservice.log'.
The sleepservice section above showed how to have rsyslog listen on port 514, so now we just need to create '/etc/rsyslog.d/30-echoservice.conf' with the following content:
Now restart the rsyslog service and you should see the syslog listener on port 514, restart the echoservice, and now you should see log events being sent to the file every few seconds.
Listing the running processes shows that the process is running as the 'echoservice' user.
Privileged Ports
In the above example, we have the echoservice listening on port 8080. But if we used a port less than 1024, special privileges would need to be granted for this to run as a service (or in the foreground for that matter).
The way to resolve this is not to run the application as root, but to set the capabilities of the binary. This can be done with setcap:
REFERENCES
systemctl daemon-reload
The Go project's official download page is at https://golang.org/dl.
After downloading a binary release suitable for your system, you can install go by following the official installation instructions at https://golang.org/doc/install.
There are some other options for Debian based systems like Ubuntu. 3utools icloud unlock. These packages were not created by the Go project, and we don't support them, but they may be useful for you.
If you're using Ubuntu 18.04 LTS or 20.04 LTS on amd64, arm64, armhf or i386, then you can use the longsleep/golang-backports PPA and update to Go 1.15.
Note that golang-go
installs latest Go as default Go. If you do not want that, install golang-1.15
instead and use the binaries from /usr/lib/go-1.15/bin
.
If that's too new for you, try: (Ubuntu 19.04 max)
Golang Ubuntu 19 04 Full
Note that golang-1.11-go
puts binaries in /usr/lib/go-1.11/bin
. If you want them on your PATH, you need to make that change yourself.
Fnaf ar gameplay. Using snaps also works quite well:
A restart may or may not be required for the command to be recognized depending on your system.
Using getgo (proof-of-concept command-line installer for Go):
Getgo will install the Go distribution (tools & stdlib) to '/.go' inside your home directory.