How to deploy and guard golang applications using systemd
In the age of cloud computing, rapid deployment allows developers to deliver products quickly and respond quickly to user needs. golang has become an indispensable programming language in the cloud computing space in recent years, combining fast compilation and efficient concurrency. But what if the application crashes? To keep the application running continuously, we need to use daemons to monitor and restart the application. This article describes how to use systemd to deploy and guard golang applications.
Install and configure systemd
systemd is one of the most popular initialization and daemon management systems available today. It is responsible for starting system services and daemons, as well as monitoring their status and restarting them if necessary. Before you can use systemd, you must install it.
Install systemd with the following command:
$ sudo apt-get update $ sudo apt-get install -y systemd
2. Create service files
Before creating the daemon, we need to create a systemd service file. A service file is a text file that contains information describing a service. The location of the service file is in the /etc/systemd/system/ directory. For example, let's create a service file called golang-demo.service:
$ sudo touch /etc/systemd/system/golang-demo.service
Edit the golang-demo.service file:
$ sudo nano /etc/systemd/system/golang-demo.service
Add the following to the file:
[Unit] Description=My golang app After=network.target [Service] Type=simple User=ubuntu Group=ubuntu ExecStart=/usr/local/bin/golang-demo [Install] WantedBy=multi-user.target
Among them,
[Unit]
The section contains the service name and description, and when the service should start (innetwork.target
After).[Service]
The section describes the details of starting the service, including the users used, groups, and the location of the startup script file.[Install]
The section specifies the target on which systemd should start the service.
Create the golang application
Next, we need to create a golang application called Golang-demo. In terminal, clone a simple go application using the following git command:
$ git clone https://github.com/shalar/golang-demo.git
Go to the project directory and build and compile the application:
$ cd golang-demo $ go build
We need to move the generated binary golang-demo to the /usr/local/bin directory.
$ sudo mv golang-demo /usr/local/bin/
4. Start the service and test it
Now we are ready to start the service and test it. Use the command to start the service:
$ sudo systemctl start golang-demo.service
To view the status of the service:
$ sudo systemctl status golang-demo.service
If you see active (running), which means the service is running, you can go to http://localhost:8080 with your browser and you should see the output.
Five, boot from the start
As mentioned earlier, in the [Install] section, we specify which target systemd should start the service on. Now, we can use the following command to start up:
$ sudo systemctl enable golang-demo.service
Now, the system automatically starts the service every time it starts.
Vi. Conclusion
This article showed you how to deploy and guard golang applications using systemd. systemd is a popular daemon manager that makes it easy to manage the start, stop, restart of services, etc., to keep our applications always online.
This article on how to use systemd to deploy and protect golang applications is introduced to this, more related to systemd deployment and protect golang content please search script home previous articles or continue to browse the following related articles hope that you will support Script home in the future!
Related article
-
An example of an uncommon switch statement in golang is detailed
This article mainly introduces an uncommon switch statement writing method in golang, this article gives you a very detailed introduction through the example code, which has certain reference value for your study or work, and you can refer to the need of friends2023-05-05 -
Resolve the case when Golang json serializes strings with more \
This article mainly introduces the solution to Golang json serialized string when more \, has a good reference value, I hope to help you. Let's take a look2020-12-12 -
How to use go gin to manipulate cookies
Today, Xiaobian will share an explanation of using go gin to operate cookies for you. Xiaobian thinks the content is very good, and now it is shared with you, which has a good reference value. Friends who need to follow Xiaobian to see2019-04-04 -
Arrays and slicing of go features
This article mainly introduces the go feature of the array and slice problem, this article through the example code to give you a very detailed introduction, for everyone's study or work has a certain reference value, the need of friends can refer to the next2020-11-11 -
golang finds the maximum sum of instances of continuous subarrays
This article mainly introduces golang to find the maximum of continuous subarray and examples, has a good reference value, I hope to help you. Let's take a look2020-12-12 -
Process steps for developing hardware drivers using Go
Golang is a simple and efficient programming language, its powerful concurrency performance and rich standard library make it an ideal choice for the development of hardware drivers, in this article, we will explore how to use Golang to develop hardware drivers, and provide an example to help you get started, need friends can refer to the next2023-11-11 -
Golang tutorial non-reentrant function implementation method
This article mainly introduces the implementation method of the non-reentrant function of the Golang tutorial, and introduces it in great detail through the example code, which has certain reference value for everyone's study or work, and needs friends to study together with the Xiaobian below2018-09-09 -
Analysis of method set in Go language and choice of receiver type
This article mainly introduces the relevant knowledge of the Go language method set and the choice of receiver type in detail. The example code in this article explains it in detail, which is helpful for us to further learn the go language. If you need it, you can refer to it2023-11-11 -
Golang gorging implements a non-replicable type detail
In this article, we will implement a type that cannot be copied, while deepening our understanding of reference types, value passing, and Pointers. The example code in the article explains in detail, interested can understand2023-02-02
Latest comments