4

If you look at Nginx it calls "nginx reload" to reload itself. Is there any way to send a signal from the command line to a running process? Even if the main process starts child processes how can I send commands to the main to notify its children?

ex:

myapp start -debug // starts a server
myapp reload -gracefull // stops the app gracefully

Now i need to send os signals to notify my server to perform a graceful shutdown

kill -QUIT pid
kill -USR2 pid

I hope my question is clear enough Thnx

2 Answers 2

7

Receive signals

Take a look at the os/signal package.

Package signal implements access to incoming signals.

There is even an example in the documentation :

// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)

// Block until a signal is received.
s := <-c
fmt.Println("Got signal:", s)

Send signals

To see how to send signals take a look at signal_test.go, it uses syscall. For example :

// Send this process a SIGHUP
t.Logf("sighup...")
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
waitSig(t, c, syscall.SIGHUP)
Sign up to request clarification or add additional context in comments.

6 Comments

I know this and i use this in my program. But i want it to send a signal to its own process from the commandline
and if my program is already running and i want to send it a sig from its own cli? app -quit. its a different pid right?
@AnthonyDeMeulemeester I guess you can store the pid in a local file or in an environment variable.
I call the same binairy with its cli but they dont share os.Env
@AnthonyDeMeulemeester Do they share the same file system? When binary starts write PID number to a PID file, then when you want to reload read PID file for PID number.
|
0

I figured out that in go i we can pass the environment to syscall.Exec

err := syscall.Exec(argv0. os.Args. os.Environ())

simply copies the current env to the child process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.