MongoDB startup: /usr/bin/dirname: extra operand ‘2>&1.pid’

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, December 23, 2014

Follow me on Twitter as @mattiasgeniar

After a package update to 2.6.6 for MongoDB on a CentOS 6 server, I had a problem restarting the service.

Update: this should now be fixed in SERVER-16081, try a new “yum update” to download the latest packages.

$ /etc/init.d/mongod restart
Stopping mongod:                                           [  OK  ]
Starting mongod: /usr/bin/dirname: extra operand `2>&1.pid'
Try `/usr/bin/dirname --help' for more information.
                                                           [FAILED]

The error would indicate a problem in the start-up init.d script, given the error around dirname and extra operands. If we look into the /etc/init.d/mongod file, we see around line 63 the following command.

$ vim /etc/init.d/mongod
...
daemon --user "$MONGO_USER" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
...

The alert that triggers the error into stderr of the init.d script, is the 2>&1 part. It tries to redirect all error output. A quick fix is to modify that line and not redirect stderr output.

$ vim /etc/init.d/mongod
...
# Disable the original line and remove the 2>&1 redirection
# daemon --user "$MONGO_USER" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
daemon --user "$MONGO_USER" "$NUMACTL $mongod $OPTIONS >/dev/null"

But the next MongoDB update will most likely reset your init.d script back to the package defaults, so it’s only a short-term fix.

Now, if your MongoDB isn’t starting, and you see these errors, have a look in your /var/log/mongodb/mongod.log logfiles. Because the init.d script may throw errors when start/stop/restarting the service, the real error is only logged in the mongod.log files.

$ grep 'ERROR' /var/log/mongodb/mongod.log
...
[initandlisten] ERROR: Insufficient free space for journal files
[initandlisten] Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles

So if MongoDB isn’t starting, and you’re seeing a dirname error in your shell when using the init.d scripts, check the /var/log/mongodb/mongod.log logs. The error in the init.d script seems to be just that, an error, without any impact.



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.