1

When I run a simple bash script, say myscript.sh

#!/bin/bash
sleep 30

from the terminal, and then do pgrep myscript.sh I don't get any result. Why?

2 Answers 2

1

You're probably doing this:

pgrep myscript.sh

This won't show the process you're running because it is /bin/bash that is running your script.

You should be doing:

pgrep -fl myscript.sh

To list your process.

As per man pgrep:

-f Match the pattern anywhere in the full argument string of the process instead of just the executable name.

Sign up to request clarification or add additional context in comments.

Comments

1

Your just running your bash script, u need to use -f flag, please check man page Check pgrep is installed in your machine. Just do man pgrep, if you get command not found that install the utility.

pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. All the criteria have to match.

Example usage:

pgrep name | xargs kill

If you use pgrep name | kill, the ouput of pgrep name is feed to stdin of kill. Because kill does not read arguments from stdin, so this will not work.

Using xargs, it will build arguments for kill from stdin. Example:

$ pgrep bash | xargs echo
5514 22298 23079

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.