CCates
CCates

Reputation: 1255

How to run a go app continously on an Ubuntu server

Couldn't seem to find a direct answer around here.

I'm not sure if I should run ./myBinary as a Cron process or if I should run "go run myapp.go"

What's an effective way to make sure that it is always running? Sorry I'm used to Apache and Nginx.

Also what are best practices for deploying a Go app? I want everything (preferably) all served on the same server. Just like how my development environment is like.

I read something else that used S3, but, I really don't want to use S3.

Upvotes: 1

Views: 1886

Answers (3)

viraptor
viraptor

Reputation: 34145

Use the capabilities your init process provides. You're likely running system with either Systemd or Upstart. They've both got really easy descriptions of services and can ensure your app runs with the right privileges, is restarted when anything goes down, and that the output is are handled correctly.

For quick Upstart description look here, your service description is likely to be just:

start on runlevel [2345]
stop on runlevel [!2345]
setuid the_username_your_app_runs_as
exec /path/to/your/app --options

For quick Systemd description look here, your service is likely to be just:

[Unit]
Description=Your service

[Service]
User=the_username_your_app_runs_as
ExecStart=/path/to/your/app --options

[Install]
WantedBy=multi-user.target

Upvotes: 5

petomalina
petomalina

Reputation: 2140

You may want to go for virtual terminal utility like screen here. Example:

screen -S myapp # create screen with name myapp
cd ... # to your app directory
go run myapp.go # or go install and then ./myappfrom go bin dir
Ctrl-a+d # to go out of screen

If you want to return to the screen:

screen -r myapp

EDIT: this solution will persist the process when you go out of terminal, but won't restart it when it'll crash.

Upvotes: 0

Felipe Arenales
Felipe Arenales

Reputation: 991

You can put it in an inifiny loop, such as:

#! /bin/sh
while true; do
  go run myapp.go
  sleep 2 # Just in case
done

Hence, once the app dies due some reason, it will be run again.

You can put it in a script and run it in background using:

$ nohup ./my-script.sh >/dev/null 2>&1 &

Upvotes: 0

Related Questions