I am using the following code:
$fp = fopen("lock.txt", "w");
if (flock($fp, LOCK_EX|LOCK_NB)) { // do an exclusive lock
//Do something here.
}else{
echo "Could not lock file!";
}
This code runs fine when I run my script from my browser (it locks and I can only run it once) but when running it via cron job, the lock.txt file isn’t even created. I am a little stumped as I presume the Cron runs as the same user that runs the browser version so it obviously has permissions.
Anyone else had something like this?
EDIT: Ok the file is created now I have done specific paths to the directory. However, the file is overwritten everytime the cron starts an isn’t actually locking the file.
This is the correct way to do that.