How to check if a service that I don't know the name of is running on Ubuntu - linux

I do not know the service's name, but would like to stop the service by checking its status.
For example, if I want to check if the PostgreSQL service is running or not, but I don't know the service's name, then how could I check its status?
I know the command to check the status if the service name is known.

I don't have an Ubuntu box, but on Red Hat Linux you can see all running services by running the following command:
service --status-all
On the list the + indicates the service is running, - indicates service is not running, ? indicates the service state cannot be determined.

For Ubuntu (checked with 12.04)
You can get list of all services and select by color one of them with 'grep':
sudo service --status-all | grep postgres
Or you may use another way if you know correct name of service:
sudo service postgresql status

Maybe what you want is the ps command;
ps -ef
will show you all processes running. Then if you have an idea of what you're looking for use grep to filter;
ps -ef | grep postgres

There is a simple way to verify if a service is running
systemctl status service_name
Try PostgreSQL:
systemctl status postgresql

If you run the following command you will get a list of services:
sudo service --status-all
To get a list of upstart jobs run this command:
sudo initctl list

You can use the below command to check the list of all services.
ps aux
To check your own service:
ps aux | grep postgres

the best way is using of nmap tool in terminal.
nmap is an useful tool that analyse an up system, using it's IP Address, then show all actived network services.
open terminal and use of this example :
~$ nmap 192.168.1.3/24
Starting Nmap 5.21 ( http://nmap.org ) at 2016-05-16 22:49 IRDT
Nmap scan report for 192.168.1.3
Host is up (0.00020s latency).
Not shown: 994 closed ports
PORT STATE SERVICE
22/tcp open ssh
23/tcp open telnet
139/tcp open netbios-ssn
445/tcp open microsoft-ds
3389/tcp open ms-term-serv
3689/tcp open rendezvous

run
ps -ef | grep name-related-to-process
above command will give all the details like pid, start time about the process.
like if you want all java realted process give java
or if you have name of process place the name

To check the status of a service on linux operating system :
//in case of super user(admin) requires
sudo service {service_name} status
// in case of normal user
service {service_name} status
To stop or start service
// in case of admin requires
sudo service {service_name} start/stop
// in case of normal user
service {service_name} start/stop
To get the list of all services along with PID :
sudo service --status-all
You can use systemctl instead of directly calling service :
systemctl status/start/stop {service_name}

Dirty way to find running services. (sometime it is not accurate because some custom script doesn't have |status| option)
[root#server ~]# for qw in `ls /etc/init.d/*`; do $qw status | grep -i running; done
auditd (pid 1089) is running...
crond (pid 1296) is running...
fail2ban-server (pid 1309) is running...
httpd (pid 7895) is running...
messagebus (pid 1145) is running...
mysqld (pid 1994) is running...
master (pid 1272) is running...
radiusd (pid 1712) is running...
redis-server (pid 1133) is running...
rsyslogd (pid 1109) is running...
openssh-daemon (pid 7040) is running...

For centos, below command worked for me (:
locate postgres | grep service
Output:
/usr/lib/firewalld/services/postgresql.xml
/usr/lib/systemd/system/postgresql-9.3.service
sudo systemctl status postgresql-9.3.service

for Centos 6.10 :
/sbin/service serviceNAME status
for Centos 7.6 and ubuntu 18.04:
systemctl status NAME.service
works for all of them:
service --status-all

Based on this answer on a similar topic https://askubuntu.com/a/58406 I prefer: /etc/init.d/postgres status

if you are looking particularly for postgres there is a specific command called "pgrep"
you can find the usage in the below mentioned article
https://mydbanotebook.org/post/troubleshooting-01/
this article provides the following details:
check if postgres server is running
where does postgres store all the server config
how to start/stop postgres
hope this is helpful

Related

Node exporter port already in use, service is failed

Node exporter is in failed state, journalctl says:
level=fatal msg="listen tcp :9100: bind: address already in use" source="node_exporter.go:114"
I tried reset-failed and restarting the service still the same issue. Then i listed processes using 9100 port and killed the process (the process was a node_exporter process) but right after i kill it whether normally or with -9 it just starts a new process as a result my 9100 port is always in use and can't start node_exporter because of it. Is there a solution for this?
Learn process-id of process, who use port 9100
netstat -lpn | grep 9100
Learn parent pid of process
ps -p PID -o ppid
If parent process is systemd(pid is 1), then find its service name via
systemctl status PID
And decide what to do with it.
If parent process is not systemd, but something like containerd-shim, this means that this process is managed via Kubernetes Daemonset.
In such case you need to decide to reuse existing process, or to change port in your node_exporter.service, you may add --web.listen-address=:9101 to ExecStart property in your service manifest and apply systemctl daemon-reload, then restart your service.
I had the same issue, but I found a solution:
search the process that uses 9100 port:
ss -ntpl 9100
look what PID of process and then kill him:
kill <pid>
Also, remember about node taints and tolerations

sudo service command not found when installing mongodb

I am currently on OS/X using macbook. I want to stop the instance of mongodb service running. Hence I tried:
> sudo service mongodb stop
sudo: service: command not found
After looking up on Google, they asked me to add PATH hence I did the following:
> `vim ~/.bash_profile` (created a new bash_profile) and added the following there:
export PATH=/usr/bin:/usr/sbin:/bin:/usr/local/bin:/sbin:/opt/x11/bin:$PATH
It does not seem to work and I still get the same error:
The command you are using is for Linux. To kill all mongod instances on your system, you have to issue
killall mongod
in a Terminal window. In order to kill a specific mongod instance, you have to issue
ps ax | grep [m]ongod
This will give you a list of mongod instances currently running on your Mac. Pick the one you want to stop (you can distinguish them via their command line options) and have a look to the very left of that line. The number is the process id (or pid for short) of that mongod instance. You kill that instance by issuing
kill <pid>
Replacing <pid> with the actual process id.

BASH - how to make this always running from system boot and on crash restart?

I have this protocol port open to read remotely from Python, PHP applications but daily it crash and the port is unavailable as a result Python, PHP all client application fails
$ cat /var/tmp/server.sh
#!/bin/bash
while true; do tail -f /usr/local/freeswitch/log/freeswitch.log | nc -l -p 9999 -q 1 &
Q. Is there anyway to make this script always running like service this start or stop and if its crashed that somehow it automatically again get restarted ? Any advise or link to do such thing? i am using CentOS 6.x
Put your script in /etc/inittab as following
id:1:respawn:/var/tmp/server.sh
Refer to http://linux.about.com/od/commands/l/blcmdl5_inittab.htm for more information about the /etc/initab file.
After editing /etc/inittab restart your system.

docker attach vs lxc-attach

UPDATE: Docker 0.9.0 use libcontainer now, diverting from LXC see: Attaching process to Docker libcontainer container
I'm running an istance of elasticsearch:
docker run -d -p 9200:9200 -p 9300:9300 dockerfile/elasticsearch
Checking the process it show like the following:
$ docker ps --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49fdccefe4c8c72750d8155bbddad3acd8f573bf13926dcaab53c38672a62f22 dockerfile/elasticsearch:latest /usr/share/elasticsearch/bin/elasticsearch java About an hour ago Up 8 minutes 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp pensive_morse
Now, when I try to attach the running container, I get stacked:
$ sudo docker attach 49fdccefe4c8c72750d8155bbddad3acd8f573bf13926dcaab53c38672a62f22
[sudo] password for lsoave:
the tty doesn't connect and the prompt is not back. Doing the same with lxc-attach works fine:
$ sudo lxc-attach -n 49fdccefe4c8c72750d8155bbddad3acd8f573bf13926dcaab53c38672a62f22
root#49fdccefe4c8:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 49 20:37 ? 00:00:20 /usr/bin/java -Xms256m -Xmx1g -Xss256k -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMa
root 88 0 0 20:38 ? 00:00:00 /bin/bash
root 92 88 0 20:38 ? 00:00:00 ps -ef
root#49fdccefe4c8:/#
Does anybody know what's wrong with docker attach ?
NB. dockerfile/elasticsearch ends with:
ENTRYPOINT ["/usr/share/elasticsearch/bin/elasticsearch"]
You're attaching to a container that is running elasticsearch which isn't an interactive command. You don't get a shell to type in because the container is not running a shell. The reason lxc-attach works is because it's giving you a default shell. Per man lxc-attach:
If no command is specified, the current default shell of the user
running lxc-attach will be looked up inside the container and
executed. This will fail if no such user exists inside the container
or the container does not have a working nsswitch mechanism.
docker attach is behaving as expected.
As Ben Whaley notes this is expected behavior.
It's worth mentioning though that if you want to monitor the process you can do a number of things:
Start bash as front process: e.g. $ES_DIR/bin/elasticsearch && /bin/bash will give you your shell when you attach. Mainly useful during development. Not so clean :)
Install an ssh server. Although I've never done this myself it's a good option. Drawback is of course overhead, and maybe a security angle. Do you really want ssh on all of your containers? Personally, I like to keep them as small as possible with single-process as the ultimate win.
Use the log files! You can use docker cp to get the logs locally, or better the docker logs $CONTAINER_ID command. The latter give you the accumulated stdin/stderr output for the entre lifetime of the container each time though.
Mount the log directory. Just mount a directory on your host and have elasticsearch write to a logfile in that directory. You can have syslog on your host, Logstash, or whatever turns you on ;). Of course, the drawback here is that you are now using your host more than you might like. I also found a nice experiment using logstash in this blog.
FWIW, now that Docker 1.3 is released, you can use "docker exec" to open up a shell or other process on a running container. This should allow you to effectively replace lxc-attach when using the native driver.
http://blog.docker.com/2014/10/docker-1-3-signed-images-process-injection-security-options-mac-shared-directories/

Stop Unlisted Services

I have just reinstalled LAMPP. When I try starting LAMPP using /opt/lampp/lampp start, it says another ftp and mysql daemon is already running along with another web server. I deleted the old instance of LAMPP without stopping all the services. When I use service --status-all these services aren't listed.
Is there any way I can stop it now?
Yes, with this : pkill -f apache; pkill -f httpd
You can run that on each service you want to stop.
To test if it's still running, try pgrep -fl <APP_NAME>.
If it's the case, consider using pkill -1 -f <APP_NAME> and if still present pkill -9 -f <APP_NAME>. -9 is the higher signal. Don't use -9 by default, you will facing problems with not well closed applications (file descriptors and other system stuff)
see man 7 signal.

Resources