76

I always run my Apache server in this way:

sudo service apache2 start

But in many guides I saw commands like this:

sudo systemctl start apache2

Can somebody shortly write the difference between these commands and give me advice about what command I should use in everyday work?

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
Roman Romanov
  • 896
  • 1
  • 7
  • 8

4 Answers4

56

service operates on the files in /etc/init.d and was used in conjunction with the old init system. systemctl operates on the files in /lib/systemd. If there is a file for your service in /lib/systemd it will use that first and if not it will fall back to the file in /etc/init.d. Also If you are using OS like ubuntu-14.04 only service command will be available.

So if systemctl is available ,it will be better to use it

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
shahin
  • 3,515
  • 1
  • 17
  • 17
  • 28
    According to [this answer](https://askubuntu.com/a/903405/790184), `service` is a wrapper for all three init systems (/init, systemd and upstart). – Dan Swain Mar 08 '18 at 21:37
  • 8
    Dan Swain is right. `service` is a wrapper, and thus its preferable over `systemctl` that works only if you have an `apache` service configuration in `/etc/systemd/system/` – stelios Jul 05 '18 at 05:22
  • Also note the syntax difference between two commands, ```sudo service ``` and ```sudo systemctl ``` – Vinu Raja Kumar C Mar 03 '22 at 06:35
27

With borrowing from this answer:

service is an "high-level" command used for start, restart, stop and status services in different Unixes and Linuxes. Depending on the "lower-level" service manager, service redirects on different binaries.

For example, on CentOS 7 it redirects to systemctl, while on CentOS 6 it directly called the relative /etc/init.d script. On the other hand, in older Ubuntu releases it redirects to upstart.

service is adequate for basic service management, while directly calling systemctl give greater control options.

Moreover, systemctl runs services file from this path: /lib/systemd/

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
  • 4
    Wouldn't it be better to "vote to close" as a duplicate instead of copy/pasting another answer? – rinogo Aug 27 '20 at 15:22
  • 3
    @rinogo This is not just a copy/paste. Actually, it is a kind of reference and quote as specified. The given scores for this answer also indicate that this answer has been needed in this gap place. Keep in mind our aim is to ease and solve askers' issues as much as possible. – Benyamin Jafari Sep 08 '21 at 07:11
2

Service started as a helper script for sysvinit an latter became a wrapper script that works regardless of having sysvinit or systemd.

chomwitt
  • 21
  • 2
1

For my choice service is more reliable, especially in automatization tasks. It's wrapping commands for different init systems, thus you can use it for older distributions and expect the same result. And in my observation there is often no working reload command (update daemon config without process killing ) for systemd unit, when it works with service <service-name> reload normally.