如何让一个进程在linux中重新加载自身?

发布于 2024-07-14 13:47:41 字数 344 浏览 7 评论 0原文

我有一个服务,比如 foo,用 C++ 编写,以 root 身份运行。 有常用的脚本 /etc/init.d/foo start|stop|restart。

在某些时候,foo 需要重新加载自身。 通常在升级完成后。 但是做这样的事情

system("/etc/init.d/foo restart")

是行不通的,因为一旦重新启动杀死了 foo,system() 调用显然也会被杀死,并且重新启动脚本永远不会执行完成。

我是否可以使用另一个调用来代替 system() 作为调用进程的同级进程异步运行,而不是创建同步子进程?

谢谢!

I have a service, say foo, written in C++, that runs as root. There is the usual scrip, /etc/init.d/foo start|stop|restart.

At certain times, foo needs to reload itself. Normally after an upgrade has finished. But doing things like:

system("/etc/init.d/foo restart")

doesn't work since as soon as restart kills foo, the system() call obviously gets killed as well, and the restart script never executes to completion.

Is there another call I can use instead of system() that runs asynchronously as a sibling to the calling process, instead of creating a synchronous child?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

椵侞 2024-07-21 13:47:41

您考虑过 exec[*] 系列吗? 这是一个 - execve

Have you considered the exec[*] family yet? Here's one -- execve.

╰◇生如夏花灿烂 2024-07-21 13:47:41

您可以将其放在 inittab 中,让 init 负责在进程因任何原因退出时重新启动该进程。 如果您的进程发生崩溃或断言()或意外退出,这也将处理自动重新启动。

然后,为了处理您的用例,该进程将自行终止。

You could put it in inittab and let init worry about restarting the process when it exits for any reason. That would also take care of automatic restarts if your process happens to crash or assert() or otherwise exit unexpectedly.

Then, to handle your use case, the process would simply terminate itself.

痞味浪人 2024-07-21 13:47:41

fork() 后面跟着 exec() 怎么样?

how about fork() followed by exec()?

丶视觉 2024-07-21 13:47:41

考虑

/etc/init.d/foo reload

为你的守护进程实现(通过我的 Debian 盒子的 grep 判断,这是相当标准的)。

这通常是通过向进程发送 SIGHUP 信号来完成的;
守护进程应该有一个信号处理程序来捕获这个
并重新加载任何配置。

如果进程知道需要重新加载,它可以向自己发出信号。

如果您确实需要重新启动以获取新库,请使用 exec*()

Consider implementing

/etc/init.d/foo reload

for your daemon (pretty standard judging by a grep of my Debian box).

This is typically done by sending the process a SIGHUP signal;
the daemon should have a signal handler which catches this
and reloads any configuration.

If the process knows it needs to reload it can just signal itself.

If you really do need to restart to pick up new libraries then go with exec*()

昔梦 2024-07-21 13:47:41

结合到目前为止的两个答案,使用 fork-exec

Combining the two answers so far, use a fork-exec.

贪了杯 2024-07-21 13:47:41

补充一下 Ori 已经说过的,一些 Linux 发行版仍然使用 initab,但 Ubuntu 和其他可能的发行版已经切换到 /etc/event.d。 您将一个文件放入其中(复制并编辑现有文件之一),然后使用“sudo start ssh_tunnel”或任何您的文件名称启动守护进程。

然后当你需要它重新启动时,你只需用一个信号杀死它,系统就会重新启动它。 或者它可以通过调用“exit(0);”自行决定是时候重新启动了。 管他呢。

To add to what Ori has already said, some Linux distros still use initab, but Ubuntu and possibly others have switched to /etc/event.d. You put a file in there (copy and edit one of the existing ones) then start the daemon with "sudo start ssh_tunnel" or whatever your file is called.

Then when you need it to restart, you can just kill it with a signal, and the system will restart it. Or it could decide on its own that it's time to restart, by calling "exit(0);" or whatever.

白昼 2024-07-21 13:47:41

在原始命令行上单独使用 exec*() 就可以解决问题。 您可能可以省略 fork,因为您有两个不需要的副本正在运行,然后原始副本需要退出。

但还要根据您的发行版查看 inittab 和 event.d,看看它是否能以更好的方式满足您的需要。

exec*() by itself on the original command line should do the trick. You can probably omit the fork, `cause then you have two copies running which you don't need, and the original copy then needs to exit.

But also look at inittab and event.d depending on your distro, to see if it will do what you need in a better way.

被翻牌 2024-07-21 13:47:41

Ori 和 Paul 建议的第三种可能性是使用 daemontools。 它更便携,但不太可能可用。 您创建一个名为 /service/foo/run 的脚本,并且 daemontools 会在服务退出时重新生成您的服务。

http://cr.yp.to/daemontools.html

A third possibility to what Ori and Paul have suggested would be to use daemontools. It is more portable though less likely to be available. You create a script called /service/foo/run and daemontools will respawn your service whenever it exits.

http://cr.yp.to/daemontools.html

青丝拂面 2024-07-21 13:47:41

查看 inittab 的手册页。

它描述了如果进程死亡(重生),系统将如何自动重新启动进程。

正确设置后,您的服务所要做的就是退出,系统将自动为您重新启动(重生)。

Look at the man page for inittab.

It describes how the system will auto restart your processes if it dies (respawn).

With this set up correctly all you service has to do is exit and the system will take care of the re-start (respawn) for you auto-magically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文