0

I managed to start the service in 2 different ways

  1. Using normal command
root@ip-172-31-35-92:/#/usr/bin/mongod --config /etc/mongod_27018.conf &
  1. Using systemd service.
service mongod_27018 start

When either way I start the service a different PID is generate.

Problem statement

When I start service with command, I cannot stop using systemd service mongod_27018 stop. In order use systemctl start/stop/restart, I need to manually kill the process ID kill -9 $PID

Is there a way I can stop the service even though it is started from command ?

Unit file content

[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target

[Service]
User=mongodb
Group=mongodb
EnvironmentFile=-/etc/default/mongod
ExecStart=/usr/bin/mongod --config /etc/mongod_27018.conf
PIDFile=/var/run/mongodb/mongod_27018.pid
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
LimitMEMLOCK=infinity
TasksMax=infinity
TasksAccounting=false


[Install]
WantedBy=multi-user.target

1 Answer 1

1

The command service mongod_27018 start is managing the process using a process manager, either systemd if it is available or sysvinit if it is not. The process manager makes it easy to start/stop processes and check their status.

The process manager would only track processes it is directly managing. When you run the command /usr/bin/mongod --config /etc/mongod_27018.conf &, the process manager does not know to track the process and would require manually sending signals to the process to manage it.

If you have to manage manually run commands, then you would need some way of tracking them which can be done with process IDs (PID). The command in your example should output the PID of the process, or you can use the special bash variable $! to get the PID of the last command.

1
  • I understand that, I would stop using command instead managing both.
    – Achar007
    Dec 8, 2021 at 5:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .