Linux list running applications

How do I get a list of running applications by using the command line?

I want to list only running application like: Firefox, gedit, Nautilus, etc. using the command line. Note: I don’t want to list all running process, only applications which are running (say manually launched GUIs).

«only list application which is running» what do you mean? all applications (known also as processes) are running. You mean only opened windows, like ‘firefox, empathy and thunderbird’ or all process that your user started like ‘unity, compiz, gnome-terminal, apt-get’?

3 Answers 3

A combination of wmctrl and xprop offers many possibilities.

The output could look in this case similar like this:

"Firefox", window id: 0x032000a9 "Gnome-terminal", window id: 0x03a0000c "Thunar", window id: 0x03600004 "Geany", window id: 0x03c00003 "Thunar", window id: 0x0360223e "Mousepad", window id: 0x02c00003 "Mousepad", window id: 0x02c00248 "Xfce4-terminal", window id: 0x03e00004 
running_gui_apps() < applications=() # loop through all open windows (ids) for win_id in $( wmctrl -l | cut -d' ' -f1 ); do # test if window is a normal window if $( xprop -id $win_id _NET_WM_WINDOW_TYPE | grep -q _NET_WM_WINDOW_TYPE_NORMAL ) ; then # filter application name and remove double-quote at beginning and end appname=$( xprop -id $win_id WM_CLASS | cut -d" " -f4 ) appname=$appname=$ # add to result list applications+=( "$appname" ) fi done # sort result list and remove duplicates readarray -t applications < <(printf '%s\0' "$" | sort -z | xargs -0n1 | uniq) printf -- '%s\n' "$" > 
Firefox Geany Gnome-terminal Mousepad Thunar Xfce4-terminal 

You can add the function to your ~/.bashrc or run it from an script file.

Up-voted due two reasons: (1) it actually answered the question; and, (2), most importantly, didn’t try to complicate the question with additional information that very clearly weren’t in the scope of the question — these kind of information cause more confusion to the users than help them (us) to understand and solve the problem.

wmctrl -l could be a thing you wanted. First install it

sudo apt-get install wmctrl 

You can also combine it with System Monitor’s list, by default it shows «All my processes» which means all process that belong to you as a user.

To have only names of applications, run:

no, i opened firefox browser. Your command shows 0x03c000b3 0 avinash-Lenovo-IdeaPad-Z500 How list of running applications by command? — Ask Ubuntu — Mozilla Firefox plus some extra lines about Desktop,Hud,etc.

@AvinashRaj this is an addition, wmctrl -l|awk ‘; print $out>’ , will add to my answer. Thank you for pointing out.

Introduction

The power of xdotool and wmctrl comes out when you need to perform manipulation on the windows, such as moving or resizing. However, I strongly believe that for the purpose of only listing running programs and information about them, xprop and qdbus are two sufficient tools and installing xdotool and wmctrl unless the user wants those for additional functionality — is a purposeless task. In this answer I would like to present two scripting solutions with xprop and qdbus .

Note that I am by no means against xdotool or wmctrl . I’ve used them extensively myself , but I find them more powerful when combined with other tools. Here are just a few examples where I’ve used them :

Xprop

The script bellow uses only xprop to extract list of the active windows , filter out only true windows ( not dock type suck as Unity Launcher or Unity Panel ) and display their info:

$ bash xprop_windows.sh XID TYPE TITLE -------------------------------- 56623112| "x-terminal-emulator", "X-terminal-emulator"| "sakura" 81789126| "Navigator", "Firefox"| "Restore Session - Mozilla Firefox" 82002372| "Navigator", "Firefox"| "gui - How do I get a list of running applications by using the command line? - Ask Ubuntu - Mozilla Firefox" 33554444| "gnome-terminal", "Gnome-terminal"| "\"Terminal\"" 33554486| "gnome-terminal", "Gnome-terminal"| "\"Terminal\"" 
get_hex_xids() < xprop -root -notype _NET_CLIENT_LIST | \ awk 'BEGIN\ ' > convert_hex2dec() < HEXIDS=$(get_hex_xids) echo $HEXIDS | bc >print_header() < printf "%s\t%s\t%s\n" "XID" "TYPE" "TITLE" printf "%s\n" "--------------------------------" >list_info() < convert_hex2dec | while read line; do TYPE=$( xprop -id $line _NET_WM_WINDOW_TYPE | awk -F '=' '' ) if [ $TYPE != "_NET_WM_WINDOW_TYPE_NORMAL" ]; then continue fi -id $line WM_CLASS | awk -F '=' '' ) NAME=$( xprop -id $line _NET_WM_NAME | awk -F '=' '' ) printf "\n%s|%s|%s\n" "$line" "$CLASS" "$NAME" done > print_header list_info 

