60

From my question Can Process id and session id of a daemon differ?, it was clear that I cannot easily decide the features of a daemon. I have read in different articles and from different forums that service --status-all command can be used to list all the daemons in my system. But I do not think that the command is listing all daemons because NetworkManager, a daemon which is currently running in my Ubuntu 14.04 system, is not listed by the command. Is there some command to list the running daemons or else is there some way to find the daemons from the filesystem itself?

9
  • Are you sure it's not listed? How are you checking? I can see it on my Debian. Note that the name is network-manager, not NetworkManager.
    – terdon
    Commented Dec 22, 2014 at 10:50
  • Yes. I am sure. Nothing related to the term network is listed. Also it lists anacron which is mentioned as not a daemon in its init script.
    – Jackzz
    Commented Dec 22, 2014 at 10:52
  • Anacron not being a daemon is more a question of semantics because it is not run constantly. It is still run as a service which is what you normally refer to as daemons. Please edit your question and i) tell us which Ubuntu you are running and ii) what exactly you mean by "daemon". What is your final objective here?
    – terdon
    Commented Dec 22, 2014 at 10:56
  • I suppose any service running in the background is a daemon. I mentioned anacron because it was said in /etc/init.d/anacron that it is not a daemon. My objective is to write a C++ program to list all daemons running in my system. For that I need to know which files to parse to get the details.
    – Jackzz
    Commented Dec 22, 2014 at 11:01
  • 1
    Well, if you define daemons as services, service --status-all is what you need. Ubuntu seems to treat NetworkManager differently. I get both networking and network-manager in the output of services --status-all on Debian but only networking on Ubuntu. I think you need to define what exactly you mean by "daemon".
    – terdon
    Commented Dec 22, 2014 at 11:04

4 Answers 4

71

The notion of daemon is attached to processes, not files. For this reason, there is no sense in "finding daemons on the filesystem". Just to make the notion a little clearer : a program is an executable file (visible in the output of ls) ; a process is an instance of that program (visible in the output of ps).

Now, if we use the information that I gave in my answer, we could find running daemons by searching for processes which run without a controlling terminal attached to them. This can be done quite easily with ps:

$ ps -eo 'tty,pid,comm' | grep ^?

The tty output field contains "?" when the process has no controlling terminal.

The big problem here comes when your system runs a graphical environment. Since GUI programs (i.e. Chromium) are not attached to a terminal, they also appear in the output. On a standard system, where root does not run graphical programs, you could simply restrict the previous list to root's processes. This can be achieved using ps' -U switch.

$ ps -U0 -o 'tty,pid,comm' | grep ^?

Yet, two problems arise here:

  • If root is running graphical programs, they will show up.
  • Daemons running without root privileges won't. Note that daemons which start at boot time are usually running as root.

Basically, we would like to display all programs without a controlling terminal, but not GUI programs. Luckily for us, there is a program to list GUI processes : xlsclients! This answer from slm tells us how to use it to list all GUI programs, but we'll have to reverse it, since we want to exclude them. This can be done using the --deselect switch.

First, we'll build a list of all GUI programs for which we have running processes. From the answer I just linked, this is done using...

$ xlsclients | cut -d' ' -f3 | paste - -s -d ','

Now, ps has a -C switch which allows us to select by command name. We just got our command list, so let's inject it into the ps command line. Note that I'm using --deselect afterwards to reverse my selection.

$ ps -C "$(xlsclients | cut -d' ' -f3 | paste - -s -d ',')" --deselect

Now, we have a list of all non-GUI processes. Let's not forget our "no TTY attached" rule. For this, I'll add -o tty,args to the previous line in order to output the tty of each process (and its full command line) :

$ ps -C "$(xlsclients | cut -d' ' -f3 | paste - -s -d ',')" --deselect -o tty,args | grep ^?

The final grep captures all lines which begin with "?", that is, all processes without a controlling tty. And there you go! This final line gives you all non-GUI processes running without a controlling terminal. Note that you could still improve it, for instance, by excluding kernel threads (which aren't processes)...

$ ps -C "$(xlsclients | cut -d' ' -f3 | paste - -s -d ',')" --ppid 2 --pid 2 --deselect -o tty,args | grep ^?

... or by adding a few columns of information for you to read:

$ ps -C "$(xlsclients | cut -d' ' -f3 | paste - -s -d ',')" --ppid 2 --pid 2 --deselect -o tty,uid,pid,ppid,args | grep ^?
1
  • linux keeps many info on a virtual filesystem, procfs. so it is feasible "finding daemons on the filesystem" !!!
    – Massimo
    Commented Aug 30, 2018 at 19:30
8

With a modern system running systemd (Debian based, Fedora, RedHat/CentOs, ...), the command systemctl is there to handle everything that is controlled by systemd. So to get a list of services, you can do:

systemctl | grep daemon

Edit 2022/10 : To be more specific, systemd handles many things in the kernel, you can run $ sudo systemctl -t help to see.

To check for services, run $ sudo systemctl --type=service and grep for the one you want.

1
  • This is the correct, modern answer Commented Feb 18, 2023 at 10:28
1

If using systemd to list all currently running services (aka. daemons) run the following command:

sudo systemctl --type=service --state=active
0

I wrote for the "old" sysv initd, you have to check if it is working on your distro.

Good demons have well written startup scripts in /etc/initd

When changing runlevel, how does init know the running daemons ?

It looks for their names in the directory

/var/lock/subsys

So you can

get the names list from there

scan all the running processes and check if the name is inside the list: bingo !

To scan all the processes: list every subdirectory in

/proc

If its name is digits, it is the pid of a running process.

For example, the status of the process with pid 1234 is this file

/proc/1234/status

Open it and get the first line, starts with "Name:"

See

http://man7.org/linux/man-pages/man5/proc.5.html

https://linuxexplore.com/2014/03/19/use-of-subsystem-lock-files-in-init-script/

(sorry for the nasty formatting, I am writing from my cell phone...)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .