Creating MongoDB Start-up script - linux

I have been struggling on creating mongodb start-up script.
I have this file here "script.js" that contain my scripts that i want to execute every-time the server boot.
My script file is under "/opt/temp/script.js"
db.getCollectionNames().forEach(function(collection) {
print(collection);
});
Which command should I write to execute this file every time after the sever restarted ?
Where to put that command ?
Please help me
Thanks you..

What version of Linux are you using, what version of MongoDB have you installed, and how did you install it?
If, for example, you installed Mongodb on Ubuntu as per the MongoDB Docs you should have a start up script in /etc/init.d/mongod which will auto-start MongoD on boot.
If you have installed, say, a release candidate in a non standard location, I found these scripts to be fairly good and very alterable.
Using this script will NOT guarantee mongod starting on boot. You'll need to use sysv-rc-conf to set the run-levels correctly, like the apache2 line in the image linked.

Hi I managed to get it working I created Script.jsfile with my mongoDb commands and place it at /op/myscripts. Then on the mongodb-confI included the command to execute my script once mongodb is started command mongo /opt/myscripts/script.js.

Related

'mongo' is not recognised as an internal or external command, operable program or batch file [duplicate]

I installed MongoDB community Server from this link. But when I look into C:\Program Files\MongoDB\Server\6.0\bin
, I am not able to locate mongo.exe file. I am able to see mongod.exe and mongos.exe.
Can someone help. Thank you.
legacy mongo shell (mongo) no longer ships with server binaries (for servers >= 6.0). You can download a new shell version (mongosh) from here
Answer from dododo worked for me.
Download Mongo Shell - mongosh from MongoDB Download Center
Extract the contents of the bin from the downloaded zip file to the bin file of your MongoDB folder and run mongosh instead of mongo.
To run the Mongo Shell , Download msi package from the MongoDB Community Download - https://www.mongodb.com/try/download/community
The version 6.0 do not ships with server binaries so mongo.exe file wouldn't be shown. So instead download mongo shell - https://www.mongodb.com/try/download/shell
After downloading unzip the bin directory in your Original bin directory
(You would also need to create a new directory in C: drive named data/db)
Now open the bin directory from Mongodb folder, and run mongod.exe
After that keeping Mongod.exe running in background open mongosh.exe ,
Your mongo Shell will be started.
After version 6.0.0 mongo.exe does not get installed to your bin folder so you have to manually install the new MongoDB shell which is called mongosh then you have to add its path to your system variables and then run mongosh --version to see if it got installed. Afterwards if you want to create your own databases and preform operations run "mongosh" in your terminal to connect to a MongoDB instance running on your localhost with default port 27017.
Read the mongoDB documentation for more information:
install mongosh:
https://www.mongodb.com/docs/mongodb-shell/install/
The mongosh shell vs the old mongo shell:
https://www.mongodb.com/docs/mongodb-shell/
Connect to a Deployment:
https://www.mongodb.com/docs/mongodb-shell/connect/
It should be noted, that in MongoDB version 6.0 there are two items:
The legacy mongo shell is removed from MongoDB 6.0
The MongoDB Shell (mongosh) is not installed with MongoDB Server. You need to follow the mongosh installation instructions to download and install mongosh separately. This was already announced in MongoDB version 5.0
The legacy mongo shell does not exist anymore on MongoDB version 6.0. If you desire the old mongo.exe, then you can install if from an earlier MongoDB version.
You also need to add a path variable of the bin folder where mongosh application is present (after downloading from the below link:
https://www.mongodb.com/try/download/shell ).
Then you can use mongosh command from anywhere using CMD:
STEPS TO FOLLOW AFTER DOWNLOADING SHELL FROM https://www.mongodb.com/try/download/shell
Include both your mongosh shell and mongod in environment variables.
go to your c drive and create db folder inside data folder
then run your mongosh and mongod in two different cmd windows
separately
..first run mongod
..then run mongosh
if you have check-marked run as a service at the time of download
then next time you are not required to start mongosh , it will
automatically get started.

Autostart a node.js script using init.d in Debian

I have a little node application on a server (node mailer) that I run by going to its source folder and executing npm start. I figured the best way to run this automatically would be to create a my_script.sh file and drop it in the init.d directory of my debian box. Inside the file (below the !#/bin/bash line), the code to execute is
'/opt/mycode/source/npm start'
I save the line to the .sh file and restarted the machine, but so far haven't got it to work. My question is: is this even how you start a script like this (using that command and an .sh file)? It does start normally when I do it manually (when I navigate to it and run npm start in the terminal). I included the single quotes around it because of the space between npm start. Also, if I want to verify that it worked, which process would I look for other than just pinging my smtp mailer? Finally, I know I need to run:
update-rc.d my_script.sh defaults
but I was also confused at to whether I had done this correctly either (is it just the name of the file that goes there or the file plus the extension)?
The script that you leave on the init.d folder should not have any extension and should have functions to start, stop and get the status of the service (your application).
I'll leave a link with an example as well as with some basis in order to build the Linux service script.
I would suggest reloading the daemon with systemctl daemon-reload in order to refresh the Linux service files once you add a new one.

Firecracker microVM: how to execute a script file when a Firecracker microVM startup?

I went through Get Start Guide and api_request_doc to find a way to execute a script when start a microVM? But there is not way to do it.
Is there a way to achieve it? I want to run some script when a vm startup or execute a script without login.
So, far firecracker has not provided any support to start a script on startup.
You can use system daemons like openrc (for alpine based) and systemd to start the script on the startup.
to start script using systemd on startup, check https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux

Why is my mongo db not starting? (Bitnami MEAN Stack)

I installed the stack on my laptop and it was working fine. Then I restarted my computer and tried to start mongo and it would not start.
the error message I get in the manager is
meanstack-3.0.6-1/mongodb/scripts/ctl.sh : mongodb could not be
started Stderr: child process exited abnormally
And when I try to start manually from a terminal I get an error saying data/db directory not found even though there is one.
I have already tried these steps with no luck:
1) Remove .lock file
sudo rm /var/lib/mongodb/mongod.lock
2) repair the mongodb
mongod –repair
3) start the mongodb
sudo service mongodb start
4) start the mongo client
just a tip: check your available free disc space, sometimes this kind of problems occurs because mongDb don't have enough space on the disc to run the server. if this is not working then manually check the log file Mongodb.log at 'yourPath\bitnami\meanstack-3.x.x-x\mongodb\log' hear you will sure find some proper clue.
Rename below file to anything
meanstack-3.0.6-1/mongodb/data/db/mongod.lock
restart mongodb from bitnami
note:-- mongodb installed in your system is different then installed through bitnami, they are using different ports, i.e. currently you have two instance of mongodb installed in your system.
I needed to install an old version of Mongo so I replaced all the files in the /bin folder with the old version and then I was getting the error "mongodb could not be started". I then deleted all the files under data/db and restarted using "./ctlscript.sh start mongodb" which worked.

