Linux checking running process

Bash check if process is running or not on Linux / Unix

Bash check if process is running or not

Bash commands to check running process:

  1. pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen.
  2. pidof command – Find the process ID of a running program on Linux or Unix-like system
  3. ps command – Get information about the currently running Linux or Unix processes, including their process identification numbers (PIDs).

Let us see some examples about checking processes that running or not in Linux and Unix systems.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

What is a Linux or Unix process?

A Linux process is nothing but an executing (i.e., running) instance of a program. For example, Apache or Nginx web server runs on Linux or Unix-like system to display web pages in the background. All running process in the background is called as Daemon. So Apache/Nginx is a class of processes that run continuously in the background, and we say nginx or httpd daemon is running on the server. However, how do you verify that Nginx or HTTPD is running? You need to use the commands.

Is nginx process is running or not?

Bash check process running with pidof command

The syntax is:
pidof program
pidof httpd
pidof mysqld
pidof nginx

Bash shell check if a process is running or not with ps

Again the syntax is:
ps -C daemon
ps -C nginx
ps -C httpd
It is common to use the grep command or egrep command with ps as follows:
ps aux | grep nginx
ps aux | egrep -i «(nginx|httpd)»

Determine whether a process is running or not using a shell script

Each Linux or Unix bash shell command returns a status when it terminates normally or abnormally. You can use command exit status in the shell script to display an error message or take some sort of action. You can use special shell variable called $? to get the exit status of the previously executed command. To print ? variable use the echo command:
pgrep -x mysqld
echo $?
pgrep -x nginx
echo $?
pidof httpd
echo $?
ps -C httpd
echo $?

A 0 exit status means the command was successful without any errors. A non-zero (1-255 values) exit status means command was failure.

Читайте также:  Как полностью удалить bitdefender с компьютера windows 10

Linux/Unix bash command to determine if process is running

It is now easy to check if the process was found or not using exit status value:

Click to enlarge

Bash shell script to check running process

Bash if..else..fi statement allows to make choice based on the success or failure of a command:

A note about service and systemctl command

One can use systemctl command to control the systemd system under Linux. It can provide status of service too. For example, find out if nginx is running or out, run:
systemctl status
systemctl status sshd
systemctl status nginx
Older Linux distros and Unix like system such as FreeBSD use service command for the same purpose. The syntax is:
sudo service status
sudo service nginx status
sudo service sshd status

Conclusion

You learned how to determine whether a process is running or not and use a conditional shell script to start/stop process based on that condition. See pgrep and bash man page here for more information.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Thank you for this article, it helped me a lot to check if a program that I run in rc.local (Raspberrry) was working or not and to check for how long it is running.

I have a trouble with the continuity of this program running. It starts automatically with the raspberry start on, but frequently the program stop working and close (never run more than two days).

I was thinking about to handling this error rebboting the system programatically every hour for example.

Could you help me to find out where to looking for a solution or if it is possible to check by a routine(code) if the program is running or not as a condition to reboot or not the system?

Источник

How to check running process in Unix using command line

Check running process in Unix

The procedure to monitor the running process in Unix using the command line is as follows:

  1. Open the terminal window on Unix
  2. For remote Unix server use the ssh command for log in purpose
  3. Type the ps aux command to see all running process in Unix
  4. Alternatively, you can issue the top command to view running process in Unix

How to manage processes from the Unix terminal

The ps command is a traditional Unix command to lists running processes. The following command shows all processes running on your system:
ps -aux
sudo ps -a

The process ID (PID) is essential to kill or control process on Unix. For example consider the following outputs:

  1. vivek – User name
  2. 43126 – PID (Unix process ID)
  3. 10:59 – Process start time
  4. vim – Actual process or command

There may be too many processes. Hence, it uses the following less command/more command as pipe to display process one screen at a time:
ps -aux | more
sudo ps -aux | less
Press q to exit from above Unix pagers. You can search for a particular Unix process using grep command/egrep command:
ps aux | grep nginx
sudo ps aux | grep vim
sudo ps -aux | egrep ‘sshd|openvpn’

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

pgrep command

Many variants of Unix comes with the pgrep command to search/find process. The syntax is:
pgrep process
sudo pgrep sshd
pgrep vim
pgrep -l nginx

The -l option passed to the pgrep command to display long format and process name too.

top command

The top command is another highly recommended method to see your Unix servers resource usage. One can see a list of top process that using the most memory or CPU or disk.
top
sudo top
sudo top [options]

Unix kill command

Want to kill a process? Try kill command. The syntax is:
kill pid
kill -signal pid
Find PID using ps, pgrep or top command. Say you want to kill a PID # 50797, run:
kill 50797
For some reason if the process can not be killed, try forceful killing:
kill -9 50797
OR
kill -KILL 50797

pkill command

