0
votes

I want to make a bash file that automatically runs nodemon and mongod. I have the following code:

#!/usr/bin/env bash

workingDirectory="$(pwd)/"
serverFolder="${workingDirectory}first-app/caffeineOverflow"

cd "${serverFolder}"
mongod &
export caffeine_overflow_jwtPrivateKey=ok 
nodemon & 

I run it with sudo so that I can run mongod. I get the following error: exception in initAndListen: DBPathInUse: Unable to lock the lock file: /data/db/mongod.lock (Resource temporarily unavailable). Another mongod instance is already running on the /data/db directory, terminating

What should i do in order for this code to run?

1

1 Answers

0
votes

That means that another instance of mongodb server is running. Which is very likely since mongod & runs the process in the background and may stay running if it's not properly shut down. Include a mongod --shutdown step in your script like this:

#!/usr/bin/env bash

workingDirectory="$(pwd)/"
serverFolder="${workingDirectory}first-app/caffeineOverflow"
cd "${serverFolder}"

# Ensure that mongod is not running by killing it
mongod --shutdown

# Continue
mongod &
export caffeine_overflow_jwtPrivateKey=ok 
nodemon &