Command check process linux

How to check the process is already running or not

I want to check the particular process in already running or not. I refereed this Q&A. But I didn’t get any specific solution. Following is the example that I tried: I have created abc.sh file and run this script on background, like sh abc.sh & . Now this file is running on background and I fire the ps aux | grep «abc» command. Following is the output of this command:

prakash 3594 0.0 0.0 4388 820 pts/0 S+ 16:44 0:00 grep --color=auto abc 

After that I stop the abc.sh running script and fire the same command ps aux | grep «abc» command. But I am getting same output like:

prakash 3594 0.0 0.0 4388 820 pts/0 S+ 16:44 0:00 grep --color=auto abc 

7 Answers 7

Every process will be listed in the output of ps aux ; whether running, sleeping, zombie or stopped.

However, in your case, since you ran the process using sh abc.sh , sh is the application(shell) that is running and not abc.sh . Hence, ps aux will not contain the process abc.sh because of which grep could not yield any result.

So, the correct way you should have used it is as:

This may also return you other process that are running having the string sh anywhere in their output of ps aux .

You should note that the process will be «running» when the output of ps aux has its STAT as R . If it is something other than that, it is not running at the instance you fired the command to check the running processes. The different process states can be found in the man page for ps:

D uninterruptible sleep (usually IO) R running or runnable (on run queue) S interruptible sleep (waiting for an event to complete) T stopped, either by a job control signal or because it is being traced W paging (not valid since the 2.6.xx kernel) X dead (should never be seen) Z defunct ("zombie") process, terminated but not reaped by its parent 

You could as well run the top command to check if the process is running or sleeping and the amount of CPU, RAM it is consuming. (This will again list your process as sh ).

However, if you do want your process to be listed as abc.sh , then you should have the first line of the script you are running as:

so that the shell will know what application to use to run the script(sh in this case, change it to #!/bin/bash for bash) and then provide executable permissions to the process using:

replacing /path/to/ with the location of the abc.sh file and then run abc.sh using

again replacing /path/to/ with the location of the abc.sh file.

Источник

Linux/Unix command to determine if process is running?

I need a platform independent (Linux/Unix|OSX) shell/bash command that will determine if a specific process is running. e.g. mysqld , httpd . What is the simplest way/command to do this?

15 Answers 15

While pidof and pgrep are great tools for determining what’s running, they are both, unfortunately, unavailable on some operating systems. A definite fail safe would be to use the following: ps cax | grep command

Читайте также:  Linux x64 rpm что такое

The output on Gentoo Linux:

14484 ? S 0:00 apache2 14667 ? S 0:00 apache2 19620 ? Sl 0:00 apache2 21132 ? Ss 0:04 apache2

The output on OS X:

42582 ?? Z 0:00.00 (smbclient) 46529 ?? Z 0:00.00 (smbclient) 46539 ?? Z 0:00.00 (smbclient) 46547 ?? Z 0:00.00 (smbclient) 46586 ?? Z 0:00.00 (smbclient) 46594 ?? Z 0:00.00 (smbclient)

On both Linux and OS X, grep returns an exit code so it’s easy to check if the process was found or not:

#!/bin/bash ps cax | grep httpd > /dev/null if [ $? -eq 0 ]; then echo "Process is running." else echo "Process is not running." fi 

Furthermore, if you would like the list of PIDs, you could easily grep for those as well:

ps cax | grep httpd | grep -o '^[ ]*4*'

Whose output is the same on Linux and OS X:

The output of the following is an empty string, making this approach safe for processes that are not running:

echo ps cax | grep aasdfasdf | grep -o '^[ ]*6*'

This approach is suitable for writing a simple empty string test, then even iterating through the discovered PIDs.

#!/bin/bash PROCESS=$1 PIDS=`ps cax | grep $PROCESS | grep -o '^[ ]*2*'` if [ -z "$PIDS" ]; then echo "Process not running." 1>&2 exit 1 else for PID in $PIDS; do echo $PID done fi 

You can test it by saving it to a file (named «running») with execute permissions (chmod +x running) and executing it with a parameter: ./running «httpd»

#!/bin/bash ps cax | grep httpd if [ $? -eq 0 ]; then echo "Process is running." else echo "Process is not running." fi 

Please keep in mind that you’re simply parsing the output of ps ax which means that, as seen in the Linux output, it is not simply matching on processes, but also the arguments passed to that program. I highly recommend being as specific as possible when using this method (e.g. ./running «mysql» will also match ‘mysqld’ processes). I highly recommend using which to check against a full path where possible.

The process can be running, but stopped. So if the goal is to test if mysqld or httpd are «up and running» (responding), you should also check if it is stopped or not.

Sorry, but while the answer is certainly right from a semantic point of view I’m fully against trying to find a process by pattern matching on the process arg vector. Any such approach is doomed to fail sooner or later (you actually admit to that yourself, by saying that more checks are needed). I’ve added my own recommendation in a separate answer.

grep will also find itself running (e.g. ps cax | grep randomname will always return 0 because grep finds grep randomname (hope this is clear. ). One fix is to add square brackets around the first letter of the process name, e.g. ps cax | grep [r]andomname .

ps cax may not output command name wholly. E.g it prints «chromium-browse» instead of «chromium-browser».

You SHOULD know the PID !

Finding a process by trying to do some kind of pattern recognition on the process arguments (like pgrep «mysqld» ) is a strategy that is doomed to fail sooner or later. What if you have two mysqld running? Forget that approach. You MAY get it right temporarily and it MAY work for a year or two but then something happens that you haven’t thought about.

Читайте также:  Alt linux обновление ядра

Only the process id (pid) is truly unique.

Always store the pid when you launch something in the background. In Bash this can be done with the $! Bash variable. You will save yourself SO much trouble by doing so.

How to determine if process is running (by pid)

So now the question becomes how to know if a pid is running.

This is POSIX and hence portable. It will return the pid itself if the process is running or return nothing if the process is not running. Strictly speaking the command will return a single column, the pid , but since we’ve given that an empty title header (the stuff immediately preceding the equals sign) and this is the only column requested then the ps command will not use header at all. Which is what we want because it makes parsing easier.

This will work on Linux, BSD, Solaris, etc.

Another strategy would be to test on the exit value from the above ps command. It should be zero if the process is running and non-zero if it isn’t. The POSIX spec says that ps must exit >0 if an error has occurred but it is unclear to me what constitutes ‘an error’. Therefore I’m not personally using that strategy although I’m pretty sure it will work as well on all Unix/Linux platforms.

Источник

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).

Читайте также:  Dump резервное копирование 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.

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