Go Program still runs after stopped by ctrl+c

1,745 views
Skip to first unread message

anfernee...@gmail.com

unread,
Dec 29, 2015, 1:01:41 PM12/29/15
to golang-nuts
Hi all, 

I've build a program in go with the intention of having it run forever, until I kill the process. It does not spawn any daemons or anything of the sort; the program is just a forever loop that executes a command and then sleeps. I've noticed that even after stopping the program with ctrl+c, the process still runs. Even though this is my intention, I'm worried that I may be following bad practices. Thoughts on this? 

Jan Mercl

unread,
Dec 29, 2015, 1:10:42 PM12/29/15
to anfernee...@gmail.com, golang-nuts
> On Tue, Dec 29, 2015 at 7:01 PM <anfernee...@gmail.com> wrote:

> I've build a program in go with the intention of having it run forever, until I kill the process. It does not spawn any daemons or anything of the sort; the program is just a forever loop that executes a command and then sleeps. I've noticed that even after stopping the program with ctrl+c, the process still runs. Even though this is my intention, I'm worried that I may be following bad practices. Thoughts on this? 

Forever loop busy blocks the goroutine scheduler. Do not do that. Use for example a `select {}` instead. If you want to prevent ctrl-c to terminate your program, handle the signal.
--

-j

Anfernee Jervis

unread,
Dec 29, 2015, 2:21:10 PM12/29/15
to golang-nuts, anfernee...@gmail.com
I'm not sure how a select replaces a forever loop. How does one make a program loop forever, with a select? Any examples?

Bruno Albuquerque

unread,
Dec 29, 2015, 2:25:43 PM12/29/15
to Anfernee Jervis, golang-nuts
func main() {
    select{}
}


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shawn Milochik

unread,
Dec 29, 2015, 2:26:39 PM12/29/15
to golang-nuts
On Tue, Dec 29, 2015 at 2:21 PM, Anfernee Jervis <anfernee...@gmail.com> wrote:
I'm not sure how a select replaces a forever loop. How does one make a program loop forever, with a select? Any examples?

 
A select block can't replace an infinite loop. But you can put a select block inside an infinite loop.

What are you trying to do? If you describe the problem you're trying to solve instead of the symptom you're experiencing I'm sure we can suggest a better solution. What is the program meant to do, and why should it do it forever?




Message has been deleted

Jan Mercl

unread,
Dec 29, 2015, 2:40:03 PM12/29/15
to Anfernee Jervis, golang-nuts

> On Tue, Dec 29, 2015 at 8:21 PM Anfernee Jervis <anfernee...@gmail.com> wrote:

> I'm not sure how a select replaces a forever loop. How does one make a program loop forever, with a select? Any examples?

Without any code shown, I assumed, probably wrongly, the reference to "forever loop" is something like

        func main() {
                whateverSetupAndPerform()
                for {}
        }

ie. the for statement used to prevent main from exiting, because. 1) It's seen (too) often. 2) It can prevent a signal handler to get scheduled (I think and for GOMAXPROCS=1 only).
--

-j

Anfernee Jervis

unread,
Dec 29, 2015, 2:41:18 PM12/29/15
to golang-nuts, Sh...@milochik.com
I would just like to understand why the process is still running after terminating it with Ctrl+c. I was told here about a forever loop being a bad practice and that I should use a select instead. I knew not of a way to use a select to loop forever, only of a way to block forever with a select, which is why I was asking how do you replace a for loop with a select. Don't get me wrong, I appreciate your concern bit I'm not here for a solution. Just an answer. 

for reference, my program needs to repeatedly execute a statement and sleep for a period. What I currently have for this is:

for {
    //exec statement
    time.sleep(//someperiod)

Anfernee Jervis

unread,
Dec 29, 2015, 2:42:59 PM12/29/15
to golang-nuts, anfernee...@gmail.com
I figured this was the case. I should have been a bit more descriptive. 

Anfernee Jervis

unread,
Dec 29, 2015, 3:07:12 PM12/29/15
to golang-nuts
Seems like the answer to this question is environment based, and doesn't exactly have anything to do with Go. 
Reply all
Reply to author
Forward
0 new messages