Golang services stops unexpectedly


(Vikram Singh) #1

I am working on an app integrated with REST API developed in golang, I have used gin framework and deployed api on aws ec2 instance.

What I do to run the api is as follows
1.Upload all files in src directory
2.Go get and go install brbapi ( brbapi is package/folder name )
3.start tmux and run “brbapi” ( using tmux to keep api running )

this start the service and I can use the rest api , but sometime ( once in a day ) the services stops unexpectedly without any error or log

Are there any ways to keep it alive and add some logging methods to get the failure logs ?


(Norbert Melzer) #2

tmux is not meant to run services, use systemd and setup a proper restart strategy.

Aside of that, I have some services running productively, all of them run 24/7, without any unexpected stops/exits.

My assumption is, that you trigger some path in your application that simply os.Exits.


(Vikram Singh) #3

Thanks Nobbz for your reply, I checked my code and os.Exists is not used anywhere. I was checking to use systemd as you suggested but didnt get where to start, as I am a windows users I am not much familiar with ubuntu and commands. Could you suggest me some tutorials
Also it would be good if you can explain how you are running your services 24/7


(Norbert Melzer) #4

Well, perhaps its not your code, but some other. Also it would help if you were checking the exit code. And without knowing anything about your program, your logging and how you start it, we can’t tell anything about where to look. Perhaps if you are not using os.Exit, you simply leave your main loop because of a true exit condition?

And how I run them 24/7? Well, as I said, I let systemd manage them. Or better, I write the software and our ops team provides the necessary systemd units, they also do all the monitoring integration and stuff.

If you do not know linux you probably shouldn’t target it. Or at least find someone who knows enough to help you on site, who is allowed to take a look at your sources and your runtime environment to be able to set up everything correctly.

There are far to many things to consider to do this over a webforum without knowing nothing about your software.


(Jay Ts) #5

Hi Vikram,

I agree completely with Norbert. You need to find someone who knows Linux well and can set it up for you.

There are other ways your Go program can terminate. Perhaps there is something in your code that triggers a runtime error. I suggest you look through your program and make sure to check for errors returned from library calls. If you detect an unrecoverable error conditions, print a log message and then maybe you can at lease exit gracefully. :smile:

If you are lucky, you will then have a log with at least one useful error message that helps you discover the problem.

If you are not doing so already, look at the log package, and make sure you understand the error interface, and that you are using those properly.


(Boban Acimovic) #6

I would first find out why does it stop, this must be fixable and your Go daemons should be able to run without stop/crash for a very long time.


(Gerben Jacobs) #7

To learn about systemd and Go apps that run 24/7 checkout this video by Francesc Campoy: https://www.youtube.com/watch?v=SQeAKSJH4vw