2

I read the docs on webfaction and install the mongodb with the steps

and it said :

To start the database, run $HOME/webapps/application/mongodb-linux-architecture-version/bin/mongod --auth --dbpath $HOME/webapps/application/data/ --port number.
To stop MongoDB while it is running in the foreground, press Ctrl + C. 

But I need to keep the database always running
How can I do ?
Please giude me .Thanks

user2492364
  • 6,543
  • 22
  • 77
  • 147

4 Answers4

6

Create Makefile in $HOME/webapps/

start:
        if ! pgrep mongod; then ./<mongodb-dir-linux-version>/bin/mongod --auth --dbpath <dbpath> --port <port> --fork --logpath ./logs/mongodb.log; fi


stop:
        pgrep mongod | xargs kill

Now you can easily start/stop mongodb from mongodb app directory make start/make stop and close the shh session. Add it to cron to keep mongodb service alive even if something suddenly happens (for ex. server shutdown):

> export EDITOR=nano
> crontab -e

Add the following two lines and save.

*/10 * * * * make -C ~/webapps/<mongodb app name>/ -f ~/webapps/<mongodb app name>/Makefile start
@reboot make -C ~/webapps/<mongodb app name>/ -f ~/webapps/<mongodb app name>/Makefile start
Arugin
  • 1,931
  • 2
  • 13
  • 17
0

If you want the mongod server always up and running then you have to install it as a service. There is an installation guide in MongoDB official site: MongoDB Install as a Service on Linux

vmr
  • 1,895
  • 13
  • 24
0

The Makefile (located under ~/webapps/mongodb) could look as follows:

# Mongodb Makefile
start restart:
    ~/webapps/mongodb/mongodb-linux-x86_64-<your distribution>/bin/mongod --auth --dbpath ~/webapps/mongodb/data --port 12345 --fork --logpath ~/webapps/mongodb/logs/mongodb.log

stop:
    ~/webapps/mongodb/mongodb-linux-x86_64-<your distribution>/bin/mongod --port 12345 --dbpath ~/webapps/mongodb/data --shutdown

Be sure to create the logs folder in advance.

mkdir logs

@Arugin's answer does not work for multiple mongodb instances running.

flaudre
  • 2,358
  • 1
  • 27
  • 45
0

I found this works on webfaction for me:

In your $HOME/webapps/mongo40 folder create a mongo_restart.sh bash script

`ps -A | grep -q '[m]ongod'`

if [ "$?" -eq "0" ]; then
    exit 0
else
    nohup $HOME/webapps/mongo40/mongo64-rhel70/bin/mongod --auth --dbpath $HOME/webapps/mongo40/data/ --port <port number> &

exit 0

then, add this to your crontab with crontab -e:

@reboot nohup /home/<userfolder>/webapps/mongo40/<mongo install folder>/bin/mongod --auth --dbpath $HOME/webapps/mongo40/data/ --port <port number>
*/2 * * * * /home/<userfolder>/webapps/mongo40/mongo_restart.sh

That will check and reboot mongo every 2 minutes if the bash script finds it is not in memory.

Marc Maxmeister
  • 4,191
  • 4
  • 40
  • 54
  • A related useful trick is to add this script to your webfaction server to enable memory reporting and rebooting remotely: https://stackoverflow.com/questions/16603367/restart-python-py-when-it-stops-working/16625326#16625326 – Marc Maxmeister Aug 16 '18 at 23:39