I can start mongodb on terminal via command
./mongod
It starts the mongodb server and then display me information that server is running on this port. but It does not give my terminal back. How can I start mongodb and can get terminal back so mongodb is running the background.
Also how to shutdown if its running in background
Use
./mongod --fork
or
./mongod &
To shutdown you have to send it a TERM signal.
ps aux | grep mongod - to find a PID
kill -TERM PID - send it a TERM signal, and using the first example we can use the PID file:
kill -TERM $(cat /var/run/mongodb/mongod.pid)
Also you can shut it down from the shell.
$ ./mongo
> use admin
> db.shutdownServer()
--
And another method:
./mongod --fork --pidfilepath /var/run/mongodb/mongod.pid
then (please notice the ticks around the cat)
kill -9 `cat /var/run/mongodb/mongod.pid`
./mongod &
You will see a number in the output, something similar to
[1]+ ./mongod &
To kill the process execute a kill %1 where 1 is the number between the angular brackets.
Related
Based on the question here, I have the following rc.local
#!/bin/bash
#start script
exec 2> /scriptfolder/rc.local.log
exec 1>&2
set -x
/scriptfolder/startscript.sh
exit 0
Here is the specific line of startscript.sh that isn't working properly from rc.local
nohup sudo mongod --dbpath /scriptfolder/subfolder/ta_data/ --port 27027 --logRotate reopen --logappend --logpath /scriptfolder/autostart.mongod.log --fork &
I've also tried a few variants, including:
nohup sudo mongod --dbpath /scriptfolder/subfolder/ta_data/ --port 27027 &> /scriptfolder/autostart.mongod.log&
They all work when I run ./startscript.sh manually but when it runs from rc.local mongo never starts. Subsequent commands (including some docker commands) do run, so I know the script itself is executing. I don't see any errors in rc.local.log and autostart.mongod.log is just empty.
Here is the shell script that I am using to check MongoDB's status. checkmongo.sh through a cron job every two minutes.
#!/bin/bash
echo "shell script called"
if pgrep mongo > /dev/null
then
echo "MongoDB Running"
else
echo "Stopped .. starting MongoDB"
sudo service mongodb start
fi
Cron job is as follows :
*/2 * * * * /etc/init.d/checkmongo.sh> /etc/init.d/cronjob.log 2>&1
No matter MongoDb is running or not cronjob.log always has the content as "MongoDB Running". What am I doing wrong ? Please help. Thanks
Vinay,
A couple of things:
1 -- Do you want to check if MongoDB is running or do you want to check if it is responding to queries?
Your current design will only tell if there is a process whose name includes the word "mongo" running.
I would design a "canary query" that you can run with the mongo shell and use the result to determine if MongoDB is running or not. So in the rare case where MongoDB daemon might be running but not responsive you could be alerted.
2 --Your process is checking for the word "mongo", the executable for the mongoDB database is "mongod".
Your script is called "checkmongo". Every time you script runs there is a "checkmongo" process running and the if conditions will always be true. The script is detecting itself instead of the mongod process.
Change your search condition to mongod
if pgrep mongod > /dev/null
#!/bin/bash
mongod_status=`service mongod status`
echo "${mongod_status}"
if [[ "${mongod_status}" == *"start/running"* ]]
then
echo "MongoDB is already running."
else
sudo service mongod start
echo "Start MongoDB."
fi
I am writing a monit script for mongo(in ubuntu). Script given below
check process mongodb
with pidfile "/var/lib/mongodb/mongod.lock"
start program = "/sbin/start mongodb"
stop program = "/sbin/stop mongodb"
But I am getting error log
monit: Error reading pid from file '/var/lib/mongodb/mongod.lock'
Please let me know if any work around in monit for this issue.
Monit needs a pid file (containing the pid of the mongod process) so you have to create it when you start mongod
#!/bin/bash
mongod &
echo "$!" > /var/run/mongod.pid
and set /var/run/mongod.pid as pidfile
A bit late, but I found the pid file here: /var/run/mongodb.pid
I had the same error using MongoDB, this solved my problem:
chown -R mongod:mongod /var/lib/mongodb
When I ran the Mosquitto (MQTT) broker for the first time there was no issue. But however from he second time when i ran it using default config I could not run the code successfully because of the following error:
1379497253: mosquitto version 1.2 (build date 2013-09-17 17:59:39+0530) starting
1379497253: Using default config.
1379497253: Opening ipv6 listen socket on port 1883.
1379497253: Error: Address already in use
I would like to know how to stop the broker from command line. It'll be nice if someone can help.
mosquitto starts to be a service.
sudo vi /etc/mosquitto/mosquitto.conf #more detail in `man mosquitto.conf`
sudo vi /etc/mosquitto/conf.d/custom.conf #add or change listening port as your need
sudo service mosquitto restart
If you don't know the PID, than you can use "pkill" instead of "kill" in linux.
Command: "pkill mosquitto"
You don't say which OS you are using, but assuming it's Linux and you have mosquitto running in the background, you just use the kill command.
Use ps to find the pid of the currently running mosquitto instance then kill the pid.
You can use your own batch script file like mosquit.sh in bash.
This is my script to stop it on CentOS.
#!/bin/sh
sudo kill $(ps aux |awk '/mosquitto/ {print $2}')
Assuming it's the linux service, a kill command will only stop using that particular socket, and will open up on another socket upon killing the PID. On redhat, to kill the service would be systemctl stop mosquitto
For Windows, open a Console as Administrator and use
taskkill /im <programname.exe> /f
Where /im = select by program (image) name, and /f = force -
C:\Users\bburns
> taskkill /im mosquitto.exe
ERROR: The process "mosquitto.exe" with PID 5344 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).
C:\Users\bburns
> taskkill /im mosquitto.exe /f
SUCCESS: The process "mosquitto.exe" with PID 5344 has been terminated.
I tired to start supervisor but getting error. Can anyone help? Thanks
/etc/init.d/supervisord file.
SUPERVISORD=/usr/local/bin/supervisord
SUPERVISORCTL=/usr/local/bin/supervisorctl
case $1 in
start)
echo -n "Starting supervisord: "
$SUPERVISORD
echo
;;
stop)
echo -n "Stopping supervisord: "
$SUPERVISORCTL shutdown
echo
;;
restart)
echo -n "Stopping supervisord: "
$SUPERVISORCTL shutdown
echo
echo -n "Starting supervisord: "
$SUPERVISORD
echo
;;
esac
Then run these
sudo chmod +x /etc/init.d/supervisord
sudo update-rc.d supervisord defaults
sudo /etc/init.d/supervisord start
And getting this:
Stopping supervisord: Shut down
Starting supervisord: /usr/local/lib/python2.7/dist-packages/supervisor/options.py:286: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
'Supervisord is running as root and it is searching '
Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
For help, use /usr/local/bin/supervisord -h
Conf file (located at /etc/supervisord.conf):
[unix_http_server]
file=/tmp/supervisor.sock; (the path to the socket file)
[supervisord]
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock; use a unix:// URL for a unix socket
[program:myproject]
command=/home/richard/envs/myproject_stage/bin/python /home/richard/webapps/myproject/manage.py run_gunicorn -b 127.0.0.1:8002 --log-file=/tmp/myproject_stage_gunicorn.log
directory=/home/richard/webapps/myproject/
user=www-data
autostart=true
autorestart=true
stdout_logfile=/tmp/myproject_stage_supervisord.log
redirect_stderr=true
first of all, type this on your console or terminal
ps -ef | grep supervisord
You will get some pid of supervisord just like these
root 2641 12938 0 04:52 pts/1 00:00:00 grep --color=auto supervisord
root 29646 1 0 04:45 ? 00:00:00 /usr/bin/python /usr/local/bin/supervisord
if you get output like that, your pid is the second one. then if you want to shut down your supervisord you can do this
kill -s SIGTERM 29646
hope it helpful. ref: http://supervisord.org/running.html#signals
sudo unlink /tmp/supervisor.sock
This .sock file is defined in /etc/supervisord.conf's [unix_http_server]'s file config value (default is /tmp/supervisor.sock).
$ ps aux | grep supervisor
alexamil 54253 0.0 0.0 2506960 6440 ?? Ss 10:09PM 0:00.26 /usr/bin/python /usr/local/bin/supervisord -c supervisord.conf
so we can use:
$ pkill -f supervisord # kill it
This is what works for me. Run the following in the Terminal (For Linux machines)
To check if the process is running:
sudo systemctl status supervisor
To stop the process:
sudo systemctl stop supervisor
Try running these commands
sudo unlink /run/supervisor.sock
and
sudo /etc/init.d/supervisor start
As of version 3.0a11, you could do this one-liner:
sudo kill -s SIGTERM $(sudo supervisorctl pid) which hops on the back of the supervisorctl pid function.
There are many answers already available. I shall present a cleaner way to shut down supervisord.
supervisord by default, creates a file named supervisord.pid in the directory where supervisord.conf file exists. That file consists the pid of the supervisord daemon. Read the pid from the file and kill the supervisord process.
However, you can configure where the supervisord.pid file should be created. Refer this link to configure it, http://supervisord.org/configuration.html