I have a cron job that connect with PostgreSQL DB, to select and insert Data, it works each 2 min, am using pg_connect(); my server each day get down and gives me error in PostgreSQL log for creating multiple connections and the count of connections per user exceed 100 per day, how can i use singleton term in my cron. can any one help plz
Asked
Active
Viewed 291 times
-1
-
Um, what? The *exact text** of the error would be a good start. So would some paragraph breaks. – Craig Ringer Sep 12 '14 at 16:36
1 Answers
2
As far as I can guess, you are trying to ask:
"How do I ensure that only one copy of a cron job runs at a time".
If so, the conventional solution is the flock
shell command. The PHP flock()
function can also be used for the same purpose.
Specific to PostgreSQL, you can instead use table locks and take a NOWAIT
lock early in the cron script, so it'll error out if the lock attempt fails. Of course that'll only work if your PHP script checks for errors and aborts when it sees an error.

Community
- 1
- 1

Craig Ringer
- 307,061
- 76
- 688
- 778
-
Thank u Craig, i tried and it seems that will help me to solve this case, but i want to ask u how can i stop the script when the file is locked i don't what to keep waiting the prevision job to finish, because as I told you am running it every 2 min. – ruba Sep 12 '14 at 20:29
-
Thank u again Craig i found the answer I added LOCK_NB to the second parameter it works thank u :) – ruba Sep 12 '14 at 21:16