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?
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.There are a lot more than what you mentioned that
systemctl
is capable of:systemd
works with units, there are different type of units: targets, services, sockets, etc. targets are same concept as runlevels, they are a bunch of units together.You can use
systemctl
to set or get the default system target.You can go into other targets:
Other targets are: multiuser, graphical, recue, emergency, reboot, poweroff.
As you said, you can use
systemctl
to manage services, some of the other commands related to service management which I'm aware of are:You can use it to find out about a service status:
You can mask or unmask a service:
Wen you mask a service it will be linked to
/dev/null
, so manually or automatically other services can't active/enable it. (you should unmask it first).Another usage of systemctl is to list units:
Which list all kind of units, loaded and active.
List service units:
Or to list all available units not just loaded and activated ones:
You can create aliases or even control remote machines
At the other hand
service
does what it have to do, managing services and having nothing to do with other peoples business ;)