Linux get all running processes

Getting List of Running processes and applications

How can I see which application and what processes are running recently after the login using terminal command.

2 Answers 2

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.

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 
#!/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() 

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.

  • (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.

Источник

Читайте также:  Astra linux install fly

Check Running Process in Linux

Want to know what all processes are running on your systems? Here’s how to get details of the running processes in Linux.

As a system administrator, you might need to check all the processes that are consuming your computer’s resources.

To get the list of all the running processes, run the ps command with aux argument flags in the following fashion:

This will give you a list of all running processes by all users on your system. You may use grep to filter the process using a pattern.

Let’s see about using it in detail. I’ll also share other commands to show running processes in Linux.

ps command

The ps command is the standard command that most sysadmins use in a UNIX-like operating system.

There are several options that you can use with the ps command, but the set of options to use when you want a list of all processes is aux .

So, running the following command will show me all processes on my system:

Or, you can use the BSD-style syntax, which are still available in GNU ps

  • a : Display information about other users’ processes as well as of the user’s own (if the processes are connected to terminals i.e. daemons get excluded)
  • u : Display in a user readable format
  • x : Include processes that are not connected to a terminal i.e. include daemons

This will give you an incredibly long list of running processes that were running at the time of executing the ps command.

Most people, including me, pipe this output in grep to find a needle in the haystack.

$ ps aux | grep alacritty pratham 4653 0.1 0.0 596776 63856 ? RNsl Mar09 3:43 alacritty pratham 4974 0.0 0.0 592792 58892 ? SNsl Mar09 0:18 alacritty pratham 6287 0.0 0.0 590204 56308 ? SNsl Mar09 0:14 alacritty pratham 8241 0.0 0.0 585504 51956 ? SNsl Mar09 0:07 alacritty pratham 514536 0.0 0.0 18808 2572 pts/1 SN+ 13:56 0:00 /usr/bin/grep --color=auto alacritty

Notice how the grep command was also included in the output. This is because it also has ‘alacritty’ in the process name (as the argument).

Читайте также:  Команда linux удалить всю директорию

Be wary of this behaviour [that grep will be included in the output] if you use it in a script.

The only difference between using ps aux and ps -A is that when you use ps aux , you can easily grep the user, or alternatively, use the -u option. Whatever works for you.

Let’s see some other Linux commands to see running processes.

pgrep command

The pgrep command accepts a pattern to match and if there are any processes that match with the provided pattern, a process ID (PID) is returned to stdout.

Below is the syntax to use for pgrep command:

Let’s say, for example, I want to see the PIDs of any process that have the name ‘alacritty’. I would use the following command for that:

$ pgrep alacritty 4653 4974 6287 8241

As I ran that command, I got four PIDs indicating that four processes match with the pattern ‘alacritty’ and their PIDs are outputted to the stdout.

You can also use the -u flag (as opposed to u ) along with ps command to specify a particular user and filter out the results, making it easier to manage.

There might be multiple users on my computer using Vim, user pratham and root . If I want to filter processes and only want to see if pratham has an active Vim process or not, here is how I find it out.

$ ps -u pratham | grep vim 516525 pts/2 SNl+ 0:00 nvim

pstree command

The pstree command, as its name implies, shows a hierarchical view of parent processes and child processes.

partial output of pstree command

When run, the pstree will show a top-down, tree-like structure output of processes as shown in the picture above.

Читайте также:  Статические маршруты linux ubuntu

You can also note that the PID 1 is systemd, indicating that my Linux system uses systemd.

Since there is not much information about PID, user, start time, CPU usage etc, it is not exactly a «go-to» command. But it still helps to know which child process belongs to which parent process.

Use a system monitor

Any UNIX-like system will have a tool that you can use to monitor the usage of physical resources like CPU, RAM, Network etc.

A few tools that people prefer and are widely used are top , atop , htop and btop .

Here is the output of top command running on my computer. It shows information like total tasks, CPU and Memory usage.

picture of top command running

The atop command differs from top , obviously, but it also shows much more information about the processes like CPU, RAM usage, I/O etc.

picture of atop command running

The htop utility is a widely used resource monitoring utility to get a real-time bar of per-core CPU usage, RAM and swap.

picture of htop command running

Finally, btop is one of the newest addition to the system resource usage monitoring utilities. The best thing about it is that we get a history of CPU usage.

picture of btop command running

Conclusion

This article covers how you can view the processes running on your computer. And there are several methods of viewing it. But, the most preferred method is to use the ps command.

If you want an interactive view of the running processes (sorted by CPU usage or RAM usage etc), you can use a system monitor like top or htop.

Источник

Оцените статью
Adblock
detector