Manga Black
Manga Black

Reputation: 197

Check running cron processes with php

I have the following result when I do a

ps ax

13790 pts/0    S      0:00 /usr/bin/php cron3.php 0
13804 pts/0    S      0:00 /usr/bin/php cron3.php 1
13819 pts/0    S      0:00 /usr/bin/php cron3.php 2
13834 pts/0    S      0:00 /usr/bin/php cron3.php 3
13851 pts/0    S      0:00 /usr/bin/php cron3.php 4
13863 pts/0    S      0:00 /usr/bin/php cron3.php 5
13879 pts/0    S      0:00 /usr/bin/php cron3.php 6
13897 pts/0    S      0:00 /usr/bin/php cron3.php 7
13913 pts/0    S      0:00 /usr/bin/php cron3.php 8
13927 pts/0    S      0:00 /usr/bin/php cron3.php 9
13941 pts/0    S      0:00 /usr/bin/php cron3.php 10
13958 pts/0    S      0:00 /usr/bin/php cron3.php 11
13969 pts/0    S      0:00 /usr/bin/php cron3.php 12
13983 pts/0    S      0:00 /usr/bin/php cron3.php 13
13996 pts/0    S      0:00 /usr/bin/php cron3.php 14
14012 pts/0    S      0:00 /usr/bin/php cron3.php 15
14026 pts/0    S      0:00 /usr/bin/php cron3.php 16
14039 pts/0    S      0:00 /usr/bin/php cron3.php 17
14052 pts/0    S      0:00 /usr/bin/php cron3.php 18
14067 pts/0    S      0:00 /usr/bin/php cron3.php 19
14083 pts/0    S      0:00 /usr/bin/php cron3.php 20
14095 pts/0    S      0:00 /usr/bin/php cron3.php 0
14113 pts/0    S      0:00 /usr/bin/php cron3.php 1

I want to able to only ensure that at 1 time, only 1 cron3.php [argv] can be run.

Meaning if

cron3.php 1 

is running in the process, I need to able know that its running by a php line or something to check.

I tried googling and the closest I found is

exec("ps auxwww|grep cron3.php|grep -v grep", $output);

However If i change it to

exec("ps auxwww|grep "cron3.php 1"|grep -v grep", $output);

It list me all that contain 1, including the process like 10, 11 , 12 etc.

Thanks

Upvotes: 0

Views: 102

Answers (2)

Kita
Kita

Reputation: 2634

Place a $ at the end of grep pattern:

 grep "cron3.php 1$"

In regular expression $ means "end of text".

Upvotes: 1

Barmar
Barmar

Reputation: 780889

Anchor it to the end of the line:

exec("ps auxwww | grep '[c]ron3.php 1$'", $output);

Using [c]ron3 is a trick to avoid having to use a second grep -v grep, since that matches cron3, but it won't match itself.

Upvotes: 2

Related Questions