I have a Golang program implementing a web server. It is expected to be running continuously, and incase of any unexpected failure or crash, to restart itself. For that, I am trying to configure it as a UNIX-process using supervisord
. However, the issue I am facing is that the external go libraries included in the code aren't getting recognised as supervisord
is unable to recognise the GOPATH
. This is leading to errors such as:
web_server.go:11:2: cannot find package "github.com/gorilla/mux" in any of:
/usr/lib/go/src/github.com/gorilla/mux (from $GOROOT)
($GOPATH not set)
when running the web server using supervisord. The supervisord configuration for my web server is:
[program:web_server]
command=go run web_server.go
directory=/home/ubuntu
autostart=true
autorestart=true
startretries=5
stderr_logfile=/home/ubuntu/err_logs/web_server.err.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
What is the workaround for this?
One important property of supervisord
as stated here, that I had been missing out is:
Subprocesses will inherit the environment of the shell used to start the
supervisord
program. Several environment variables will be set by supervisord itself in the child’s environment also, includingSUPERVISOR_ENABLED
(a flag indicating the process is under supervisor control),SUPERVISOR_PROCESS_NAME
(the config-file-specified process name for this process) andSUPERVISOR_GROUP_NAME
(the config-file-specified process group name for the child process).These environment variables may be overridden within the [supervisord] section config option named
environment
(applies to all subprocesses) or within the per-[program:x]
section environment config option (applies only to the subprocess specified within the[program:x]
section).
Therefore, adding the GOPATH
in the environment
variable solved the issue.
[program:web_server]
command=go run web_server.go
directory=/home/ubuntu
autostart=true
autorestart=true
startretries=5
stderr_logfile=/home/ubuntu/err_logs/web_server.err.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
environment=GOPATH="/home/ubuntu"