If I run the following command, everything works as expected:

sudo mongod --fork --dbpath /path/to_data/ --port 27027 --syslog

However, if I put the following command in startmongo.sh and run it with sudo, I get an error:

mongod --fork --dbpath /path/to_data/ --port 27027 --syslog


[root@server Folder]# sudo ./startmongo.sh
about to fork child process, waiting until server is ready for connections.
forked process: 2394
ERROR: child process failed, exited with error number 100
To see additional information in this output, start without the "--fork" option.

If I look at the log file I see the following:

STORAGE [initandlisten] exception in initAndListn: Location28596: Unable to determine status of lock file in the data directory /path/to_data/: boost::filesystem::status: Permission denied: "/path/tod_data/mongod.lock", terminating

So how can I start Mongo from a script?

  • are you using the command " su " too? – simon Sep 4 '19 at 21:40
  • no. I listed everything I'm doing. – Devil's Advocate Sep 5 '19 at 13:05

    Enables a daemon mode that runs the mongod process in the background. By default mongod does not run as a daemon: typically you will run mongod as a daemon, either by using --fork or by using a controlling process that handles the daemonization process (e.g. as with upstart and systemd).

So when you using fork without using sudo effective uid don't have permission.

Edit: So I mean, if you don't want to daemonize process you should use nohup with & and mange everything within script. With your logs data directory /path/to_data/: boost::filesystem::status: Permission denied mean newly spawned process don't have permission to access files or data. Here --fork is doing something interesting so it doesn't work as you mentioned above.

You can try two options:

  1. Use like nohup mongod --fork --dbpath /path/to_data/ --port 27027 --syslog &
  2. Add sudo within script or directly call upstart or systemd file from script.

Hope this will help.

  • I don't understand. I'm not running the mongod command with sudo, but I am running the shell script with sudo. What is the difference and how do I fix it? – Devil's Advocate Sep 5 '19 at 13:06
  • Updated my answer, hope I understand your question correctly. – asktyagi Sep 5 '19 at 13:55
  • nohup seems to have done what I needed. Thanks! – Devil's Advocate Sep 5 '19 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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