Cron command run php script every 60 seconds and then overload everything - php

I have the task in cron: every 3 hours to run php script.
I tried with WGET and PHP as well, but got the same problem. Sometimes script works more than 2 minutes, but mostly 30 seconds enough. And if execution time more than 60 seconds it get dropped (504 getway) and cron run this script again. And then again and again. Fatal overload in several hours.
I tried this with a huge amount of different syntax, but fail:
php -q /var/www/webmy/data/www/website.com/news.php
/usr/bin/wget -O - -q -t 1 http://website.com/news.php
How can I manage the command with only 1 try to run my script? I don't need it to run million times in every 60 seconds. Any limitations?
Maybe I can limit/drop execution time to 20 seconds to prevent any inappropriate script running. I just need to run script, I don't need system to wait any time, the script finishes task in any way.

You can set the maximum execution time directly in the php script with set_time_limit ( int $seconds );
see more here

First of all, the wget suggestion is a bad one. If you're going to use a PHP script for a cron task, you're better off running it as a command line script, thus running it directly and not through a web server.
Assuming you don't rely on server information or GET/POST variables in your script: have you tried running it once manually? Is anything strange happening when you do so?
A simple crontab entry like the one below will run your script only once every three hours:
* */3 * * * php /path/to/script.php

If you want guaranteed avoiding of two scripts running in same time, you should use some kind of locking, e.g. simplest file locking with flock

Related

Run PHP file without opening it

Is it possible, to run my PHP code without opening it?
For example, I have "test.php" which includes the following SQL code:
`INSERT INTO Humans(Name) VALUES ('Ric')`
I want to run this code in every 30 seconds, but the problem is that I can't open it every 30 seconds. :)
You can schedule a task if you're using windows, or a cron job if you're on linux.
For these cases, it is for the majority of web hosting solutions available CRON function (automatic call PHP scripts at a specified time).
The question is whether your web hosting provider allows calls PHP scripts in so short a time as 30 seconds as you say.
Under Linux, cron tasks cannot run every 30 seconds as the cron granularity is only 1 minute.
If you want you can have a workaround by create a script that executes every minute but runs the command twice
whatever.sh
#!/bin/sh
cd /the/directory/where/the/script/is
php task.php
sleep 30s
php task.php
Make sure it's executable
chmod +x whatever.sh
Then setup cron to execute the script every minute
* * * * * /path/to/whatever.sh

How to specify run timing for bash scripts?

I was looking for a way to continuously run a PHP script every 15 seconds online, so that I may manage some accounts using an API. I was able to find a script that satisfies what I was looking for as follows:
#!/bin/bash
#This script runs every 15 seconds
#This script is ran in /etc/rc.local (startup)
while (sleep 15 && php test.php) &
do
wait $!
done
This script works perfectly and runs every 15 seconds. However, now I want to modify the script such that it may
do what the script is already doing
run a second script, every 5 minutes
Is there a way to modify the current while loop so that I may achieve this? Thanks!
If you really want to use a loop
Runs forever until you terminate it, although yours also does.
Change sleep to whatever interval you want, i just set it to 1 for this example.
Set up if statements and use modulus to set the time frame for each one, also probably want to set count back to 0 in the highest timed if to stop it getting too large.
You can add as many as you want for as many times as you want :)
#!/bin/bash
while (true)
do
sleep 1
if (( count % 15 == 0 )) ;then
php test.php
fi
if (( count % 300 == 0 )); then
SecondScript
count=0
fi
(( count = count +1 ))
done
There are much better ways to do this because PHP requires and HTTP request to run that file.
Run a cron job to run this request every set interval (not recommended)
Use another program like python which can run a daemon to do this
Do the calculation every time the page is requested (recommended and almost always possible)
The right way to do such kind of things is via "Cron Job".
The software utility Cron is a time-based job scheduler in Unix-like
computer operating systems. People who set up and maintain software
environments use cron to schedule jobs (commands or shell scripts) to
run periodically at fixed times, dates, or intervals.
source: http://en.wikipedia.org/wiki/Cron
Useful Source: How to get a unix script to run every 15 seconds?

save bit of realtime data 4 times per day with php without human interaction

We're building an application that uses data from an api. We don't want to proces all this data because it's too much. We want to capture the data 4 times per day. Therefore we need a php script that saves this to the database without a button or human help.
We know how to get the data and understand how to upload this to a database. We don't know how to do this timed and if this is even possible.
We tried to do this with this code, but the page needs to be refreshed to echo.
if (date('His') == 114300) {
echo date('His');
}
A PHP script only runs when it's called via a request from a browser or another client, or when run through the command line.
You could, in theory, create a script with infinite execution time, and have that running. The script would need to check the server time and make the requests. The problem would be that it'd stop if the server restarted, etc. It's also highly inefficient.
Just call your script with a cronjob, four times a day, e.g. like so:
0 */6 * * * curl -k http://example.com/download.php >/dev/null 2>&1
The /6 means every six hours. The curl command calls the script and >/dev/null discards the output.
The best way to do this is by creating a cron job that runs 4 times a day
0 13,14,15,16 * * * /usr/bin/php path/to/cron.php &> /dev/null
your path/to/cron.php will be run at 13:00, 14:00, 15:00 and 16:00
http://www.thegeekstuff.com/2011/07/php-cron-job/
If you have a server (standalone / vps, with shell access), install a cron job.
If you have cpanel access, find the cron job setting.

Creating a crontab that executes a php script and 1 hour later removes itself

I'm trying to create a phpscript that creates a crontab that starts an application and shuts it down after 1 hour. I've figured out how to start the application and how to kill the process. All that is left is creating the cronjob, executing it 1 hour from now and it removing itself.
Is this possible? If so, how?
Have you thought about using the at daemon? It is not as popular as cron but does exactly what you want: run a certain command a single time at a particular point in time.
If you can execute shell scripts on the server you might be able to write a shell script that runs the program in background, sleeps for 1h and stops it afterwards if necessary. This would reduce the number of at/cron queue items.
To start the application simply create a crontab:
crontab -e
In your command prompt. Then write the crontab with the time you want it to start and the path to where the php script is stored:
10 10 * * * /Users/you/phpinhere/myphp.php &> /Users/you/output
You can direct it to an output file so then you can read if there area any errors etc.
Then do another cron underneath your first one for an hour later, and run the script to kill it.
I would create 2 cron jobs: One starts the process and one will kill it one hour later.

long time cron job via wget/curl?

I am working on cron jobs for my php app and planning to use cron via wget/curl.
Some of my php cron jobs can take 2-3 hours. How to let php work 2-3 hours from cron tab ? Is it good practice to run such long time jobs via cron wget/curl ? Anyone got experience on this ? I also have email queue and i needs to be run every 10 seconds, but cron tab is minute level. Any suggest on this case ?
Thanks for reading.
When you use wget/curl, you are requesting for a page from a webserver. Every webserver will have a time out period, so this might time out.
Also some of the hosting providers may stop the process running beyond certain minutes ( basically done to control the rogue threads).
So it is not advisable to schedule using wget/curl if the job takes more than few minutes.
Try scheduling it using actual scheduler. You can run php from command line
php [options] [-f] [--]
[args...]
php command should be on the path.
You can use the following at the start of your script to tell PHP to effectively never time out:
set_time_limit(0);
What may be wiser is, if crontab is going to run the script every 24 hours, set the timeout to 24 hours so that you don't get two copies running at the same time.
set_time_limit(24*60*60);
Crontab only allows minute-level execution because, that's the most often you should really be launching a script - there are startup/shutdown costs that make more rapid scheduling inefficient.
If your application requires a queue to be checked every 10 seconds a better strategy might be to have a single long-running script that does that checking and uses sleep() occasionally to stop itself from hogging system resources.
On a UNIX system such a script should really run as a daemon - have a look at the PEAR System_Daemon package to see how that can be accomplished simply.
I've just run into the problem with a long running PHP script executed via cron and wget. The script terminates after 15 minutes due to the default timeout in wget.
However I believe this method does in fact work if you set up the correct timeouts. The PHP script you run needs to be set to have an unlimited run time. Best to set this at the start of your long running script.
set_time_limit(0);
When using wget you also need to remove its timeout by passing -T0. e.g.
wget -q0 -T0 http://yourhost/job.php
Be very careful not to overload your server with long running scripts.
If using wget to call the long running cron job, consider some of it's defaults that aren't desirable in this situation. By default it'll --tries 20 times if no data is read within --read-timeout of 900 seconds. So at a maximum set tries to 1 to ensure the cron job is only called once, since you of course wouldn't want a long running script called multiple times while it's still running.
wget -t 1 https://url/
If on cPanel and you don't care for any output emails after each cron job run, also use the following flags -O and -q to hide output, and redirect errors with 2>&1.
wget -O /dev/null -q -t 1 https://url/ 2>&1

Categories