'How to run golang script in PM2?

How do you auto start golang scripts in PM2 on Ubuntu 20.04? this is how I run golang scripts:

./rimgo

I tried pm2 start "./rimgo" --name rimgo but it didn't work. What is the correct command?



Solution 1:[1]

I already run a Golang script. Check this steps:

  1. go build main.go For to build the script. This must generate a main binary file.
  2. Grant execution permission for this file with sudo chmod +x main
  3. Try to execute from command line with ./main
  4. Update the pm2 to latest & type pm2 start ./main --name FooBar

Solution 2:[2]

you can follow this steps :

  1. Create you binary using go build main.go
  2. give execution permission to the binary using sudo chmod +x main.
  3. create a package.json file:
{
    "apps" : [{
      "name"        : "main",
      "script"      : "./main"
    }]
}

and finally:

execute :

pm2 start package.json

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alambertt
Solution 2 rassakra