Qdbus

The code bellow performs essentially the same task, however it filters out applications first, then lists its children windows , and finally provides info about them.

$ bash ~/bin/qdbus_windows.sh Name: Terminal Active :false Children: 33554486|false|""Terminal"" 33554444|false|""Terminal"" -------------- Name: Firefox Web Browser Active :false Children: 82002372|false|"gui - How do I get a list of running applications by using the command line? - Ask Ubuntu - Mozilla Firefox" 81789126|false|"Restore Session - Mozilla Firefox" -------------- Name: MY CUSTOM TERMINAL Active :true Children: 56623112|true|"sakura" -------------- 
#!/bin/bash get_window_paths() < qdbus org.ayatana.bamf /org/ayatana/bamf/matcher org.ayatana.bamf.matcher.WindowPaths >get_running_apps() < qdbus org.ayatana.bamf /org/ayatana/bamf/matcher org.ayatana.bamf.matcher.RunningApplications >list_children() < qdbus org.ayatana.bamf "$1" org.ayatana.bamf.view.Children >window_info() < for window in "$@" ; do XID=$TYPE=$(qdbus org.ayatana.bamf $window org.ayatana.bamf.window.WindowType) NAME="$(qdbus org.ayatana.bamf $window org.ayatana.bamf.view.Name)" ACTIVE=$(qdbus org.ayatana.bamf $window org.ayatana.bamf.view.IsActive) MONITOR=$(qdbus org.ayatana.bamf $window org.ayatana.bamf.window.Monitor) # printf "%s|%s|%s|%s\n" $TYPE $MONITOR $ACTIVE "$NAME" printf "%s|%s|\"%s\"\n" $XID $ACTIVE "$NAME" done > window_paths=( $( get_window_paths | tr '\n' ' ') ) apps_list=( $( get_running_apps | tr '\n' ' ' ) ) for app in $ ; do #echo $app printf "Name: " qdbus org.ayatana.bamf $app org.ayatana.bamf.view.Name printf "Active :" qdbus org.ayatana.bamf $app org.ayatana.bamf.view.IsActive printf "Children:\n" # list_children $app windows=( $( list_children $app | tr '\n' ' ' ) ) window_info "$" printf "%s\n" "--------------" done 

A bit simpler command but requires filtering out the output uses Unity’s window stack dbus interface. Here is essentially a function I have in my .mkshrc

window_stack() < qdbus --literal com.canonical.Unity.WindowStack /com/canonical/Unity/WindowStack \ com.canonical.Unity.WindowStack.GetWindowStack | \ awk -F '|\]|,/,"");gsub(/\[/,"\n");print $2>' | \ awk '!/compiz/&&!/^$/ && $4!="\""$3"\"" < L[n++] = $0 >\ END < while(n--) print L[n] >' > 
$ window_stack Argument: (usbu) 56623112 "x-terminal-emulator" true 0 Argument: (usbu) 82002372 "firefox" false 0 Argument: (usbu) 81789126 "firefox" false 0 Argument: (usbu) 33554486 "gnome-terminal" false 0 Argument: (usbu) 33554444 "gnome-terminal" false 0 

Источник

Linux List Processes – How to Check Running Processes

Bolaji Ayodeji

Bolaji Ayodeji

Linux List Processes – How to Check Running Processes

Every day, developers use various applications and run commands in the terminal. These applications can include a browser, code editor, terminal, video conferencing app, or music player.

