Getting List of Running processes and applications
Solution 1:
Generally ps
command does it.
If you type man ps
, you will get the manual for this command, and there you can check which flag you will need. For example, ps -e
will list all running processes in the system.
Another command is top
which will show an active view of all running process.
Solution 2:
The following script lists all processes, and splits them in applications and other processes.
As definition for an application, I practice that the process is initiated from a .desktop
file (since practically all applications are represented by a .desktop
file), and that the .desktop
file appears in Dash (the .desktop
file has no line: NoDisplay=true
).
Work to be done:
The script, as it is, derives the application's process name from the (last section of-) the command, found in the desktop file, and also from information, found in the possible symlinks it might refer to (e.g. in case of LibreOffice
> process name: soffice.bin
). In some cases however, an application runs from a remote script, called from the .desktop
file. In those cases, the process will not be recognized as an application.
The script gives an output like:
Processes, related to applications:
PID TTY TIME CMD
1933 ? 00:03:55 firefox
18091 ? 00:00:00 dia
18162 ? 00:00:01 soffice.bin
31167 ? 00:00:06 alarm-clock-app
31174 ? 00:00:09 nautilus
31301 ? 00:00:20 dropbox
31998 ? 00:01:35 idle3
Other processes:
PID TTY TIME CMD
1 ? 00:00:01 init
2 ? 00:00:00 kthreadd
3 ? 00:00:02 ksoftirqd/0
5 ? 00:00:00 kworker/0:0H
7 ? 00:00:15 rcu_sched
8 ? 00:00:08 rcuos/0
etc...
The script
#!/usr/bin/env python3
import os
import subprocess
def createlist_appcommands():
dtfile_dir = "/usr/share/applications"
dtfile_list = [item for item in os.listdir(dtfile_dir) if item.endswith(".desktop")]
commands = []
for item in dtfile_list:
try:
with open(dtfile_dir+"/"+item) as data:
searchlines = data.readlines()
command = [line for line in searchlines if line.startswith("Exec=")
and not "NoDisplay=true\n" in searchlines
][0].replace("Exec=", "").replace("\n", "").split("/")[-1].split(" ")[0]
commands.append(command)
except Exception:
pass
return commands + [trace_symlinks(item) for item in commands if not trace_symlinks(item)== None]
def trace_symlinks(command):
target = subprocess.Popen(["which", command], stdout=subprocess.PIPE)
location = (target.communicate()[0].decode("utf-8")).split("\n")[0]
check_filetype = subprocess.Popen(["file", location], stdout=subprocess.PIPE)
filetype = (check_filetype.communicate()[0].decode("utf-8")).split("\n")[0]
if "symbolic link" in filetype:
return filetype.split("/")[-1].replace("' ", "")
else:
pass
def createlist_runningprocs():
processesb = subprocess.Popen(["ps", "-e"], stdout=subprocess.PIPE)
process_listb = (processesb.communicate()[0].decode("utf-8")).split("\n")
linked_commands = [(item, item[24:]) for item in process_listb][1:]
applist = createlist_appcommands()
print("Processes, related to applications:\n PID TTY"+" "*10+"TIME CMD")
matches = []
for item in applist:
for i in range(0, len(linked_commands)):
if item[:15] in linked_commands[i][1] and len(item[:15])/len(linked_commands[i][1]) > 0.5:
matches.append(i)
matches = sorted(matches)
for i in range(0, len(linked_commands)):
if i in matches:
print(linked_commands[i][0])
print("\nOther processes:\n PID TTY"+" "*10+"TIME CMD")
for i in range(0, len(linked_commands)):
if not i in matches:
print(linked_commands[i][0])
createlist_runningprocs()
How to use
Copy the script in an empty file, save it as processes.py
, run it by the command:
python3 /path/to/processes.py
Edit: updated my answer, rewrote the script.
Improvements:
(much) better performance
the script now traces and recognizes applications, initiated via symlinks (which may have another process name). Although exceptions are always possible, they should be rare now.