Linux – Confused about “/etc/init.d” and services in “/lib/systemd/system”

init-scriptinit.dlinuxsystemd

I am completely new to the linux world, so sorry for anything wrong I might say.

I am trying to run a Mongo DB on a Debian 8.5 machine. When I installed the package (pre built from percona.com), I have noticed the following files:

/etc/init.d/mongod (1)
/lib/systemd/system/mongod.service (2)

About /etc/init.d/mongod

I understand that this is called (as long as it is registered via update-rc.d) at boot / in other particular system states (don’t want to get technical on this, I’m absorbing lots of informations and this seems less important).

This is perfectly fine for me. The script does lots of initialization and finally launches the mongo daemon. It seems to have “triggers” for start, stop, restart, etc and as far as I understand I can trigger those with sudo service mongod <action>.

About /lib/systemd/system/mongod.service

This file seems to do the same thing (i.e. run mongo), but with less configuration, just one line in the ExecStart parameter:

[Unit]
Description=MongoDB (High-performance, schema-free document-oriented database)
After=time-sync.target network.target
[Service]
Type=forking
User=mongod
Group=mongod
PermissionsStartOnly=true
EnvironmentFile=/etc/default/mongod
ExecStart=/usr/bin/env bash -c "/usr/bin/mongod $OPTIONS > ${STDOUT} 2> ${STDERR}"
PIDFile=/var/run/mongod.pid
[Install]
WantedBy=multi-user.target

As far as I understand this can be triggered with sudo systemctl start mongod.

  • I don’t understand if this is called at boot or not.

  • I don’t understand why the need for two of these ‘service’ files, and how can I get rid of one (possibly the one in /lib/systemd, since it is much simpler).

  • I don’t understand if there’s any relation between the two.

  • I have read that systemctl works on init.d scripts too, and in this case I don’t understand which of the two files will be triggered by systemctl mongod start.

I think there’s some redundancy and I should choose just one of the two ways. And I want to be sure that it is

  • called at boot
  • callable by command (like service or systemctl).

Could you help me clear my mind? With the help of some comments I can probably focus and narrow down the question.

Best Answer

When you have both an init.d script, and a systemd .service file with the same name, systemd will use the service file for all operations. I believe the service command will just redirect to systemd. The init.d script will be ignored.

Use systemd. It's new in Debian 8, but it's the default. Systemd service files are supposed to look simpler than init.d scripts. You didn't mention any specific feature you need that's not supported by the systemd service.

If the service file was not included, systemd would happily use the init.d script. So the mongod package developer is telling you they think this systemd definition is better :).

Look at the output of systemctl status mongod. If the service is enabled to be started at boot time, the Loaded: line will show "enabled". Otherwise you can use systemctl enable mongod. You can also include the option --now, and it will start mongod at the same time.