Run script only once for 5 minutes (Linux) - php

I have a php script that I'd like to run for a specific amount of time (e.g. 5 minutes), but only once. With cron jobs this will run indefinitely. Is there another way?

The way to handle this is:
When something triggers to need for the cron job to run, put a flag somewhere that the cron job will read.
Have cron job that runs all the time asking "do I need to run?". It checks hte flag. If it sees it, it deletes the flag and runs for specified time. If not, just wait until it next runs (to check for the flag)
In the actual cron job, set "max_execution_time" to 5 minutes. Then it shouod stop at (or just over) 5 minutes. To be more precise, include a timer in your script running in a loop, if you can.

Related

dose new running corn job can effect on last running for a php script?

I have a php script with time of proccess between 5-15 min.
I want run this script every 5 min with a corn job.
also I luck php script during proccess....for example if script do a proccess corn job can not run that script again (return false)!
Now this is my question:
for example corn job run in 0min and 5th min 10th min 15th min 20th min...
and my script finish at 8th min for first corn job runnig.
dose php scrip break if corn job run in 5th min?
yes I luck script during proccess...but I think if corn job run at 5th min, the connection will be reset and script breaks. Am I wrong or not?
You need to implement some sort of locking mechanism.
The first started script creates a lock file in the file system. All scripts first check if such script already exists. If it does, they exit right away. If not they start to process the normal flow.
The only crucial thing here is to make sure that stale lock files do not block the processing flow. So that in case a script does not remove the lock file it created when it finishes, that sill other scripts can start processing. This is possible if you use the process ID of scripts as content of the lock file. That way each starting script can check if the process that created the existing lock is still active or not.
This all assumes that you start your scripts as local processes, so via CLI, and not as scripts being execute by your local http server. Local scripts (usually) do not have a limitation in execution time.

will it harmful for server to run a cron job in every second

For the checking of online users who doesn't log out properly but close the browser, I want to run a cron job in every 2/3 second. (I am updating database in every 10 second when logged in)
Will it harmful for server?
Cron lowest possible frequency is 1 minute so you cannot fire anything more often with it. As for overkill - it may or may not be there, but you need to review the code and load it produces yourself.
You can't run cron jobs more frequently than once a minute so this isn't possible from a cron anyways. You'd have to have a process running in a loop with sleep to achieve this, but the idea is overkill anyways - once a minute is fine.

handling concurrency in cron php

I am setting up a cron job that executes a script page.php every minute
I wanted to know about the concurrency.
The script it self take 15 minutes to complete, so how the cron will run?
Lets take a time frame of 15 minutes, how many crons will run in that time? 1 or 15?
will cron runs till its completion or it can be interrupted?
Thanks.
When you start a php script via cron it will run until it finishes or the execution time is exceeded.
The system will always start a new instance of the script whether the last script is still running or not.
If you know that the script is running a long time you should set up a bash script that checks if a script is already running and only start the php script if this is not the case.
Cron doesn't handle concurrency for you - your script must do so.
Turns out, this is quite easy, as there is a trivial PHP operation, that is atomic: unlink().
We tend to use it this way:
a global flag file enables the script
The main cron job does nothing but touch a timer flag file
Every minute, another cron job starts the worker script
This worker script tries unlink() on the global flag file
If it does not suceed (result evaluates to false), it quits (This makes sure, only one instance runs at a time)
This worker script tries unlink() on the timer flag file
If it does not suceed (result evaluates to false), it touch()es the global flag file and quits (This makes sure, one cron run starts at most one instance)
Now it runs
after finishing, it touch()es the global flag file to restart the cycle
This way, cron runs, that would result in concurrency, are left out in a loggable way.
it depends in your scheduled task/cron settings. You can configure to launch 15 times your PHP script, or launch it when the previous job is done.
Your scheduled task/cron can be interrupted by it self, by an external signal or also by the configuration you made in scheduled task/cron (stop job if it is not finished in N minutes...)
Hope that helps :)

creating cron Job command every minute

I have a question in cron jobs where i create a job to run a page every one minute.
If this page didn't finish its job completely (not executed completely) in this minute, then
will the cron job command run this page again from the beginning?
or will it run this page again but will allow the first to complete?
or will it wait for the page to complete and do the command?
or will it do some thing else????
Every cron job is run independently in a separate sub process, so the cron job will run irrespective of whether some other job is running or not.
Hence, if your script is taking more than one minute to run, and you have scheduled it to run every minute, it will have unintended consequences because two process, or n+1 process for script execution time in range [n, n+1) minutes, will now be running in different execution stage.
This will be true for any overlaps in execution time of cron jobs.

running multiple instances of cron at the same time

I have set of some entries in my database to run some scripts in php. So i will run a cron file every few mins to see if for next few mins i have some scripts to run, i will trigger them, what i am wondering what happens if i have a script to be executed at 12:10 which will execute in 5 mins, what happens if i have another request to run the same script at 12:12 while the execution of the first script is not finished yet. Is the file locked till the execution is done? or what?
Its the same file to be executed in different timings.
I think i can do this with the cron tab, but i dont prefer that.
They will have no problems running simultaneously but bear in mind you could have issues if they will be writing to the same file/database table.
It will run the script twice, thrice or as many times it likes. If you only want to run this script one at a time, you can create a lock file and then check to see if it exists. Example:
if(file_exists('/tmp/script.lock')){
exit();
}
file_put_contents('/tmp/script.lock','');
// execute code
unlink('/tmp/script.lock');

Categories