在执行结束时执行操作 [英] do actions on end of execution

查看:174
本文介绍了在执行结束时执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个http服务器(使用 http.Handle 启动),我想做一些操作。



<我怎样才能做到这一点(在Linux上)?是否有可能在ctrl-C的情况下执行这些操作?



我不熟悉unix信号,因此答案可能很微不足道。 b $ b

解决方案

您可以使用 signal 包。但请注意,这些信号仅在进程明确死亡时才发送;正常退出(由进程本身启动)不涉及任何类型的信号。我认为正常退出只是在主程序中做一些事情(应该产生工人程序然后等待它们)。

阅读 man 7信号获取更多关于POSIX信号的一般信息。


I have an http server (launched using http.Handle) and I would like to do some operations.

How can I do that (on linux) ? Is it possible to do those operations in case of a ctrl-C ?

I'm not familiar with unix signals so the answer may be trivial.

解决方案

You can subscribe to the TERM and INT signals using the signal package. But note that these signals are only sent when the process is killed explicitly; normal exit (initiated by the process itself) does not involve any sort of signals. I think for normal exit just do something in the main routine (which supposedly should spawn worker goroutines and then wait on them).

Read man 7 signal for more general info on POSIX signals.

这篇关于在执行结束时执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