8

How do you restart a service on Raspbian?

With most Linux distros I've used, it's simply:

sudo service <name> restart

But when I try to run:

sudo service ssh restart

on Raspbian, it hangs indefinitely. Does Raspbian have some non-standard way of interfacing with services?

Edit: Running sudo top -c shows that the process /sbin/init owned by root is consuming 100% CPU. If I kill this, this the service commands return immediately, but with a failed message like:

[raspberrypi] out: Warning! D-Bus connection terminated.
[raspberrypi] out: Failed to wait for response: Success

Why is /sbin/init behaving this way?

Cerin
  • 2,211
  • 7
  • 30
  • 49
  • How long it hangs? Mine seems normal whenever i'm restarting service. – xdhe Jun 13 '16 at 15:11
  • Me too, you must have a problem somewhere. – Thomas Kowalski Jun 13 '16 at 16:03
  • curiosity, why would you want to restart ssh ? – Max Muster Jun 13 '16 at 16:40
  • @eichertc If you reconfigure the server you need to restart it for the changes to take effect. – goldilocks Jun 13 '16 at 17:24
  • @eichertc, After I did a fresh install, I wasn't able to SSH in using the default login, so I logged in via a terminal, confirmed internet access was working. The only other thing I could think of was that the ssh service was stopped or not installed by default, which I've seen before. I confirmed it was installed, so I tried to start or restart the ssh service, and both commands hung for several minutes before I cancelled them. – Cerin Jun 13 '16 at 19:34

2 Answers2

7

With most Linux distros I've used, it's simply sudo service <name> restart...

This is because many or most linux distros are descended from Debian, which traditionally has used a "UNIX System 5" (aka. SysV) style init system. In fact, if you go back far enough all linux distros used this kind of system.

I am not sure whether the service command was part of the original SysV init or whether it was a convenience created by Debian, but it certainly has been around a long time and spread throughout the Debian family tree.

However, about a decade ago linux distributions began to move away from an init system that was increasingly considered inefficient and outdated in various aspects. This includes Ubuntu, which eventually came out with upstart, and Redhat/Fedora and derivatives, who developed systemd (where "d" is for daemon).

Eventually, Ubuntu decided to abandon upstart in favour of systemd, and Debian has done the same thing in version 8 by depreciating SysV. Raspbian, the operating system predominantly used on the Pi, is a very close derivative of Debian, uses the same version numbering, etc. (which is why the first version of Raspbian was 7, not 1), and hence has now also switched to systemd.

The service command should continue to work as Debian has kept version 8 (jessie) backward compatible in this sense, but you may want to try systemd's methodology instead:

sudo systemctl restart <name>

Note the command (restart) and the service name are inverted. Also, this will probably not provide any output but does indicate success or failure via the exit status (echo $?; anything other than 0 means fail). You can get some information about how things went with:1

systemctl status <name>

No sudo required as far as I have ever noticed. There should be a time there indicating when the service was stopped and started again and it will be very clear whether it is running, or whether it failed. In the latter case an error will usually be described. Note that a "dead" service has not necessarily failed, it just means it did whatever it was supposed to do then exited.

I believe doing this will better handle a problematic service which hung service.


1. I have a suggestion here about how to simplify this hassle.

goldilocks
  • 56,430
  • 17
  • 109
  • 217
3

I think that your problem may be that you are connecting to the Pi through ssh and expecting it to restart, the problem is when restarting the ssh process your ssh connection to that very same process is broken. If this is not the case and you are actually physically connected to the Pi with a monitor and keyboard then please elaborate on what you see when it hangs?

Mohammad Ali
  • 2,338
  • 1
  • 9
  • 17
  • You should be able to restart the ssh service via `ssh` without the connection dropping (try it). – goldilocks Jun 13 '16 at 16:16
  • @goldilocks I have tried it before, the connection does drop for me, but then certain ssh clients such as putty will attempt to reconnect and if successful will not notify the user that the connection had been closed and reopened. – Mohammad Ali Jun 13 '16 at 16:19
  • I don't use putty; I'm openSSH at both ends. It may be some aspect of how I have ssh or sshd configured although looking through that I have no idea what -- so perhaps it is some aspect of how *you* have it configured ;) – goldilocks Jun 13 '16 at 16:23
  • Just tried this with `sudo service ntp start` and it also hangs indefinitely. It seems like Raspbian's installation of Systemd is really screwy... – Cerin Jun 20 '16 at 05:17