I have a job test.sh schedule to run every 5 mins, and another job test1.sh scheduled to run 12.30pm , @12.30 both jobs will run and is making a deadlock. So I need to check the job test.sh is running for that I am using :

ps -ef | grep test1.sh

but this seems to be true always as it produces a line for the same command.

  # ps -ef | grep test1.sh
team 24896   607  0 11:55 pts/11   00:00:00 test1.sh
team 24925   523  0 11:55 pts/4    00:00:00 grep test1.sh

how to avoid printing grep test1.sh?

I am very new to unix.

Thanks, Ann

  • Suggestion: just fix that deadlock. If the number of shared resources is small a strict lock ordering will do the trick. Nov 14, 2016 at 20:37

3 Answers 3


If you're just looking to see if it's running rather than pipe ps output to grep you could use pgrep. This will only output the PID of the process and be much more efficient. I am using the -x flag so that it does an exact name match for test1.sh

pgrep -x test1.sh

If you want to see the command name as well you could also use the -l flag.

pgrep -xl test1.sh
876 test1.sh

And if you wanted to do a partial match you could just remove the -x

pgrep -l test
876 test1.sh
877 test2.sh
888 test123.sh
8745 test.bin
  • Thank you so much. As a new user to this portal I am finding a way to appreciate all the answers I got ...
    – Ann
    Nov 14, 2016 at 19:17
  • Note that as of pgrep 3.3.4 from procps-ng the flag to show the full command has been changed to -a instead of -l. It seems to be there already in recent Fedora and Ubuntus at the very least. Nov 14, 2016 at 19:30

You can just ignore the grep one by one of the following:-

ps -ef | grep test1.sh | grep -v grep


ps -ef | grep "[t]est1.sh"

The second one look descent and it saves lot of time.


This happens because grep and ps are launched in parallel, thus the grep process is matched because the target string test1.sh appears as its argument in ps. A simple but probably sub-optimal way to bypass this would be:

ps -ef | grep "test1.sh" | grep -v "grep"

The second pipe takes the output of the first and excludes lines containing a match for the string "grep".

  • @Ann you should credit Ashwin by accepting his answer. EDIT: actually Zachary's answer is even more efficient. It's your call though ! Nov 14, 2016 at 19:01
  • I am thankful for all the answers. I am new to this portal. Is there any way I can mark this question as answered by giving credits to the replies I got.?
    – Ann
    Nov 14, 2016 at 19:16

Not the answer you're looking for? Browse other questions tagged .