Is running "reboot" command enough to reliably restart the Raspian (Debian) system if filesystem/hdd is ok?
From the man page for reboot:
When called with --force or when in runlevel 0 or 6, this tool invokes the reboot(2) system call itself and directly reboots the system. Otherwise this simply invokes the shutdown(8) tool with the appropriate arguments.
Assuming you don't fall into the first category (i.e. most normal cases of using the reboot command, assuming you don't pass the --force
flag), shutdown
will be invoked to gracefully bring the system down. This includes cleanly unmounting file systems and so forth, so you shouldn't have a problem.
Will it work when there is an infinite loop in stop() function of rc script or if there are some defunct processes, 0 space left on root fs or almost zero free RAM?
The Linux kernel is pretty good at terminating user-space processes which have hung or are occupying the processor in an infinite loop. If you watch the screen as you bring a Debian system down, you can occasionally see some red error messages when an application had to be force closed as part of the shutdown process.
Your mileage may vary if you have very little system RAM remaining, as this might prohibit you launching the reboot
command in the first place. But this all depends on what you did to eat up all the system memory, and why you cannot back out of this situation before attempting to force a reboot.
You can have issues if you have been messing around in the kernel to the extent that the kernel cannot kill itself, but that is to be expected. A failed kernel module which holds the CPU in an infinite loop, for instance, will often not be receptive to forced killing during an attempted reboot.
I know I can do something like this to restart reliably but I am not sure if it's safe from filesystem point of view
The code you presented will work (see the first comment on this post), but as noted there, seems to be a little superfluous.
Ultimately, if you are using this system for development, you should certainly be taking appropriate backups of any critical data so you can recover more easily from a file-system crash, rather than worrying about how to avoid the crash in the first place.