handling concurrency in cron php - 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 :)

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.

How can I create, monitor and run/stop a process in linux

I have a cron which runs every minute but the thing is it queues every request from the past minute and executes some tasks one after another. I want to run a background process which will run infinite time. I'll check if there is any new request came in & will process that immediately.
do {
//do my stuff
} while(true)
I need to know the command to check if the process is running or not, if not then start this, else do nothing
FYI - I'm not a linux guy and dont know anything about bash or shell. I need PHP code which I can add in the every minute cron which will just monitor this process is running or not.
What you are looking for is a service control and/or a watchdog. You can use D. J. Bernstein Daemontools or similar software.
Also, if you want to do it in PHP you could, inside the start part of your daemon (that is what you are building) raise a flag (a file), then withing a cron job, run another program to check if the flag is raised (the file exist) every N minutes.

Execute php script Sequentially and Continuously

I have developed a script which should execute continuously and sequentially. Now on creating a cron job for this script, it keeps executing asynchronously.
1) I kept a while loop in script and thought of executing this script once so i used #reboot to execute the script once but if apache craches then this script will not start executing by its own ??
2)Using * * * * * cron it executes this script but creates multi-thread and all cron overlaps on the server eventually.
I have ran out of ideas how to execute a server script continuously and sequentially even if apache server restarts.
You're asking for:
a script which should execute continuously and sequentially
That's the definition of a daemon. You can use upstart to easily create a daemon with your php code.
Here you can find a good article to explain how create a daemon with upstart and node.js (but is easy to adapt to a php script): http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/
cron is for repeating jobs on a timed basis (min interval: 1 minute). It's intended for scripts that eventually exit and need to be restarted from scratch. Sounds like your script is essentially trying to be a daemon - started once, then left running permanently.
Instead of cron to repeatedly start new copies of the script, simply start your script ONCE via an init script, e.g. /etc/rc.local
There are some ideas to deal with checking if a script is running in this question, and you could have a cron run every couple of minutes to ensure its running and starting it if not.

Run script only once for 5 minutes (Linux)

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.

Does a cron job kill last cron execution?

I have a cron job the executes a PHP script. The cron is setup to run every minute, this is done only for testing purposes. The PHP script it is executing is designed to convert videos uploaded to the server by users to a flash format (eg... .flv). The script executes fine when manually doing it via command line, however when executing via cron it starts fine but after one minute it just stops.
It seems that when the next cron is executed it "kills" the last cron execution.
I added the following PHP function:
ignore_user_abort(true);
In hopes that it would not abort the last execution, I tested setting the cron to run every 5 minutes, which works fine, however a conversion of a video may take over 5 minutes so I need to figure out why its stoping when another cron is executed.
Any help would be appreciated.
Thank you!
EDIT:
My cron looks like:
*/1 * * * * php /path_to_file/convert.php
I don't think cron kills any processes. However, cron isn't really suitable for long running processes. What may be happening here is that your script tramples all over itself when it is executed multiple times. For example, both PHP processes may be trying to write to the same file at the same time.
First, make sure you not only look in the php error log but also try to capture output from the PHP file itself. E.g:
*/1 * * * * * php /path/to/convert.php & >> /var/log/convert.log
You could also use a simplistic lockfile to ensure that convert.php isn't executed multiple times. Something like:
if (file_exists('/tmp/convert.lock')) {
exit();
}
touch('/tmp/convert.lock');
// convert here
unlink('/tmp/convert.lock');
cron itself won't stop a previous instance of a job running so, if there's a problem, there's almost certainly something in your PHP doing it. You'll need to post that code.
No, it will not. You can keep a second process from running by creating a lock file that the script checks for on each run. If the file exists, it does not run. This should also, if appropriate, be used in conjunction with a maximum execution time so that one process does not stall future executions indefinitely. The lock file can just be an empty plain text file called /tmp/foo.lock.

Categories