Why "heavy" script in the CRON running multiple times?

Greetings,

CRON once a minute hews to the database and looks for the last entry field with a pid=0.

If it finds one, it parsed the json with the further entry in different database tables that figured out.

Recently noticed such trouble -
For example, in the parsed json is a block:
...
{
"pr_id":5,
"com_total":150,
}
...


com_total with the value (150) to dismantle the object is present only in the singular, com_total is written in a separate table. In the end, I see "duplicate", that is, the table is two/three/more than 150 times, though it 1 time. (and not all records have duplicates )

Noticed a pattern that the "heavier" json, ie the more effort for processing, the more likely duplication. ~1000-1500 blocks - good, 2000 - snake some operations will execute 2 times, 4000 - 3 times and so on.

If the reason is a certain lack of something, I was expecting that the script will simply crash. But no, it runs, but somehow very tricky.

Do not tell me what could be the reason?
Thank you.
July 9th 19 at 11:00
3 answers
July 9th 19 at 11:02
Solution
Obviously, the minute the script execution is not enough, and it handles the same data several times. You need to keep track of what the previous run ended during the launch of the new. If not, then interrupt the execution of a new start and nothing processed.

But the most correct would be to popratiloff script. Minute is a very long time for the computer, and during this time he can do a lot. Most likely, you have a suboptimal algorithm.
Clicked "Ask a question" and myself flew it in the head )) it is Useful still to paint their thoughts. Thank you. - Anissa23 commented on July 9th 19 at 11:05
July 9th 19 at 11:04
For the crown all you do have to write some additional log, such as a text or database and the script to do more tricky conditions. Got the tape 150 recorded in the songwriter if the script successfully worked in the end erase the entry. In the beginning of the script check if the script took 150 and songwriter is 150 something wrong...
July 9th 19 at 11:06
flock -n /tmp/lockfile -c /path/to/script.sh

Find more questions by tags CronPHP