Skip to content

Instantly share code, notes, and snippets.

@jwilm
Created June 22, 2013 22:49
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jwilm/5842956 to your computer and use it in GitHub Desktop.
Save jwilm/5842956 to your computer and use it in GitHub Desktop.
MongoDB systemd service unit configuration
[Unit]
Description=MongoDB Database Service
Wants=network.target
After=network.target
[Service]
Type=forking
PIDFile=/var/run/mongodb/mongod.pid
ExecStart=/usr/local/bin/mongod --config /etc/mongod.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
User=mongodb
Group=mongodb
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target
@danmutblix
Copy link

@jwilm thanks for a nice unit file, I have one question though, how is the $MAINPID set?

Assumes mongod config file specifies pidfilepath = /var/run/mongodb/mongod.pid and is located at /etc/mongod.conf.

@danmutblix
Copy link

@jwilm, I figured it out, it's an environment variable set by systemctl

systemctl show mongod will print the MainPID among other things 👍

@KojoEnch
Copy link

KojoEnch commented Sep 9, 2020

@Genda1ph yours is perfect for Debian 9.
systemctl enable mongod and voila 👍

@Auze
Copy link

Auze commented Jan 13, 2021

if relevant, here for CentOS 7.8 :

[Unit]
Description=MongoDB Database Service
Wants=network.target
After=network.target

[Service]
Type=forking
PIDFile=/var/run/mongodb/mongod.pid
ExecStartPre=/bin/mkdir -p /var/lib/mongo
ExecStartPre=/bin/chown mongod:mongod /var/lib/mongo
ExecStart=/usr/bin/mongod --config /etc/mongod.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
User=mongod
Group=mongod
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target

@Mohamed-Bala
Copy link

mongodb.service
Loaded: masked (Reason: Unit mongodb.service is masked.)
Active: failed (Result: signal) since Wed 2021-04-28 10:04:49 CAT; 44min a>
Main PID: 6242 (code=killed, signal=ABRT)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment