systemd
gives us the systemctl
commands suite which is mostly used to enable services to start at boot time. We can also start, stop, reload, restart and check status of services with the help of systemctl
.
We can do, for example, sudo systemctl enable service_name
, and service_name
will automatically start at boot time. We can also disable services not to start at boot time.
Is the only difference between the service
and systemctl
commands that systemctl
can be used to enable the start of services at run time? Can we use systemctl
on any service? What other significant differences are there?
Best Answer
The
service
command is a wrapper script that allows system administrators to start, stop, and check the status of services without worrying too much about the actual init system being used. Prior to systemd's introduction, it was a wrapper for/etc/init.d
scripts and Upstart'sinitctl
command, and now it is a wrapper for these two andsystemctl
as well.Use the source, Luke!
It checks for Upstart:
If that doesn't work, it looks for systemd:
And if that fails as well, it falls back to System V
/etc/init.d
scripts:Since the
service
command is a fairly simple wrapper, it only supports a limited subset of actions compared to what the actual init system might provide.For portability over various versions of Ubuntu, users can reliably use the
service
command to start, stop, restart or examine the status of a service. For more complex tasks, however, the actual command being used, be thatinitctl
orsystemctl
or the/etc/init.d
script might have to be used directly.Further, being a wrapper, the
service
script in some cases also does more than the direct equivalent command might do. For example:/etc/init.d
scripts in a clean environment. (Note the longenv
command invocation in therun_via_sysvinit
function above.)restart
on Upstart systems to a combination ofstop
/start
, since a plaininitctl restart
will error out if the service isn't running already.It stops sockets when stopping systemd services which have associated sockets:
Upstart services were enabled directly in the service configuration file (or disabled via overrides), and System V scripts were enabled or disabled with the
update-rc.d
command (which managed symlinks in the/etc/rc*
directories), so theservice
command was never involved in enabling or disabling services on boot.