PM2-like process management solution for Golang applications?
Asked Answered
T

1

16

Does Go have a process manager similar to PM2 for NodeJS?

Basic features of PM2:

  • Run application in background indefinitely, such as a server waiting for a request.
  • Restart application upon reboot.

Editor's note: PM2 offers an easy way to run a NodeJS application in the background forever, such as for a production server. Of course you can do this with the Linux operating system, using tools not specific to any particular programming language, and those answers are helpful. As Go can create executables, you don't really need a Go-language specific solution to this question.

Tojo answered 20/9, 2019 at 5:15 Comment(10)
It's unclear what you mean when you say manage the go process—go ships its own cooperative scheduler for scheduling go routines. If you're talking about managing multiple go applications, then maybe take a look at gokit.ioUnderglaze
Sorry, I don't know how to discribe that clearly, there is a situation, I run the go file, I don't t want to it's stoped when I close the terminalTojo
linux.die.net/man/1/nohupGeodesic
You might be describing a daemon/service. On Ubuntu/Debian/Redhat systemd can be used.Underglaze
I see, I will have a try later, thank youTojo
After the edit, the question is clear, but also clearly off-topic :)Lactic
Regarding 'Post Closed as "Needs details or clarity"' I tried to add more clarity. Maybe this is really a duplicate of an existing Linux / Server Fault question. However, I think a lot of people new to Go, or evaluating Go, they'll end up here searching for PM2, Go, etc. In other words, even though "Go" is irrelevant to the answer, people might not realize that right away.Spital
Maybe a duplicate of serverfault.com/questions/905535/…Spital
Also I found you can use any language or binary with PM2 pm2.keymetrics.io/docs/usage/process-management/…Spital
Check with github.com/struCoder/pmgo the pmgoLycaon
M
43

Development Environment

For development, you'd probably need process manager that also monitor the file changes and live-reload your server binary.

I'm used to Godegansta's gin for such job for web server / api server development. There is also fresh, reflex and perhaps some others.


Production Environment

I'm using systemd to manage my Golang application process on Linux in production environment.

Define the Unit

My Unit File looks like this:

[Unit]

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/usr/local/bin/<MY_GO_APP>
WorkingDirectory=/home/user/<MY_GO_APP_HOME_DIR>
User=<MY_GO_APP_USER>
Restart=always
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=%n

Create this file as /etc/systemd/system/my_app.service, then run:

systemctl start my_app.service

would automatically start the service. As configured, systemd will always restart your process if it stopped.

Usual Operations

To have it always on when the machine starts:

systemctl enable my_app.service

If you change your unit file after the first start or enable, you need to run:

systemctl daemon-reload

To see the status of the process, run:

systemctl status my_app.service

To see the STDOUT of the process, run:

journalctl -f -u my_app.service

For further help, please read the manual page.

Manciple answered 20/9, 2019 at 6:9 Comment(8)
It works, thank you, it helps me a lot.Tojo
For me, I needed to remove the line StopWhenUnneeded=true. With this line included, the process executed and exited.Pfosi
@SametSahin: That's strange. The unit multi-user.target should be active by default in most system. What's your operating system? And did you have some unusual setup on it?Manciple
It's Ubuntu 18.04 LTS. It actually becomes active after starting it but it terminates right after it executes. I solved this by removing StopWhenUnneeded=true.Pfosi
Interesting information. I'll update the unit here so it would work for other people. Thanks.Manciple
Check with github.com/struCoder/pmgo the pmgoLycaon
@Lycaon That is what I want to give it a try. But, can it work on Windows ?Irremediable
This is very clever solution. I am using docker-composer, but I think I might try this in the future.Ravenna

© 2022 - 2024 — McMap. All rights reserved.