A cron job that takes 90 seconds to finish but is scheduled to run every minute will, sooner or later, start a second copy of itself while the first is still running. On shared hosting, where you cannot see running processes easily, those overlapping runs pile up quietly. Two copies send the same email twice, write the same rows twice, or fight over the same file. The fix is small: a lock that lets only one instance run at a time.
How overlapping runs happen
Cron does not know or care whether the previous run finished. It fires on schedule, every time, no matter what. A job that normally takes 20 seconds is fine on a one-minute schedule until one slow day: a large database export, a slow API response, a mail server that takes its time. That one run crosses the minute boundary, and now two copies are doing the same work at the same time. On a busy night you can end up with three or four. I have seen a nightly report script send the same summary email five times because the query got slower as the table grew.
The one-line fix: flock
On Linux, the simplest reliable lock is flock, which ships with util-linux and is available on most shared hosting servers. Wrap your cron command in it, with the -n flag so a second instance exits immediately instead of waiting in a queue:
/usr/bin/flock -n /home/username/tmp/report.lock /usr/bin/php /home/username/public_html/cron/report.php
When the first run starts, flock locks the file. When the second run starts a minute later, it sees the lock is held, fails at once, and exits with code 1. No queue builds up, no duplicate work. When the first run finishes or crashes, the operating system releases the lock automatically, so there is no stale lock file to clean up by hand. That automatic release is the reason flock is better than a lock file you create and delete yourself: a script that dies halfway through a manual lock file leaves the lock behind and blocks every future run until someone notices.
Put the lock file somewhere your cron user can write to, such as a tmp folder inside your home directory, not inside public_html. Use the full paths to flock and php in the cron command, because cron runs with a minimal PATH and the short names may not resolve. Find both paths with which flock and which php over SSH.
Locking inside the PHP script itself
If you cannot change the cron command, or you want the lock to live in the code where other developers can see it, use PHP's own flock() at the top of the script:
$lock = fopen('/home/username/tmp/report.lock', 'c');
if ($lock === false || !flock($lock, LOCK_EX | LOCK_NB)) {
fwrite(STDERR, "Another instance is already running.\n");
exit(0);
}
// ... the rest of the script runs here ...
// The lock is released automatically when the script ends.
flock($lock, LOCK_UN);
fclose($lock);
The LOCK_NB flag is what makes this non-blocking: without it, the second instance waits for the first to finish, which defeats the purpose. Note the exit code 0 here. The run did not fail, it correctly decided there was nothing to do, so it should not look like an error in your logs. Keep the lock file handle open for the whole run; closing it releases the lock early.
If you use Laravel, use withoutOverlapping
Laravel's scheduler has this built in. Add withoutOverlapping() to the scheduled task:
$schedule->command('reports:daily')
->everyMinute()
->withoutOverlapping();
Under the hood it takes a cache lock, so it works with whatever cache driver you configured. The default lock lasts 24 hours, which is too long if a run crashes and the lock stays held, so set an expiry that matches the job: ->withoutOverlapping(10) releases the lock after 10 minutes no matter what. Your single cron entry stays the same:
* * * * * cd /home/username/laravel-app && php artisan schedule:run >> /dev/null 2>&1
If your queue workers also run long jobs, the same idea applies there: a job that fails silently is a different problem, and I wrote about why Laravel queue jobs fail silently separately.
Log every run so overlaps become visible
A lock only helps if you know it is being hit. Redirect the cron output to a log file with a timestamp, and log skipped runs too:
/usr/bin/flock -n /home/username/tmp/report.lock /usr/bin/php /home/username/public_html/cron/report.php >> /home/username/logs/cron.log 2>&1
When a run is skipped, flock exits with code 1 and writes nothing. A small wrapper makes skips visible:
/usr/bin/flock -n /home/username/tmp/report.lock -c '/usr/bin/php /home/username/public_html/cron/report.php || echo "$(date): report skipped, already running" >> /home/username/logs/cron.log'
Check that log once a week. If skips appear regularly, the job is too slow for its interval. The real fix is to make it faster or run it less often, not to add a bigger lock. A lock prevents damage; it does not fix a job that has outgrown its schedule.
Pick an interval the job can actually meet
Before adding a lock, measure how long the job really takes. Time it by hand once, and add a safety margin of at least two or three times the measured runtime. A job that takes 40 seconds has no business on a one-minute schedule. Either shorten the job, split it into smaller pieces, or move it to a five-minute interval. Also set a hard timeout so a hung job cannot hold the lock forever: timeout 300 in front of the command kills it after five minutes and releases the lock with it.
Takeaway
Cron never checks whether the last run finished, so a slow day turns one job into several copies of itself. Wrap the command in flock -n, or lock inside the script with flock() and LOCK_NB, or use withoutOverlapping() in Laravel. Log skipped runs, measure the real runtime, and keep the interval comfortably above it. The lock is five minutes of work, and it removes a whole class of duplicate emails and corrupted data.