0

I want to know if there are other users that are running watch on the same machine in which command X is being executed. How can I do that?

terdon
  • 100,812
Pino
  • 101

1 Answers1

0

To see what processes are running on a machine, you can use ps. This is a command with a huge number of options, so you might want to take a look at man ps to learn a little about it.

To check whether anyone is running watch, you would do:

 $ ps aux | grep -w watch
terdon   15915  0.0  0.0  15912  2676 pts/4    S+   15:58   0:00 watch ls
terdon   16123  0.0  0.0  13948  2264 pts/5    S+   15:58   0:00 grep --color -w watch

The -w tells grep to only match whole words. Otherwise, processes like watchdog would also be listed. However, that also returns the grep process itself. To avoid that, you can use pgrep:

$ pgrep -xl watch
15915 watch

The -x makes it look for exact matches only and the -l makes it also list the process name and not just its PID.

Now, if you want to check for both MyOwnCommand and watch, you could do:

$ pgrep MyOwnCommand >/dev/null  && pgrep -xl watch
15915 watch

The && means that the second pgrep will only be run if the first was successful, if MyOwnCommand is running. The >/dev/null discards the output of the first pgrep.

terdon
  • 100,812