How can we check if our mongodb is running on EC2 linux server? - linux

I have installed and started the MongoDB on EC2 Linux instance. How can I verify whether it is running or not?
1 way I got to know how to verify whether it is started or not is:
By checking the contents of the file at /var/log/mongodb/mongod.log for a line reading: [initandlisten] waiting for connections on port which I have not got.
What can be more ways to check?

You can check whether using a third party tool such as Mongoclient to check if you're able to connect your database. Or use one of the below methods:
Check processes that are working on:
ps -efl | grep mongo
ps -efl returns a list of processes that are being worked on your system right now, and you can use a pipe and grep to select only processes you wish, in this situation mongo.
This should return something like:
1 S sandsto+ 6997 6992 0 80 0 - 193376 - May22 ? 00:13:27 /bin/mongod --fork --bind_ip 127.0.0.1 --port 6081 --dbpath /var/mongo --logpath /var/log/mongo.log --pidfilepath /var/pid/mongo.pid --auth --nohttpinterface --noprealloc --nopreallocj --smallfiles --replSet ssrs --oplogSize 16
0 S sercan 8857 8827 0 80 0 - 85041 - May22 ? 00:20:58 mongod --storageEngine=wiredTiger
0 S sercan 29511 29471 2 80 0 - 67236 futex_ 13:41 pts/21 00:00:00 ./mongod --dbpath /home/sercan/mongo_data/
0 S sercan 29547 29529 0 80 0 - 3907 pipe_w 13:41 pts/22 00:00:00 grep --color=auto mongo
You can see there's a script ./mongod here that's working right now.
Either you can check ports that are being used by the system with command:
netstat -an | grep 27017
netstat -an returns all the allocated ports currently in the system and if you know your mongodb's port you can easily grep it from here. And this will return:
unix 2 [ ACC ] STREAM LISTENING 4184968 /tmp/mongodb-27017.sock
If you see LISTENING here, it means it's currently working.
But best way is adding mongodb to a system service manager such as supervisord, therefore you can simply check if your mongod is running with a single command:
sudo supervisorctl status
p.s. supervisor is just an example, there're a lot of process managers in linux that you can use, even mongodb documents have explanations about them.

You could check with this command
systemctl status mongod
and then you would see either of these results
1,
If it is inactive rung this command
systemctl start mongod
2,

Related

netstat gives 1 as PID for all ssh connections

In my custom linux image using netstat 1.42 (2001-04-15) from the package net-tools 1.60 I am seeing different behaviors for different linux versions..
On Linux 5.4.28:
# netstat -tnp | grep ESTABLISHED
tcp 0 0 <local_ip>:22 <client_ip>:14321 ESTABLISHED 29342/1
tcp 0 40 <local_ip>:22 <client_ip>:22470 ESTABLISHED 28443/0
On Linux 5.10.65:
# netstat -tnp | grep ESTABLISHED
tcp6 0 52 <local_ip>:22 <client_ip>:22470 ESTABLISHED 1/init
tcp6 0 52 <local_ip>:22 <client_ip>:33679 ESTABLISHED 1/init
The thing to note here is that on Linux 5.10, netstat shows PID as 1 and process as init for all ssh sessions. But in older linux, I am seeing correct PIDs for all ssh sessions. I need to know the client_ip and the PID for every ssh session so I can kill the session for whichever client I want.
From online searches, I could not figure out any fixes.
One alternative that I can see till now is using who to get this output:
# who
root pts/1 00:00 Jan 12 07:16:19 <client_ip>
root pts/2 00:03 Jan 12 08:21:07 <client_ip>
grep this with client_ip then use ps and grep that with the second column, like ps | grep pts/1 for first client.
Is there anything I can do to netstat to fix this though? Because netstat also give the client port which is useful for me.

How to stop a redis server that was started with --daemonize yes

