In the era of cloud computing, rapid deployment can allow developers to quickly deliver products and respond to user needs quickly. golang has become an indispensable programming language in the cloud computing field in recent years, combining fast compilation and efficient concurrency performance. But what if the application crashes? In order to keep the application running continuously, we need to use the daemon to monitor and restart the application. This article will explain how to use systemd to deploy and guard golang applications.
1. Install and configure systemd
systemd is one of the most popular initialization and daemon management systems today. It is responsible for starting system services and daemons, and can monitor their status and restart when needed. It must be installed before using systemd.
Install systemd using the following command:
$ sudo apt-get update $ sudo apt-get install -y systemd
2. Create a service file
Before creating the daemon, we need to create a systemd service file. A service file is a text file containing information describing the service. The location of the service file is/etc/systemd/system/
In the directory. For example, we create a name calledService files:
$ sudo touch /etc/systemd/system/
editdocument:
$ sudo nano /etc/systemd/system/
Add the following to the file:
[Unit] Description=My golang app After= [Service] Type=simple User=ubuntu Group=ubuntu ExecStart=/usr/local/bin/golang-demo [Install] WantedBy=
in,
-
[Unit]
The section contains the service name and description, and when the service should be started (inback).
-
[Service]
The section describes the details of the startup service, including the user used, the group, and the location of the startup script file. -
[Install]
Partially specifies which target systemd should start the service.
3. Create golang application
Next, we need to create a golang application calledgolang-demo
. In terminal, clone a simple go application using the following git command:
$ git clone /shalar/
Go to the project directory and build and compile the application:
$ cd golang-demo $ go build
We need to add the generated binary filegolang-demo
Move to/usr/local/bin
in the directory.
$ sudo mv golang-demo /usr/local/bin/
4. Start the service and conduct testing
Now we are ready to start the service and test it. Start the service using the command:
$ sudo systemctl start
Check the status of the service:
$ sudo systemctl status
Seeactive (running)
, indicating that the service is running, you can access http://localhost:8080 through the browser, and you should be able to see the output.
5. Start up automatically
As mentioned earlier, in[Install]
In the section, we specify which target systemd should start the service. Now we can use the following command to start the boot automatically:
$ sudo systemctl enable
Now the system automatically starts the service every time it starts.
6. Conclusion
This article describes how to deploy and guard golang applications using systemd. systemd is a popular daemon manager that can easily manage the start, stop, restart and other operations of services, keeping our applications online at all times.
This is the article about how to use systemd to deploy and guard golang applications. For more related systemd deployment and guard golang content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!