Reboot Linux box from user space

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

  • file systems synchronization
  • Don't try to gracefully stop processes, go to runlevel 5/6, etc., as I guess things are bad and it won't work

Can these requirements be achieved together at all?

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

Thanks!

0


a source to share


4 answers


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



This should do the trick I guess!

+2


a source


I am assuming you have root.

  • The sysrq-trigger thing is great, you just need to respond to it first, which is "Fail-sync".
  • You can also check the "-n" option to disable (8). It's a canned way of doing what you want. It just sends SIGTERM, SIGKILL, syncs and shuts down or reloads (-h or -r.)


Hope it helps.

+3


a source


This is 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 this myself and finally start a reboot with RB_AUTOBOOT.

+1


a source


On a regular distribution, the easiest way to do this is:

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

      

This will sync all filesystems and then reboot. Note that sysrq b will not sync.

0


a source







All Articles