I'm trying to kill a redis server that was started with --daemonize yes
I feel like this should be easy, but I am unable to successfully kill it based on standard methods. I'm using Redis 3.2 installed via these instructions: https://www.hugeserver.com/kb/install-redis-debian-ubuntu/
$ redis-server --daemonize yes
1550:C 13 Mar 05:54:55.436 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1550:C 13 Mar 05:54:55.437 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=1550, just started
1550:C 13 Mar 05:54:55.437 # Configuration loaded
$ kill 1550
-bash: kill: (1550) - No such process
$ kill -9 1550
-bash: kill: (1550) - No such process
$ killall redis-server
redis-server(1181): Operation not permitted
redis-server: no process found
$ /etc/init.d/redis-server stop
[....] Stopping redis-server (via systemctl): redis-server.service==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to stop 'redis-server.service'.
Authenticating as: Ubuntu (ubuntu)
Password:
Why should I need to enter a password to stop it? I never set a password when i started it. And why doesn't kill work? Even if I sudo it, it just restarts again after.
-------------------EDIT-------------------
Additional information. Using the kill command on the process ID doesn't work. I have to use sudo kill but then it comes back with another processid as if something restarts it. I just killed it 3 times and it's back again with a 4th processid –
---------- Solution thanks to #Kevin Law ----------
I needed to kill the daemonized service and then kill the process. Not just one or the other...
ubuntu#ip-MyIPAddress:~/Relayer$ ps -ef | grep redis-server
ubuntu 2381 1 0 18:39 ? 00:00:00 redis-server *:6379
ubuntu 2386 1358 0 18:39 pts/0 00:00:00 grep --color=auto redis-server
ubuntu#ip-MyIPAddress:~/Relayer$ sudo service redis-server stop
ubuntu#ip-MyIPAddress:~/Relayer$ ps -ef | grep redis-server
ubuntu 2381 1 0 18:39 ? 00:00:00 redis-server *:6379
ubuntu 2418 1358 0 18:39 pts/0 00:00:00 grep --color=auto redis-server
ubuntu#ip-MyIPAddress:~/Relayer$ kill 2381
ubuntu#ip-MyIPAddress:~/Relayer$ ps -ef | grep redis-server
ubuntu 2420 1358 0 18:39 pts/0 00:00:00 grep --color=auto redis-server
ubuntu#ip-MyIPAddress:~/Relayer$
There are three options for your question from my test.
Stop redis by stoping redis service. This is the recommend way in most cases.
sudo systemctl stop redis-server
or
sudo service redis-server stop
connect to this redis-server and shutdown it by redis-cli.
redis-cli
after connected to redis, issue shutdown to turn off redis-server
shutdown
Kill the redis-server process directly:
ps -ef | grep redis-server
kill -9 (pid)
Try following command to find the redis-server instances that are running and then try killing the exact instance with kill command.
ps -aef | grep redis

Failed to restart redis-server.service: Unit redis-server.service not found

Tried to start redis-server but got:
26195:C 27 Aug 17:05:11.684 # Warning: no config file specified, using
the default config. In order to specify a config file use redis-server
/path/to/redis.conf
26195:M 27 Aug 17:05:11.684 * Increased maximum number of open files
to 10032 (it was originally set to 1024).
26195:M 27 Aug 17:05:11.685 # Creating Server TCP listening socket
*:6379: bind: Address already in use
Ran lsof -wni tcp:3000 and killed the local host and tried restarting redis-server again and got the same above error.
Tried: ps -aux | grep redis (output below), then sudo kill -9 6379
nick4896 12238 0.0 0.1 41432 9048 ? Sl Aug26 0:14
redis-server *:6379
nick4896 26304 0.0 0.0 21300 984 pts/21 S+ 17:08 0:00 grep
--color=auto redis
And ran sudo service redis-server restart, and got:
Failed to restart redis-server.service: Unit redis-server.service not
found.
Any ideas?
The problem is that symlink redis-server.service to redis.service was deleted.
Command
sudo systemctl enable redis-server
creates the symlink:
Created symlink /etc/systemd/system/redis.service → /lib/systemd/system/redis-server.service.
Came across this, I would suggest systemctl daemon-reload
Not an answer, but to complete Igor Kavzov's answer, this is the code to enter at the terminal:
sudo ln /lib/systemd/system/redis.service /etc/systemd/system/redis-server.service

How to show which port is the database socket running by Linux command?

I have three MySQL database sockets running on a Linux machine. By the cmd "sudo netstat -npl|grep mysql", I can find the ports and db sockets. But I need to match them one to one by using pid.
Is there any cmd I can use to show the port number of a db socket directly in Linux?
The output looks like this:
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3886/mysqld
The PID of the mysqld process is the number before /mysqld, i.e. 3886.
There's several different ways to do it, but for your purposes, I suggest simply looking at the output of the command you're already running.
$ sudo netstat -npl|grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1124/mysqld
unix 2 [ ACC ] STREAM LISTENING 8713 1124/mysqld /var/run/mysqld/mysqld.sock
This is telling me that process 1124 is listening on 3306 and /var/run/mysqld/mysqld.sock

How to kill a process running on particular port in Linux?

