-2

I have a grpc server(golang) which I want to start and stop via command line tool, after stopping the server it should perform some housekeeping tasks and exit the process.

  • I can do this by keeping a loop waiting for user input. Ex -
func main() {
    for {
        var input string
        fmt.Scanln(&input)
        //parse input
        // if 'start' execute - go start()
        // if 'stop' execute - stop() and housekeepingTask() and break
    }
 }

There can be different approaches. Is there any better idea or approach which can be used ?

I am looking for something similar how kafka/any db start and stop works. Any pointer to an existing solution or approach would be helpful.

Bishnu
  • 383
  • 4
  • 14
  • 1
    You need to [gRPC graceful shutdown](https://gist.github.com/akhenakh/38dbfea70dc36964e23acc19777f3869). – Mehran Prs Mar 20 '20 at 14:17
  • Services generally don't read from stdin because they're usually run as a daemon with no tty attached to receive input from. Usually you'd signal a graceful shutdown using process signals, e.g. HUP to reload config, TERM to shut down, etc. – Adrian Mar 20 '20 at 16:08
  • thanks for answering. I am struggling to execute this problem. Any help would be great – Bishnu Mar 20 '20 at 16:38

1 Answers1

-1

I got one of the correct approaches which was commented/answered by @Mehran also. But let me take a moment and answer it in detail -

This can be solved by Inter-Process-Communication. We can have a bash file that sends user signals to the program (if already running) and based on its process can act on it. (even we can have stdin file which process can read and act upon)

Some useful links - - https://blog.mbassem.com/2016/05/15/handling-user-defined-signals-in-go/ - Send command to a background process - How to signal an application without killing it in Linux?

I will try to add a working go program.

Bishnu
  • 383
  • 4
  • 14