nodejs app with forever in cronjob does not work after reboot

I created a cronjob with sudo crontab -u USERNAME -e and tried to start my node.js app after each reboot.
It had worked well at another server.
But this time, I installed the Node.js via nvm
Then tried to run the same cronjob lines, but every time after reboot it failed. I tried to figure out all the related folders and tried again to run as different lines. When I check with forever list, server shows that no process is running.
#reboot /home/USERNAME/.npm/forever start -c /home/USERNAME/.nvm/current/bin/node /home/USERNAME/APPNAME/app
#reboot /home/USERNAME/.npm/forever start /home/USERNAME/APPNAME/app
#reboot /usr/local/bin/forever start -c /home/USERNAME/.nvm/current/bin/node /home/USERNAME/APPNAME/app
I think that the reason is somehow related with nvm. But I am not sure of that. I don't want to do anything unnecessary unless I am fully sure about it.
Edited July 26, 2015
Though I have used the npm's forever module to deploy nodejs production apps, I really do not see the need to use it on linux based servers as there are so many system level alternatives available.
One of them is upstart. It will help you run your scripts as system level services. Amazon Web Services also use upstart in their Elastic Beanstalk nodejs tiers to keep nodejs apps running forever.
If you really just need to get down to it, here is a link to run your nodejs app as an upstart service.
However, it is not limited to deploying nodejs apps only and if you learn upstart, you will be able to do a lot of things with it. Here is a link for that as well.
If your original approach was preferable (as it was in my case) you can fix your scheduled cron job by explicitly passing the path to node like this: (found in this answer)
#reboot /root/.nvm/versions/node/v7.1.0/bin/node /root/.nvm/versions/node/v7.1.0/bin/forever start /var/www/server.json
Apparently NVM works in magic by setting up some system paths which aren't setup at the moment the cron jobs run.

Resources