4

I just migrated a web application from NodeJS to Go, and I am having troubles getting it to run on startup. Previously using my Node app, I would use pm2 to handle starting the app. However I cannot find a good way to do this with Go.

I have looked into making my own service with rc.d but I can't really find anything that helpful. Is there an easy way to do this? I can compile the source code to binary if need be.

At the moment I have it just running in a TTY, but I would really like it to be a daemon.

Edit: Just to clarify, I am using NGiNX to reverse proxy the Go app.

  • The nginx package is installed with its own /etc/rc.d/nginx file. What is the issue with enabling and starting this using rcctl enable nginx; rcctl start nginx as root? – Kusalananda Feb 08 '22 at 11:28
  • @they I already have NGiNX configured like that. However, the go application needs to be run on startup as well so NGiNX can reverse proxy it. – Rawley Fowler Feb 08 '22 at 16:39

2 Answers2

4

If you don't want to use the rc.d infrastructure (you should), you can just add whatever is appropriate to /etc/rc.local BUT remember that everything there is run as root, so you'll probably want to use su to run your application as another user (and while you're at it, check if you want to run it chrooted somewhere).

If you want to go the rc.d way, you might want to start by skimming throug the man pages for rc.d and rc.subr, and the template at /usr/ports/infrastructure/templates/rc.template might also be a big help. Yet another option is to find an already ported Go web application, install it and see how it is run. For instance gitea is installed as a compiled binary and then has a very simple /etc/rc.d/gitea script:

#!/bin/ksh
#
# $OpenBSD: gitea.rc,v 1.6 2021/01/17 21:07:41 pvk Exp $

daemon="/usr/local/sbin/gitea" daemon_user="_gitea"

. /etc/rc.d/rc.subr

rc_bg=YES rc_reload=NO

rc_cmd $1

Zé Loff
  • 2,112
0

I'm not really an OpenBSD guy, but don't you just need to make sure that httpd starts at boot?

SKull
  • 95
  • 1