从用户空间重新启动 Linux 盒子

发布于 2024-07-20 00:27:44 字数 223 浏览 8 评论 0原文

我需要从用户空间进程重新启动 Linux 机器。 我希望有以下行为:

  1. 同步文件系统
  2. 不尝试正常停止进程,转到运行级别 5/6 等,因为我认为事情很糟糕并且会失败

这些要求可以一起实现吗?

我打算在 proc/sysrq-trigger 中写入一个“b”。 有没有更好的办法? (不,那不会让我同步。)

谢谢!

I need to reboot a Linux box from a user space process. I want to have the following behavior:

  1. sync file systems
  2. not attempt to gracefully stop processes, go to runlevel 5/6, etc., for I assume things are bad and that would fail

Can those requirements be achieved together at all?

I was planning to write a 'b' into proc/sysrq-trigger. Is there a better way? (No, that won't give me sync.)

Thanks!

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

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

发布评论

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

评论(4

徒留西风 2024-07-27 00:27:44

我假设你有root权限。

  • sysrq-trigger 的事情很好,你只需要先回显 's' 几次,这是一个“紧急同步”。
  • 您还可以查看 shutdown(8) 的“-n”选项。 这是一种做你想做的事的固定方式。 它只是发送 SIGTERM、SIGKILL、同步并关闭或重新启动(-h 或 -r 选项。)

希望有所帮助。

I'm assuming you have root.

  • the sysrq-trigger thing is fine, you just have to echo 's' a couple times into it first, which is an "Emergency Sync".
  • You can also check out the '-n' option to shutdown(8). That's a canned way to do what you want. It just sends SIGTERM, SIGKILL, syncs and shuts down or reboots (-h or -r option.)

Hope that helps.

冰魂雪魄 2024-07-27 00:27:44

好吧,执行sync(1),kill -1,再次同步,kill -9 -1,sync,poweroff -f。

我想这应该可以解决问题!

Well, do sync(1), kill -1, sync again, kill -9 -1, sync, poweroff -f.

That should do the trick, I guess!

撕心裂肺的伤痛 2024-07-27 00:27:44

这直接来自 sys/reboot.h :

#ifndef _SYS_REBOOT_H
#define _SYS_REBOOT_H   1

#include <features.h>

/* Perform a hard reset now.  */
#define RB_AUTOBOOT     0x01234567

/* Halt the system.  */
#define RB_HALT_SYSTEM  0xcdef0123

/* Enable reboot using Ctrl-Alt-Delete keystroke.  */
#define RB_ENABLE_CAD   0x89abcdef

/* Disable reboot using Ctrl-Alt-Delete keystroke.  */
#define RB_DISABLE_CAD  0

/* Stop system and switch power off if possible.  */
#define RB_POWER_OFF    0x4321fedc

__BEGIN_DECLS

/* Reboot or halt the system.  */
extern int reboot (int __howto) __THROW;

__END_DECLS

#endif  /* _SYS_REBOOT_H */

我相信 RB_HALT_SYSTEM 将处理所有sync()等。我通常自己这样做,最后用 RB_AUTOBOOT 触发重新启动。

This directly from sys/reboot.h :

#ifndef _SYS_REBOOT_H
#define _SYS_REBOOT_H   1

#include <features.h>

/* Perform a hard reset now.  */
#define RB_AUTOBOOT     0x01234567

/* Halt the system.  */
#define RB_HALT_SYSTEM  0xcdef0123

/* Enable reboot using Ctrl-Alt-Delete keystroke.  */
#define RB_ENABLE_CAD   0x89abcdef

/* Disable reboot using Ctrl-Alt-Delete keystroke.  */
#define RB_DISABLE_CAD  0

/* Stop system and switch power off if possible.  */
#define RB_POWER_OFF    0x4321fedc

__BEGIN_DECLS

/* Reboot or halt the system.  */
extern int reboot (int __howto) __THROW;

__END_DECLS

#endif  /* _SYS_REBOOT_H */

I believe RB_HALT_SYSTEM will handle all sync()s , etc. I usually do that myself, and finally trigger the reboot with RB_AUTOBOOT.

谈下烟灰 2024-07-27 00:27:44

在普通发行版上,最简单的方法是:

system("/sbin/reboot -f");

这将同步所有文件系统,然后立即重新启动。 请注意,sysrq b 不会同步。

On an ordinary distro, the simplest way to do this is:

system("/sbin/reboot -f");

This will sync all filesystems, then reboot immediately. Note that sysrq b will NOT sync.

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