If you wish to kill a process by name, try pkill command. The syntax is:
pkill processName
pkill vim
pkill firefox
pkill -9 emacs
sudo pkill -KILL php7-fpm

killall command

The killall command kills processes by name, as opposed to the selection by PID as done by kill command:
killall vim
killall -9 emacs

nice and renice command

The primary purpose of the nice command is to run a process/command at a lower or higher priority. Use the renice command to alter the nice value of one or more running Unix processes. The nice value can range from -20 to 19, with 19 being the lowest priority. Say, you want to compile software on a busy Unix server. You can set a very low priority, enter:
nice -n 13 cc -c *.c &
Set a very high priority for a kernel update. Before rebooting Unix server, run:

To change the priority of a running process, type the following:
renice -p
renice
pgrep vim
renice 10 69947
sudo renice -10 $(pgrep vim)

Conclusion

This page shows how to manage the process on the Unix terminal. For further info see man pages or our example pages:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to check running process in Linux using command line

I am a new system administrator for the Linux operating system. How do I check running process in Linux using the command line option?

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux terminal
Est. reading time 4 mintues

One can use the Linux command line or terminal app to display a running process, change their priorities level, delete process and more. This page shows how to use various commands to list, kill and manage process on Linux.

Check running process in Linux

The procedure to monitor the running process in Linux using the command line is as follows:

  1. Open the terminal window on Linux
  2. For remote Linux server use the ssh command for log in purpose
  3. Type the ps aux command to see all running process in Linux
  4. Alternatively, you can issue the top command or htop command to view running process in Linux

Let us see some example and usage in details.

Please note that vivek@nixcraft:

$ is my shell prompt. You need to type commands after the $ prompt.

How to manage processes from the Linux terminal

The ps command is a traditional Linux command to lists running processes. The following command shows all processes running on your Linux based server or system:
vivek@nixcraft:

$ ps -aux
vivek@nixcraft:

  1. root – User name
  2. 1 – PID (Linux process ID)
  3. 19:10 – Process start time
  4. /sbin/init splash – Actual process or command

There may be too many processes. Hence, it uses the following less command/more command as pipe to display process one screen at a time:
vivek@nixcraft:

$ ps -aux | more
vivek@nixcraft:

$ sudo ps -aux | less
Press q to exit from above Linux pagers. You can search for a particular Linux process using grep command/egrep command:
vivek@nixcraft:

$ ps aux | grep firefox
vivek@nixcraft:

$ sudo ps aux | grep vim
vivek@nixcraft:

$ sudo ps -aux | egrep ‘sshd|openvpn|nginx’

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Linux pgrep command

Many variants of Linux comes with the pgrep command to search/find process. The syntax is:
vivek@nixcraft:

$ sudo pgrep sshd
vivek@nixcraft:

$ pgrep vim
vivek@nixcraft:

$ pgrep firefox
vivek@nixcraft:

Linux top command

The top command is another highly recommended method to see your Linux servers resource usage. One can see a list of top process that using the most memory or CPU or disk.
vivek@nixcraft:

$ sudo top
vivek@nixcraft:

Linux htop command to check running process in Linux

The htop command is an interactive process viewer and recommended method for Linux users. One can see a list of top process that using the most memory or CPU or disk and more:
vivek@nixcraft:

$ sudo htop
vivek@nixcraft:

Linux kill command

Want to kill a process? Try kill command. The syntax is:
vivek@nixcraft:

$ kill pid
vivek@nixcraft:

$ kill -signal pid
Find PID using ps, pgrep or top commands. Say you want to kill a PID # 16750, run:
vivek@nixcraft:

$ kill 16750
For some reason if the process can not be killed, try forceful killing:
vivek@nixcraft:

$ kill -9 16750
OR
vivek@nixcraft:

$ kill -KILL 16750

Linux pkill command

If you wish to kill a process by name, try pkill command. The syntax is:
vivek@nixcraft:

$ pkill processName
vivek@nixcraft:

$ pkill vim
vivek@nixcraft:

$ pkill firefox
vivek@nixcraft:

$ pkill -9 emacs
vivek@nixcraft:

$ sudo pkill -KILL php7-fpm

Linux killall command

The killall command kills processes by name, as opposed to the selection by PID as done by kill command:
vivek@nixcraft:

$ killall vim
vivek@nixcraft:

$ killall -9 emacs

Linux nice and renice command

The primary purpose of the nice command is to run a process/command at a lower or higher priority. Use the renice command to alter the nice value of one or more running Linux processes. The nice value can range from -20 to 19, with 19 being the lowest priority. Say, you want to compile software on a busy Linux server. You can set a very low priority, enter:
vivek@nixcraft:

$ nice -n 13 cc -c *.c &
Set a very high priority for a kernel update. Before rebooting Linux server, run:

Источник

Читайте также:  Не активируется windows server 2012 r2 standard
Оцените статью