How to check the process is already running or not

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:

ps aux | grep sh

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:

#!/bin/sh

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:

chmod +x /path/to/abc.sh

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

/path/to/abc.sh

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


My problem with grep is that it's a whole-line parser. In your example, you're searching for "abc" but it's pulling back the instance of grep (that's looking for "abc"). A bit circular. You can filter that out but I find all that a little perverse.

I would turn to awk for a little panache.

ps aux | awk '$12=="abc.sh"'

awk splits the lines into fields based on whitespace (by default). Fields $11+ are the command column(s) so if the command is "sh abc.sh ...", $11 will be sh and $12 will be abc.sh.

If you want to control the output or chain on with Bash's && and || operators, you can but you'll need to be a little more clever. grep will exit with status code 1 (technical fail, triggers ||) if nothing is found but awk will always exit code 0. We can change this by telling it to exit if it finds something and throw a 1 if it doesn't:

ps aux | awk '$12=="abc.sh" {exit 0} END{exit 1}' && echo running || echo not running

Of course if you just care about writing output to the screen, you could do that all in awk:

ps aux | awk '$12=="abc.sh" {print "running"; exit} END{print "not running"}'

The right way to check in the current bash session if a process started in background, like sh abc.sh &, is already running or not is to use jobs builtin command.

Example:

$ sh abc.sh &
[1] 6917
$ jobs
[1]+  Running                 sh abc.sh &

That's mean that the sh abc.sh process started in background is running. If the process has finished you will see something like:

[1]+  Done                    sh abc.sh

If you have more processes running in background, you can restrict the output only to the sh abc.sh process using:

jobs sh

See help jobs for more info.

The above method will work only in the current session of bash. But if you are in another bash session (e.g. in another terminal window or tab, or in a script), aside from:

ps aux | grep "[s]h bin/test.sh"

you can use also:

pgrep -a sh

which seems more simple to me. If you will see your typed command in the output (sh abc.sh in this case), that's means that the process is still running. If not, the process has finished.