I tried to close the tomcat using ./shutdown.sh from tomcat /bin directory. But found that the server was not closed properly. And thus I was unable to restartMy tomcat is running on port 8080.
I want to kill the tomcat process running on 8080. I first want to have the list of processes running on a specific port (8080) in order to select which process to kill.
This fuser 8080/tcp will print you PID of process bound on that port.
And this fuser -k 8080/tcp will kill that process.
Works on Linux only. More universal is use of lsof -i4 (or 6 for IPv6).
To list any process listening to the port 8080:
lsof -i:8080
To kill any process listening to the port 8080:
kill $(lsof -t -i:8080)
or more violently:
kill -9 $(lsof -t -i:8080)
(-9 corresponds to the SIGKILL - terminate immediately/hard kill signal: see List of Kill Signals and What is the purpose of the -9 option in the kill command?. If no signal is specified to kill, the TERM signal a.k.a. -15 or soft kill is sent, which sometimes isn't enough to kill a process.).
Use the command
sudo netstat -plten |grep java
used grep java as tomcat uses java as their processes.
It will show the list of processes with port number and process id
tcp6 0 0 :::8080 :::* LISTEN
1000 30070621 16085/java
the number before /java is a process id. Now use kill command to kill the process
kill -9 16085
-9 implies the process will be killed forcefully.
Option 1 A One-liner to kill only LISTEN on specific port:
kill -9 $(lsof -t -i:3000 -sTCP:LISTEN)
Option 2 If you have npm installed you can also run
npx kill-port 3000
One liner, a time saver
kill -9 $(lsof -t -i tcp:8080)
Explanation here: use a combination of lsof and kill
root#localhost:~# lsof -i tcp:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 23672 sine 238u IPv6 3028222 0t0 TCP localhost:http-alt (LISTEN)
select pid and use kill
kill 23672
You can use the lsof command.
Let port number like here is 8090
lsof -i:8090
This command returns a list of open processes on this port.
Something like…
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ssh 75782 eoin 5u IPv6 0x01c1c234 0t0 TCP localhost:8090 (LISTEN)
To free the port, kill the process using it(the process id is 75782)…
kill -9 75782
This one worked for me.
here is the link from the original post: link
If you want to kill a process running on port number 8080 then first you need to find the 8080 port process identification number(PID) and then kill it. Run the following command to find 8080 port number PID:
sudo lsof -t -i:8080
Here,
sudo - command to ask admin privilege(user id and password).
lsof - list of files(Also used for to list related processes)
-t - show only process ID
-i - show only internet connections related process
:8080 - show only processes in this port number
So you can now easily kill your PID using following command:
sudo kill -9 <PID>
Here,
kill - command to kill the process
-9 - forcefully
You can use one command to to kill a process on a specific port using the following command:
sudo kill -9 $(sudo lsof -t -i:8080)
For more you can see the following link
How to kill a process on a specific port on linux
Best way to kill all processes on a specific port;
kill -9 $(sudo lsof -t -i:8080)
This prints to stdout the process ids of everything running on <port_number>:
fuser -n tcp <port_number>
It also prints some stuff to stderr, so:
fuser -n tcp <port_number> 2> /dev/null
We can then supply these process ids to the kill command:
sudo kill $(fuser -n tcp <port_number> 2> /dev/null)
You could also put this in a function if you do it a lot:
function killport() {
sudo kill $(fuser -n tcp $1 2> /dev/null)
}
To know the pid of service running on particular port :
netstat -tulnap | grep :*port_num*
you will get the description of that process. Now use kill or kill -9 pid. Easily killed.
e.g
netstat -ap | grep :8080
tcp6 0 0 :::8080 :::* LISTEN 1880/java
Now:
kill -9 1880
Remember to run all commands as root
Get the PID of the task and kill it.
lsof -ti:8080 | xargs kill
First you need to do is run (replace with your port number):
fuser -k 3000/tcp
This will release the port. After you run the above command run:
service docker restart
And your problem is resolved.
try like this,
sudo fuser -n tcp -k 8080
Choose the port number and apply the grep in netstat command as shown below
netstat -ap | grep :7070
Console Output
tcp 0 0 :::7070 :::* LISTEN 3332/java
Kill the service based on PID ( Process Identification Number )
kill -9 3332
This will kill programs running on port 80
sudo fuser -k 80/tcp
Run the following command to find 8080 port number PID:
sudo lsof -t -i:8080
You can now easily kill your PID using following command:
sudo kill -9
You can use one command to to kill a process on a specific port using the following command:
sudo kill -9 $(sudo lsof -t -i:8000)
lsof -i tcp:8000
This command lists the information about process running in port 8000
kill -9 [PID]
This command kills the process
Linux: First you can find PID of this command if you know the port :
netstat -tulpn
example:-
Local Address Foreign Address State PID/Program name
:::3000 :::* LISTEN 15986/node
You then take the kill process. run the following command:
kill -9 PID
Expample: -
kill -9 15986
Linux: You can use this command if you know the port :
netstat -plten | grep LISTEN | grep 8080
AIX:
netstat -Aan | grep LISTEN | grep 8080
You then take the first column (example: f100050000b05bb8) and run the following command:
rmsock f100050000b05bb8 tcpcb
kill process.
You can know list of all ports running in system along with its details (pid, address etc.) :
netstat -tulpn
You can know details of a particular port number by providing port number in following command :
sudo netstat -lutnp | grep -w '{port_number}'
ex: sudo netstat -lutnp | grep -w '8080'
Details will be provided like this :
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
if you want to kill a process using pid then : kill -9 {PID}
if you want to kill a process using port number : fuser -n tcp {port_number}
use sudo if you are not able to access any.
Simply run this command. Don't forget to replace portnumber, with your port ;)
kill -9 $(sudo lsof -t -i:portnumber)
kill -9 `fuser 8080/tcp|xargs -n 1`, this commands also kills the process that listens on port 8080 with TCP connection
sudo apt-get install psmisc (or sudo yum install psmisc)
sudo fuser 80/tcp
Result:
80/tcp: 1858 1867 1868 1869 1871
Kill process one by one
kill -9 1858
I'm working on a Yocto Linux system that has a limited set of available Linux tools. I wanted to kill the process that was using a particular port (1883).
First, to see what ports we are listening to I used the following command:
root#root:~# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:hostmon 0.0.0.0:* LISTEN
tcp 0 0 localhost.localdomain:domain 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:9080 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:1883 0.0.0.0:* LISTEN
tcp 0 0 :::hostmon :::* LISTEN
tcp 0 0 localhost:domain :::* LISTEN
tcp 0 0 :::ssh :::* LISTEN
tcp 0 0 :::1883 :::* LISTEN
Next, I found the name of the process using port 1883 in the following way:
root#root:~# fuser 1883/tcp
290
root#root:~# ps | grep 290
290 mosquitt 25508 S /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
12141 root 8444 S grep 290
As we can see above, it's the program /usr/sbin/mosquitto that's using port 1883.
Lastly, I killed the process:
root#root:~# systemctl stop mosquitto
I used systemctl becuase in this case it was a systemd service.
This is the solution for Windows:
C:\Users\Niroshan>netstat -ano|findstr "PID :8080"
Proto Local Address Foreign Address State PID
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 18264
taskkill /pid 18264 /f
first check the process netstat -lt
check process id fuser <port number>/tcp
kill the process running on the port kill <process id>
Other way with Git Bash:
stopProcessByPortNumber() {
port=":${1}"
portStrLine="$(netstat -ano | findstr LISTENING | findstr $port)"
processId="$(grep -oP '(\d+)(?!.*\d)' <<< $portStrLine)"
echo $processId
taskkill -PID $processId -F
}
In my case cent os has some issue in suggested answer. So I used following solution :
ss -tanp | grep 65432 | head -1 | grep -Po "(?<=pid=).*(?=,)" | xargs kill
to build on what #veer7 said:
if you want to know what was on the port, do this before you kill it.
$ sudo netstat -plten |grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1000 906726 25296/java
tcp6 0 0 :::8009 :::* LISTEN 1000 907503 25296/java
tcp6 0 0 :::8080 :::* LISTEN 1000 907499 25296/java
$ ps 25296
PID TTY STAT TIME COMMAND
25296 ? Sl 0:16 /usr/lib/jvm/java-8-openjdk-amd64/bin/java -Dcatalina.base=/hom
Use 'ps' and the number of the process that netstat reported back
How to kill process if the service on the port is not responding
timeout 1 telnet localhost 8080 2>&1 | if grep -q 'Unable'; then sudo kill -9 $(sudo lsof -t -i:8080); fi
What it does
Start 'timeout' with one second, to make telnet exit
' 2>&1 | ' pipe error-message without exiting
If error-message contains 'Unable', then run the kode to kill process locking port 8080
This can ble placed in a file and run regulary from 'sudo crontab'

Resources