1

I have been looking for a way to reboot the rpi once a day or week gracefully. With gracefully I mean that the screens get stopped wih a Ctrl + C command (^C) and then it waits for the screen to be closed.

I couldn't find this anywhere. Is this even possible?

Tvde1
  • 113
  • 3
  • 1
    why are you wanting to reboot on an interval in the first place? – Chad G Aug 02 '18 at 15:06
  • That shouldn't be a problem. But I run multiple servers and I want to reboot just in case they get stuck and they do a git pull every time they are started. – Tvde1 Aug 03 '18 at 09:02
  • A watchdog on the specific process would be better then a blind reboot. And the git pull could be done on an interval – Chad G Aug 03 '18 at 15:51

1 Answers1

1

Presuming you are using a systemd based OS (as current versions of Raspbian are):

systemctl reboot

This requires root privilleges, so use the system crontab, not the Pi user's.

More generically, on any GNU/Linux distro:

reboot

See also man reboot. If instead you want to shutdown and cycle the power to restart (the title of your question makes this ambiguous), use halt -p or systemctl poweroff.

With gracefully I mean that the screens get stopped wih a Ctrl + C command (^C) and then it waits for the screen to be closed.

Note that if this, and cycling the power, is all you are doing currently it is not a graceful shutdown -- it is the same as randomly pulling the plug.

Also note that the reboot and poweroff commands trigger a system shutdown which kills all processes -- Ctrl-C also kills a process and its children, but not all the other software on the system including the OS kernel. However, if someone is actively using the system a shutdown does not wait for them to finish. If by "screens" you just mean displays, they will exit more or less the same way as Ctrl-C (but there may be some additional text posted as the system shuts down, and as it reboots).

A shutdown does wait for all process to stop, but what "wait" means is it sends a polite stop request (the same one Ctrl-C sends) first, and if after some time (possibly 90 seconds on Raspbian) if it doesn't stop, it forcefully kills the process.

I couldn't find this anywhere.

Probably because your search terms included the brand of hardware ("Raspberry Pi", which is actually irrelevant) and not the operating system ("Linux"; the OS is what performs a reboot). In addition to distinguishing between hardware and software, always search starting with the most general terms, not the most specific. Linux has been around for decades and used on tens or hundreds or hundreds of millions of systems, whereas the Raspberry Pi is a relatively obscure device.

There are sometimes significant differences between GNU/Linux distributions. In this case, if you are using Raspbian, you are interested in information related to Debian, from which it is derived.

While "Linux" by a strict definition refers only to the OS kernel, which is also used e.g., on Android (otherwise a very different OS), it is colloquially used to refer to GNU/Linux systems (GNU being the organization behind the core userland used on all distros including Debian and Raspbian; this is what makes these all very similar).

goldilocks
  • 58,859
  • 17
  • 112
  • 227
  • And this will send a ^C command to the screens I have open? I don't think you read my question right. – Tvde1 Aug 02 '18 at 13:24
  • Nothing as primitive as ctrl+C. It actually requests all processes to end. – Dirk Aug 02 '18 at 13:28
  • Well, anything short of this is not "graceful", although it may appear to be so -- if you just exit an interface and cycle the power, you are not performing a proper shutdown and significantly increase the risk of filesystem corruption. However, using this doesn't really wait for any particular interface to stop, except in the sense of killing it (which is what ctrl-C does)...I'll edit a bit in about that. – goldilocks Aug 02 '18 at 13:36
  • @Tvde1 Okay, I've tried to explain about this above under "With gracefully I mean...". In short, yes, a clean shutdown sends the same signal as Ctrl-C does, but to all processes. – goldilocks Aug 02 '18 at 13:55
  • Oh it does? Great! – Tvde1 Aug 02 '18 at 14:04