简体   繁体   English

如何通知守护进程改变行为

[英]How to signal daemon process to change behaviour

I've written a Java application that is launched as a daemon (I daemonize redirecting stderr and stdout and closing stdin though bash). 我编写了一个作为守护进程启动的Java应用程序(我守护进程重定向stderr和stdout并通过bash关闭stdin)。 However, occasionally I would like to be able to message this application and inform it to change certain parts of its behavior. 但是,偶尔我希望能够发送此应用程序并通知它更改其行为的某些部分。 I need to be able to message the application from a terminal, so anything that requires a graphical utility is a no-go. 我需要能够从终端向应用程序发送消息,因此任何需要图形实用程序的东西都是禁止的。

The change in behaviour is fairly simple. 行为的改变相当简单。 I need a toggle for the state of one thread in my application, and a way to gracefully close the application. 我需要切换应用程序中一个线程的状态,以及一种优雅地关闭应用程序的方法。

What are my options in achieving this? 实现这一目标有哪些选择? I know I could have a thread in the process that listens on a socket of some sort for messages, but this seems like overkill for the needs I have. 我知道我可以在进程中有一个线程来监听某种类型的套接字消息,但这对于我的需求来说似乎有些过分。

I'm not too familiar with Signals on Linux/Unix, so I'd like to ask if I could simply set up a custom handler for some signals, and have my code execute when the process receives a signal. 我不太熟悉Linux / Unix上的Signals,所以我想问一下我是否可以简单地为某些信号设置自定义处理程序,并在进程收到信号时执行代码。

Are there any other options that I simply don't know about or haven't thought about? 还有其他任何我根本不知道或没有想过的选择吗?

Signals may be the easiest. 信号可能是最简单的。 You have SIGUSR1 and SIGUSR2 available for application use and you can write a handler for them to manage your toggle. 您可以使用SIGUSR1和SIGUSR2供应用程序使用,您可以为它们编写处理程序来管理切换。 Generally you don't want to do much in a handler so you can set a switch and your main loop (or whatever) needs to check the switch and act accordingly. 通常你不想在处理程序中做很多事情,所以你可以设置一个开关,你的主循环(或其他)需要检查开关并相应地采取行动。 Similarly the receipt of the signal could be programmed to read a config file that you changed. 类似地,可以对信号的接收进行编程以读取您更改的配置文件。

Beyond that you can use any available IPC (fifos, sockets, MQs) but you are going to either have thread blocking on them or somehow incorporate them a select statement (or whatever the java equivalent of that is). 除此之外,您可以使用任何可用的IPC(fifos,套接字,MQ),但是您要么对它们进行线程阻塞,要么以某种方式将它们合并为select语句(或者与Java相当的任何语句)。

Personally, I like to use OS signals for this. 就个人而言,我喜欢使用OS信号。

Use java.lang.Runtime.addShutdownHook and send a SIGTERM, SIGINT or SIGHUP. 使用java.lang.Runtime.addShutdownHook并发送SIGTERM,SIGINT或SIGHUP。

Be aware that some signals are reserved for internal use, check out the docs for the JVM you are using. 请注意,某些信号保留供内部使用,请查看您正在使用的JVM的文档。

The SignalHandler is not part of the supported, public interface, so your mileage may vary. SignalHandler不是受支持的公共界面的一部分,因此您的里程可能会有所不同。

If you are instantiating a JVM from C code, simply set signal handlers (in C) before the JVM and you will be fine. 如果要从C代码实例化JVM,只需在JVM之前设置信号处理程序(在C中),就可以了。

You basically need a small client that communicates with your application (server). 您基本上需要一个与您的应用程序(服务器)通信的小型客户端。 So the default solution should be to use some IPC mechanism. 所以默认的解决方案应该是使用一些IPC机制。 Putting an IPC mechanism is a one time effort and is worth the trouble because it scales well with the requirements. 建立IPC机制是一次性的努力并且值得麻烦,因为它可以很好地满足要求。 Using signals for IPC is not recommended. 建议不要使用IPC信号。 I think sockets is a good way to go. 我认为插座是一个很好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM