14

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.

PJ Brunet
  • 3,615
  • 40
  • 37
quzhen
  • 303
  • 1
  • 2
  • 7
  • 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 https://gokit.io – Jonas G. Drange Sep 20 '19 at 05:28
  • 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 terminal – quzhen Sep 20 '19 at 05:39
  • 2
    https://linux.die.net/man/1/nohup – Peter Sep 20 '19 at 05:47
  • 1
    You might be describing a daemon/service. On Ubuntu/Debian/Redhat `systemd` can be used. – Jonas G. Drange Sep 20 '19 at 05:49
  • I see, I will have a try later, thank you – quzhen Sep 20 '19 at 05:56
  • After the edit, the question is clear, but also clearly off-topic :) – Jonathan Hall Sep 20 '19 at 07:31
  • 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. – PJ Brunet May 27 '21 at 05:12
  • Maybe a duplicate of https://serverfault.com/questions/905535/what-to-use-instead-of-pm2-for-non-node-js-applications – PJ Brunet May 29 '21 at 04:44
  • Also I found you can use any language or binary with PM2 https://pm2.keymetrics.io/docs/usage/process-management/#binary-code-execution – PJ Brunet May 29 '21 at 04:49
  • Check with https://github.com/struCoder/pmgo the pmgo – SebastianX Jul 11 '21 at 12:57

1 Answers1

36

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.

Koala Yeung
  • 7,475
  • 3
  • 30
  • 50