How to deploy and guard golang applications using systemd _Golang_ Scripting Home

How to deploy and guard golang applications using systemd

Updated: October 23, 2023 14:55:49 Submission: mrr
systemd is a popular daemon manager that can easily manage the start, stop, restart and other operations of the service, so that our applications always stay online, this article describes how to use systemd to deploy and guard golang applications, interested friends take a look at it

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.targetAfter).
  • [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

  • golang中一种不常见的switch语句写法示例详解

    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 friends
    2023-05-05
  • 浅谈Golang是如何读取文件内容的(7种)

    How Golang reads files (7)

    This article mainly introduces how Golang reads the content of the file, the article introduces very detailed through the example code, which has a certain reference value for everyone's study or work, and the friends who need to study together with the small series below
    2020-05-05
  • 解决Golang json序列化字符串时多了\的情况

    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 look
    2020-12-12
  • 使用go gin来操作cookie的讲解

    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 see
    2019-04-04
  • go特性之数组与切片的问题

    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 next
    2020-11-11
  • golang求连续子数组的最大和实例

    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 look
    2020-12-12
  • 使用Go开发硬件驱动程序的流程步骤

    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 next
    2023-11-11
  • Golang教程之不可重入函数的实现方法

    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 below
    2018-09-09
  • 浅析Go语言中的方法集合与选择receiver类型

    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 it
    2023-11-11
  • Golang拾遗之实现一个不可复制类型详解

    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 understand
    2023-02-02

Latest comments