How to specify run timing for bash scripts? - php

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?

Related

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

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

Perpetual cron job

I have developed a php script, that I want to run continuously.
For example, the script run.php executes a list of tasks, but I don't know how long it will take: it can take 30 sec, 1 min, 2 min, or more.
The problem is this script can't be executed simultaneous.
So I can't use the cron job because if I setup a cron job every minute, but the script is running during more than 1 minutes, I'll have bugs).
In fact, I want that this cron job to be Perpetual executed : everytime run.php is ended, the script run.php is reloaded, again and again...
I don't know how to resolve this problem.
Any help please ?
Thank you.
You've [at least] two options:
Under run.php code, determine if it is already running (by setting some external control system that is checked at the beginning of the script) and setup the crontab job as you described. [I recommend this approach].
Use something like (while :; do php run.php; done) & run it once, and put it inside the /etc/rc.local to make it run at every system start. I don't really like this approach, but it's a possible solution.

execute cron job every 2 hour with 30 min duration

I have searched SO, but can't seem to find any topic covering my little problem.
I'm quite new to cron jobs.
I have an IP based alarm. This alarm can control wireless power outlets, turning them on and off within the web based control panel. I can control the power outlets with a simple http command, making them turn on and off.
I have made a php script taking care of this. Right now they are 2 separate scripts, one for turning on and one for turning off. The script are only controlling one specific power outlet.
My problem is that i need a time based switching scheme. First I thought of making the php script sleep, but at the sleep time is 1 hour, it would not be my first choice.
So here we go.
Is it possible to set up cron job to:
1: run the on script for 1 sec, just to trigger the http command in the script.
2: wait 1 hour.
3: run the off script for 1 sec to trigger the http command.
4: wait 2 hours.
5: start all over again.
There are no problem with the alarm system, sending the OFF http command even if the power outlet is off, and vice versa.
You can combine all commands into single and run it every 3 hours.
0 */3 * * * /path/to/1st_script; sleep 3600; /path/to/2nd_script
This will run the 1st_script every 3 hours on 0-minute, then wait 1 hour, then run the 2nd_script.
Now I just have to figure out how to make the system accept my php script in cronjob.
I have read that I have to add /usr/bin/php to the cronjob command, but that does not exist on my RaspberryPi server (nginx with FPM/FastCGI)

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.

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