For each of these software applications that you open or commands you run, it creates a process or task.

One beautiful feature of the Linux operating system and of modern computers in general is that they provide support for multitasking. So multiple programs can run at the same time.

Have you ever wondered how you can check all the programs running on your machine? Then this article is for you, as I’ll show you how to list, manage, and kill all the running processes on your Linux machine.

Prerequisites

  • A Linux distro installed.
  • Basic knowledge of navigating around the command-line.
  • A smile on your face 🙂

A Quick Introduction to Linux Processes

A process is an instance of a running computer program that you can find in a software application or command.

For example, if you open your Visual Studio Code editor, that creates a process which will only stop (or die) once you terminate or close the Visual Studio Code application.

Likewise, when you run a command in the terminal (like curl ifconfig.me ), it creates a process that will only stop when the command finishes executing or is terminated.

How to List Running Processes in Linux using the ps Command

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.

To test this, just open your terminal and run the ps command like so:

Screenshot-2021-06-28-at-3.25.33-PM

This will display the process for the current shell with four columns:

  • PID returns the unique process ID
  • TTY returns the terminal type you’re logged into
  • TIME returns the total amount of CPU usage
  • CMD returns the name of the command that launched the process.

You can choose to display a certain set of processes by using any combination of options (like -A -a , -C , -c , -d , -E , -e , -u , -X , -x , and others).

If you specify more than one of these options, then all processes which are matched by at least one of the given options will be displayed.

Screenshot-2021-06-28-at-3.55.10-PM

Type man ps in your terminal to read the manual for the ps command, which has a complete reference for all options and their uses.

To display all running processes for all users on your machine, including their usernames, and to show processes not attached to your terminal, you can use the command below:

Here’s a breakdown of the command:

Screenshot-2021-06-28-at-4.39.05-PM

  • ps : is the process status command.
  • a : displays information about other users’ processes as well as your own.
  • u : displays the processes belonging to the specified usernames.
  • x : includes processes that do not have a controlling terminal.

This will display the process for the current shell with eleven columns:

  • USER returns the username of the user running the process
  • PID returns the unique process ID
  • %CPU returns the percentage of CPU usage
  • %MEM returns the percentage memory usage
  • VSV returns the virtual size in Kbytes
  • RSS returns the resident set size
  • TT returns the control terminal name
  • STAT returns the symbolic process state
  • STARTED returns the time started
  • CMD returns the command that launched the process.

How to List Running Processes in Linux using the top and htop Commands

You can also use the top task manager command in Linux to see a real-time sorted list of top processes that use the most memory or CPU.

Type top in your terminal and you’ll get a result like the one you see in the screenshot below:

Screenshot-2021-06-28-at-4.27.28-PM

An alternative to top is htop which provides an interactive system-monitor to view and manage processes. It also displays a real-time sorted list of processes based on their CPU usage, and you can easily search, filter, and kill running processes.

htop is not installed on Linux by default, so you need to install it using the command below or download the binaries for your preferred Linux distro.

sudo apt update && sudo apt install htop

Just type htop in your terminal and you’ll get a result like the one you see in the screenshot below:

Screenshot-2021-06-29-at-4.49.09-AM

How to Kill Running Processes in Linux

Killing a process means that you terminate a running application or command. You can kill a process by running the kill command with the process ID or the pkill command with the process name like so:

To find the process ID of a running process, you can use the pgrep command followed by the name of the process like so:

To kill the iTerm2 process in the screenshot above, we will use any of the commands below. This will automatically terminate and close the iTerm2 process (application).

Conclusion

When you list running processes, it is usually a long and clustered list. You can pipe it through less to display the command output one page at a time in your terminal like so:

or display only a specific process that matches a particular name like so:

I hope that you now understand what Linux processes are and how to manage them using the ps , top , and htop commands.

Make sure to check out the manual for each command by running man ps , man top , or man htop respectively. The manual includes a comprehensive reference you can check if you need any more help at any point.

Thanks for reading – cheers! 💙

Источник

Читайте также:  Linux ruby gem path
Оцените статью
Adblock
detector