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.
Related
I want to create a PHP script that will run non stop and execute some instructions every minute/hour (according to my need). It should never die.
How should I proceed to this one? How do I start the script itself?
What Iv'e done so far is created an infinite for loop and checking the time and if it's a new minute then calling my function. But when I call the link, it shows my browser is busy.
I want the script to run on Google Compute Engine without any need to call the URL from a second computer. I want something like android foreground service but for PHP.
Cron job is the answer.
Cron is basically a job scheduling daemon. It runs in the background and is executed automatically. You can set it to run at any time that you wish to. This nature of cron makes it useful for automating tasks.
A cron job, is a task which we carry out with the help of a cron.
So if you run a php script with the help of a cron, it'll run on the background. We don't need to run any infinite loops in the script. You can also set a cron job to run at specific time. How it is run and when it is run is totally upto how you configure the cron job. For that you have to edit the crontab file. A cron tab file is a text file containing a list of commands meant to be run at specified times and the commands in the crontab file along with their run times are checked by the cron daemon, which executes them in the system background.
Now, to create or edit entries in your own cron tab file:
$ crontab -e
Add an entry in the crontab file. One thing you have to remember is, for executing php scripts, use the php executable and call the php script from your crontab as shown in the example below
*/2 * * * * /usr/bin/php /var/www/html/project/cron-file.php
This reference might be helpful to you.
Hope this answer helps.
For Unix-system, best practice for such tasks is using Cron: https://en.wikipedia.org/wiki/Cron
Not only for PHP, but any periodical tasks.
Others have asked similar questions, but from what I understand their scripts are automated, but when you go to www.mydomain.com/myscript.php, the script still runs.
Or at least that's how it was for me when I set up a cron job in the cpanel for my script. Is there any way I can make it so the script doesn't run when I access that URL, but rather is static, except for its scheduled updates?
I've seen solutions where the script itself is edited to check the last time it ran, and if it was less than X hours ago, it presents that log file. The problem is I don't know where to find that log file. I'm using BlueHost, if that matters.
You could add something like if ($argv[1] == 'RUN') at the top of your php script (before it starts executing your stuff).
Then you just need to pass that parameter in your cron command, i.e.: 4 4 * * * /usr/bin/php /var/www/mydomain.com/myscript.php RUN to run at 04:04 every day (n.b. easy webtool to check your cron statement is correct - http://cronchecker.net/)
This way, you can keep your script where it is, but it won't execute when you browse to it.
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.
Is it possible to setup a cron job from within a PHP script on a LAMP server to run another PHP script? I'm a newb with cron jobs but I do know php quite well. Do I have to access the shell from PHP to set up the cron job? How would I code that if, for example, I want to execute execute.php automatically in 5 minutes after it is called from call.php?
Edit: Just to be clear, I only want to run execute.php once 5 minutes after it is called, and not have it repeat daily or anything like that. So, after it is executed by the cron job, the cron job should be discarded.
Cron doesn't work exactly like that, but you can set something up to create the functionality you want.
I would first set up a cron entry to execute execute.php every minute.
Then, when call.php is run, call.php makes an entry in a database table or flat file with the time that execute.php should be called.
When the cron entry is run, execute checks the database table or flat file to see if it's supposed to run the code in the file at that time, and if it is, runs it.
Use sleep at the beginning of execute.php
sleep(5*60);
//Rest of the code
It should by called like this:
require_once("execute.php");
However, call.php will not send any response for 5 minutes
I was wondering how to make a php daemon script that runs one time at the day?
Do you know any good frameworks with benefits?
or is it just small code?
Thanks
I was wondering how to make a php deamon script that runs one time at
the day?
In order to do this, get familiar with cron jobs. A cron job is a function that gets executed by the server on a time interval. Usually you'd edit your "crontab" by executing crontab -e
Then, once inside, you'd write the interval you want, followed by the command.
Typically it looks like:
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log
Since its PHP, you can either a) run your php command as a php cli command, OR b) you can make the command get executed when a particular page is run... and just execute that in cron via a curl -X GET 'http://url/' (etc.)
Also, note that you can write all of your stuff in a shell script file and actually run that file as your cron command... that reduces line-item complexity
cron
Sorry I haven't closed this one.
I actually discovered that my host didn't allowed cron jobs running. So I found a relevant homepage that offer a free service to make a request for me when I needed. In my case, I have specified a url link that should be requested to my RESTful API each day.
The link is here and works like a charm :)