#make go program restart itself
34 messages · Page 1 of 1 (latest)
yes, when you press ctrl+c on the terminal, your shell send sigterm to your program.
if your program didn't not handle it, the kernel will take over and terminate your app
https://pkg.go.dev/os/signal
It's built in.
You have lots of options in how you use it.
You can restart from any point in your program based on how you process a signal.
The details depend largely on your application.
Another thing that you may be looking for is systemd, but that's not a Go thing. Depends on your deployment environment
yes. but also it'll take hours to explain what os signal is.
Your best option is to read the package documentation and come back with questions. We may not have answers but we can also point you towards other resources.
I'd ask: Do you want to restart your whole application or do you want to start executing the same application code from the beginning
True, depending on that answer signal or systemd may be overkill.
I mean.
You can just... Call main, right?
I've never tried it.
It's a function though
Why not?
isn't it recursive?
Second question: what application do you want to restart. Your parent or some child process you spawned?
what function do i use
https://go.dev/pkg/os/signal@NotifyContext
could you give an example
the example already exists in the package doc
this topic is related to "graceful shutdown"
that will most likely lead to a stack overflow?
I wouldn't call main inside main.
It may work now but I'd be concerned it won't scale, as a pattern.
But whether you need it to scale is a function of what you're doing with your project.
with enough restarts?
you would need to put your whole main() code in a separate function
and put a loop around it
stack in go is expandable, runtime will expand it as necessary. your limit is your memory cap.
mark as solved