I need to have an if statement in a script to run if there are certain processes running. Easiest way I can see to do this is to run a ps and grep the results based on what I am looking for:
$ ps -ef | grep wtrs
---
webtrend 5046 1 0 May 12 ? 0:28 /webtrends/versions/6.1/wtrs_ui -start
---
webtrend 5030 1 0 May 12 ? 3:29 /webtrends/versions/6.1/wtrs -start
---
webtrend 3550 5030 25 07:03:40 ? 91:09 /webtrends/versions/6.1/wtrs 314 -child
---
webtrend 6473 3999 0 09:10:08 pts/4 0:00 grep wtrs
---
webtrend 4409 5030 11 07:35:54 ?60:29 /webtrends/versions/6.1/wtrs 315 -child
("--" added in as line seperators)
Normally, I would expect to see the first, second, and fourth lines. That means everything is running normally. The third and fifth lines in that example are present because the program (wtrs) is currently executing certain commands.
So, what I want to do is an if statement that says (in English):
If wtrs is present in the above example 3 or fewer times, execute the following commands, otherwise do something else.
So what I was thinking of doing was first sending the output of the ps command into a text file:
$ ps -ef | grep wtrs > test.txt
Then the if statement could say:
If test.txt < 4 lines long, execute.
Anyone know how something like that would be possible, or if it would even be possible?