-1

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

S-Man
  • 22,521
  • 7
  • 40
  • 63
ruba
  • 147
  • 1
  • 2
  • 7

1 Answers1

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.

Here's a nearly identical answer I prepared earlier.

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