I am new to linux and have been testing myself using an Amazon Lightsail instance (Ubuntu 16.04 LTS).

Going through the many guides I have came across, I see people using different commands to start/stop/restart/reload/status-check a service. Specifically these;

sudo systemctl status apache2.service
sudo /bin/systemctl status apache2.service
sudo /etc/init.d/apache2 status
sudo service apache2 status

All the above commands work.

  1. Should I prefer one command over the other?
  2. If yes then why?
  3. Are there any other commands I need to be aware of?

Using init.d in Monit caused issues when I wanted to use the status option (status will be that the service is offline when it was actually online -- restarted by Monit). Change the code in Monit from inid.d to /bin/systemctl fixed it.

It seems that using init.d provides more information on what happened that the others. If I should be using one of the other commands, is it possible to have them display more information on what was done?

ubuntu@ip-172-26-12-245:~$ sudo systemctl restart pure-ftpd.service
ubuntu@ip-172-26-12-245:~$ sudo /bin/systemctl restart pure-ftpd.service
ubuntu@ip-172-26-12-245:~$ sudo /etc/init.d/pure-ftpd restart
[ ok ] Restarting pure-ftpd (via systemctl): pure-ftpd.service.
ubuntu@ip-172-26-12-245:~$ sudo service pure-ftpd restart
ubuntu@ip-172-26-12-245:~$

I would like to thank everyone in advance who has taken the time to read and reply to this question.

  • In Linux there is often more then one way to perform an action. There is not one that is better or worse or right or wrong. Personally I use the one with the least typing. Many of these commands may be sym links or back compatibility as Ubuntu changes to systemd. – Panther May 3 '17 at 17:29
  • systemctl is the preferred syntax and service is provided as backward compatibility. /etc/init.d/pure-ftpd or similar are calling the start/stop scripts directly. – Panther May 3 '17 at 17:46
up vote 38 down vote accepted

To start, there's a whole history and struggle between going from SysVInit to SystemD. Rather than trying to break that all down in one answer though, I'll refer you to some google venturing for more details on the history as well as one particular article on the topic:

http://www.tecmint.com/systemd-replaces-init-in-linux/

In summary though, it's been a slow and arduous transition. Some legacy features were kept intact (such as init.d to some degree). If you have the option to use systemctl for your service control I recommend using that one. It's the foreseeable future for Linux and eventually older SysVInit methods will be considered deprecated entirely and removed.

To cover each one you listed specifically:

  1. sudo systemctl status apache2.service

This is the new SystemD approach to handling services. Moving forward, applications on Linux are designed to uses the systemd method, not any other.

  1. sudo /bin/systemctl status apache2.service

This is the same thing as the previous command. The only difference in this case is that it's not depending on the shell's $PATH environment variable to find the command, it's listing the command explicitly by including the path to the command.

  1. sudo /etc/init.d/apache2 status

This is the original SysVInit method of calling on a service. Init scripts would be written for a service and placed into this directory. While this method is still used by many, service was the command that replaced this method of calling on services in SysVInit. There's some legacy functionality for this on newer systems with SystemD, but most newer programs don't include this, and not all older application init scripts work with it.

  1. sudo service apache2 status

This was the primary tool used on SysVInit systems for services. In some cases it just linked to the /etc/init.d/ scripts, but in other cases it went to an init script stored elsewhere. It was intended to provide a smoother transition into service dependency handling.


Lastly, you mention wanting to know how to get more information out of the commands, since some provide more information than others. This is almost always determined by the application and how they designed their init or service file. As a general rule though, if it completed silently it was successful. However, to verify a start, stop, or restart, you can use the status sub-command to see how it is doing. You mentioned a status command being incorrect on an old init script. That is a bug that the application developers would have to look at. However, since init scripts are becoming the deprecated method of handling services, they may just ignore the bug until they remove the init script option entirely. The systemctl status should always work correctly otherwise a bug should be logged with the application developers.

  • Thank you so much for your detailed reply. I am also Googling for answers but this one really confused me, so I posted it here. I also see that sudo systemctl status apache2 works, instead of (sudo systemctl status apache2.service). Is there a harm in foregoing the .service part? – Waqas Tariq May 3 '17 at 19:12
  • @WaqasTariq no problem! Both of those should work, systemctl will search the directories where the service files are stored and add the ".service" for you if it finds it. So for example, if you hit tab once after only writing sudo systemctl status apache2 it should complete it by adding .service for you. If there's more than one apache2 systemctl file (such as .service and a .target you'd have to hit tab twice so that it shows you all the available options. – TopHat May 3 '17 at 19:14
  • Got it. Thank you for your answers and your time. – Waqas Tariq May 3 '17 at 19:16
  • @WaqasTariq your welcome! – TopHat May 3 '17 at 19:18

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.