I have two databases. Below is the code that I am using to get information from the first database.
$myrow = mysql_query("SELECT SUM(uploaded) FROM peers",$db);
$sum = mysql_fetch_array($myrow);
$c = $sum[0] / 1000000;
$d = $c / 1000000;
$l = round($d,3);
echo "<p>UP: $l TB</p>";
$myrow1 = mysql_query("SELECT SUM(downloaded) FROM peers",$db);
$sum1 = mysql_fetch_array($myrow1);
$a = $sum1[0] / 1000000;
$b = $a / 1000000;
$k = round($b,3);
echo "<p>DW: $k TB</p>";
I need to add this information to my second database and update it every 10 min with new fresh information from first database. I am using phpmyadmin.
Your question is very generic so I will try to answer for all scenario
You shoud create a process that run every 10 minutes (cron if you use Linux, scheduled task if you use windows)
If you use Linux you can
If you really want to use PHP, create a PHP script and call it using php command line or (much worst) create a php page that do what you want and have CRON to call it every 10 mins using LYNX browser.
Create a program in c/python/etc. that connects to first DB, query info, and writes to second.
Create a bash script that use mysql commandline to connect to DBs and do the same. (This has the advantage of not having to program)
If you use Windows you can:
Create a scheduled task in C# or vb.net or similar
Create a scheduled task using powershell
Use cron jobs for updating your information in DB.
cron jobs or PHP scheduler
http://net.tutsplus.com/tutorials/other/scheduling-tasks-with-cron-jobs
Related
I'm programming my own "smart home" as a learning project.
My code is running fine. I'm looking for help to improve the efficiency and of the code and/or the setup of crontab + php code.
I'm monitoring the energy consumption of my washing machine with a WIFI energy meter. Target is to notify me once the washing machine is completed so I don't forget to clear it.
on my Pi I have a crontab like so:
*/20 7-22 * * * /usr/bin/php '/home/holger/html/plugs/washer.php'
which runs following php code (I simplified for better readability):
[...]/I call the function, of course, but this function does the main task
function loop($maschine, $watt_init, $trashhold){
$max = 75;//max loops to avoid endless runs
$i = 1;//start counter
$tackt = 3;//tact time to check energy consumption
//$trashhold = 4;//ab x Watt kein standby
if ($watt_init < 1 ) {//Machine is switched off if energy consumption < 1 Watt
die;//quit
}
elseif ($watt_init < 2 ) {//Machine is switched off or in standby if energy consumption < 1 Watt
die;//quit
}
else {//Any thing else: Machine is running
while ($i < $max) {//loop as long as max loops are not reached
$watt_current = json_combine(IPplug5);//getting current energy consumption from WIFI energy meter via JSON
sleep(60*$tackt);//sleep and continue every 60s x tact time
$i++;//increase counter +1
//compare actual consumption with defined trashhold
if ($watt_current[0] >= $trashhold) {//continue while energy consumption bigger then trashhold
continue;//repeat loop
}
elseif ($watt_current[0] < $trashhold) {//stop if energy consumption lower then trashhold
break;//stop loop
}
}
echo "Program done. please clear. Runtime: " . $i*$tackt. "Min."
//[...] message me to my telegram bot
}
}
The code is running fine and I'm getting the output I need.
My question is: Is there a better way to do that?
Currently I'm afraid to overload my Pi with too many open php sessions, therefore I'm starting the code only every 20min and also let the while loop sleep for 3 Min. But for improved accuracy I like to run the cronjob more often and also let the while loop sleep only for 30s.
My requirements are to stick to my PI and php code and not to use any available software like Home Assisant.io as it contradicts with my learning approach.
Any ideas or insights welcome.
Ideally it's a not the best approach to handling and measuring power consumption. It would be best if you created an API that accepts events like on/off or threshold hold limit extends from your IP devices. Further you can create logs and store them in databases.
Although, for your current problem here is one alternate solution.
Set your cron that runs every second.
function get_powerConsumption($machine, $watt_init, $threshold)
{
if ($watt_init < 2) {
exit();
}
$time = date("Y-m-d H:i");
$filename = $machine . '_power_consumption.log'; // expecting some machine identification name here. otherwise ignore prefix
$watt_current = json_combine(IPplug5);
if ($watt_current[0] >= $threshold) {
$data = array(
$time,
$watt_current[0]
);
file_put_contents($filename, json_encode($data) . "\n", FILE_APPEND);
} elseif ($watt_current[0] < $threshold) {
$data = array(
$time,
'stopped'
);
file_put_contents($filename, json_encode($data) . "\n", FILE_APPEND);
}
}
Create another cron to look up for stopped events logged in the file. if found process the calculation based on logged data like time and consumption. You can set this cron to run based on your need like every second or minute or after some interval.
Also, handle code to delete old logs, once stopped events found.
I usually update my site "by hand", entering in one page called "enterheretoupdate.php". This page refreshes every minute to do all the job I need, so while this page is "open", my site keeps refreshing every minute.
What does "enterheretoupdate.php" do? It makes things related to mysql: create tables, selects from tables, add rows to tables, etc. Apart from that, it also make calculations on php and updates .json files.
I would like to create a cron job, so that it is not necessary for me to visit "enterheretoupdate.php" on my computer for updating my site every minute.
I am quite new on this, but I have learned how to create a cron job (I use 1and1). The example cron job I have created, consisting on sending an email every minute, works fine.
But then, I tried to save "enterheretoupdate.php" as a cron job and it does not work. Is there a "limitation" on the things a cron job can do? How should I "translate" my php file to make it work as a cron job?
Any help is really welcome.
This is how my .php file looks like:
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
//Change 1 to reload, 0 to not to reload;
$reload=1;
$gamecode=7;
$cmp="EL";
$year=2017;
if ($reload==1) echo"<head><meta http-equiv='refresh'content=".$sec.";URL='".$page."?gamecode=".$gamecode."&cmp=".$cmp."&year=".$year."'></head>";
include("../newcon.php");
include("../formulas.php");
include_once("funLightCreateTables.php");
include_once("funLightFirstFive.php");
include_once("funLightChanges.php");
include_once("funLightLiveJsons.php");
if ($cmp=="EC") {$l="U";}
if ($cmp=="EL") {$l="E";}
//Check
$q="SELECT * FROM LightLiveSchedule WHERE year=".$year." and cmp=".$cmp." and gamecode=".$gamecode."";
$res=mysqli_query($link,$q);
while ($r=mysqli_fetch_assoc($res)){
$started=$r['started'];
}
if ($started==0){
LightCreateTables($cmp,$year,$gamecode);
$q="UPDATE LightLiveSchedule SET started=1 WHERE year=".$year." and cmp=".$cmp." and gamecode=".$gamecode."";
mysqli_query($link,$q);
}
//Read
$pbp=file_get_contents("http://thesite.com/data.json?gamecode=".$gamecode."&seasoncode=".$l.$year."");
$pbp = json_decode($pbp,true);
//Insert
mysqli_query($link,"Truncate P_Live_Temp_".$cmp."_".$year."_".$gamecode."");
$lres=0;
$vres=0;
$n=0;
for ($i=0;$i<=4;$i++){
$nplays[$i]=count($pbp[$qtitle[$i]]);
$ii=0;
for ($j=0;$j<=$nplays[$i];$j++){
//change results
if ($pbp[$qtitle[$i]][$ii]['PUNTOS_A']!=null) {
$lres=$pbp[$qtitle[$i]][$ii]['PUNTOS_A'];
}
if ($pbp[$qtitle[$i]][$ii]['PUNTOS_B']!=null) {
$vres=$pbp[$qtitle[$i]][$ii]['PUNTOS_B'];
}
//clean
if (strpos($pbp[$qtitle[$i]][$ii]['CSDESCWEB'],"(")==0) {$play=$pbp[$qtitle[$i]][$ii]['CSDESCWEB'];}
if (strpos($pbp[$qtitle[$i]][$ii]['CSDESCWEB'],"(")>0) {$play=substr($pbp[$qtitle[$i]][$ii]['CSDESCWEB'],0,strpos($pbp[$qtitle[$i]][$ii]['CSDESCWEB'],"(")-1);}
//count
$points=0;
if ($play=="Three Pointer") {$points=3;}
if ($play=="Two Pointer" or $play=="Lay Up" or $play=="Dunk") {$points=2;}
if ($play=="Free Throw In") {$points=1;}
//ntconsole=00:00 at End Game
if ($play=="End Game") {$pbp[$qtitle[$i]][$ii]['NTCONSOLA']="00:00";}
//insert
$q="INSERT INTO P_Live_temp_".$cmp."_".$year."_".$gamecode."
(orden,shteam,shloc,shvis,quarter,minute,ntconsole,pcode,play,locres,visres,points)
VALUES
(".$n.",'".$pbp[$qtitle[$i]][$ii]['NTEQUIPO']."','".$pbp['ca']."','".$pbp['cb']."',".($i+1).",
".$pbp[$qtitle[$i]][$ii]['MINUTO'].",'".$pbp[$qtitle[$i]][$ii]['NTCONSOLA']."',
'".str_replace(" ","",substr($pbp[$qtitle[$i]][$ii]['NTJUGD'],1,10))."','".$play."',".$lres.",".$vres.",".$points.")";
mysqli_query($link,$q);
$ii++;
$n++;
}
}
Do you think it is suitable for a cron job? How should I proceed? Thanks a lot!
I had similar issues but the following worked for me.
See the link to change default mysql permission
How to allow remote connection to mysql
Now change your db_server value in the sql connection file from
localhost to 127.0.0.1
In your case the you need to edit the file ../newcon.php it seems.
got this code:
<?php
function test ($url){
$starttime = microtime(true);
$valid = #fsockopen($url, 80, $errno, $errstr, 30);
$stoptime = microtime(true);
echo (round(($stoptime-$starttime)*1000)).' ms.';
if (!$valid) {
echo "Status - Failure";
} else {
echo "Status - Success";
}
}
test('google.com');
?>
I want to have an option to execute this function every 5mins / 1hour / 1 day etc.. I was suggested using cron, but i never heard of cron before and after doing some research i understood that its a sepperate file, that would exeute the function every x amout of time. What if i would have multiple users, for example userA would want to run the script every 5mins, and userB would want to run the script every hour. In this sittuation i would need to create multiple cron files for each user?
Edit:
I was thinking about doing something like this:
for ($i = 0; $i < 100; $i++) {
test('google.com');
sleep(10 * 60);
}
Only in the sleep line i would have a custom $n field that each user would define by themselves. My problem with this was - it returns results only when the full cycle has finished, i would want it to return result after every "round?" (idk what its called, but basically would give 1 value, then 10mins later 2nd value and so on) .
The only way I found is to use an ajax method which will be called from the user page within a setTimeout.
See : settimeout function
May be you need use cron (UNIX) or Windows Tasks (Microsoft) depending the OS on your server.
Cron
Windows Task
Greetings,
I have used a multi curl library for PHP that facilitates fetching multiple pages in parallel (basically an easy to use API).
My Scenario: Fetch user data from API , process it and store results. All those users whose data have to be fetched are place in queue. This whole fetching , processing & storing result will take almost 8 - 10 min. And its really costly if I process it synchronously. So I have used php curl library for multi-threading. Its works fine if I run it in browser but since its cron-job so I have to run same script using command line. When I do so ; it will not work. Can anybody help me? Thanks in advance.
Psuedo Code:
$query = " Fetch users based on certain criteria LIMIT 200" ;
$result = execute-query ;
$curl_handle = curl_multi_init();
$i = 0;
$curl = array();
while ($row = mysql_fetch_assoc($result)) {
$curl[$i] = add_handle($curl_handle, API_CALL);
}
exec_handle($curl_handle);
for ($j = 0; $j < count($curl); $j++)//remove the handles
curl_multi_remove_handle($curl_handle, $curl[$i]);
curl_multi_close($curl_handle);
// Reference url
http://codestips.com/php-multithreading-using-curl/
I am trying to run a script from within a script and have the below which does not seem to work. I am basically getting a load of data from a database, looping through each row and running a script that will generate XML based on that data.
set_time_limit(0);
//connect to database
$msSqlDB = new mySqlConnect('store');
$select = "SELECT * FROM FStores";
$run = mysql_query($select);
while($row = mysql_fetch_array($run)){
exec('/var/www/web/shop_xml/index.php?shopKeeper=$row[SKID]&shop=1');
}
Whats the best possible way to do this? Will the 2nd row wait until the 1st row is successfully executed or can multiple run at the same time? Many thanks for any advice with this.
You need to do something like this:
exec('php /var/www/web/shop_xml/index.php "'.escapeshellarg($row['SKID']).'" "1" > /dev/null 2>&1 &');
Then, in your index.php script:
<?php
$shopKeeper = $argv[1];
$shop = $argv[2];
// ... do stuff
What you have attempted to do is use a HTTP query string in a file system invoke, which will not work. You need to pass the data as command-line arguments, like you would in a terminal. Then, you can get the data from $argv.
You need to start the command with php otherwise the kernel will (most likely, unless you add a hashbang and set permissions) not know how to execute the script, or have permissions to do it.
If you add > /dev/null 2>&1 & the commands will be run asynchronously, i.e. you will not have to wait for the last command to finish before you can invoke another. Be carefull with this though, you could end up with many processes if your query returns many rows.
To avoid this, you could do somthing like:
<?php
// Number of records to process at a time
$perBatch = 5;
set_time_limit(0);
//connect to database
$msSqlDB = new mySqlConnect('Freewebstore');
// Get the number of records in the table
$query = "SELECT count(*) FROM FacebookStores";
$result = mysql_fetch_row(mysql_query($select));
$count = $result[0];
for ($i = 0; $i < $count; $i += $perBatch) {
// Get $perBatch records from the DB
$query = "SELECT * FROM FacebookStores LIMIT $i,$perBatch";
$result = mysql_query($select);
for ($j = 1; $row = mysql_fetch_array($result); $j++) {
// Base command
$command = 'php /var/www/web/shop_xml/index.php "'.escapeshellarg($row['SKID']).'" "1"';
// Run all except the last asynchronously
if ($j < $perBatch) {
$command .= ' > /dev/null 2>&1 &';
}
exec($command);
}
}
This would get $perBatch records from the DB at a time, and process all but the last asynchronously. This would result in them being processed roughly $perBatch records at a time, and help to avoid a large number of processes eating server resources.
As a side note, you seem to be using an odd mix of OO DB code and procedural DB code - you should stick to one or the other to avoid confusion.
The answer by George P answers your question literally; how to run the script as-is from the command line. Let me in turn offer some alternatives.
First of all, your shop_xml/index.php script could check to see if it is being called from command line and read arguments accordingly. Thus:
if( php_sapi_name() == 'cli' ) {
// you would, of course, want to escape these for malicious code!
$shopKeeper = $argv[1];
$shop = $argv[2];
}
else {
$shopKeeper = $_GET['shopKeeper'];
$shop = $_GET['shop'];
}
Then you would call your command as follows:
$arg = escapeshellarg($row['SKID']);
exec( "/var/www/web/shop_xml/index.php \"$arg\" 1" );
However, a far better solution would be to take the code in index.php which is being called from the CLI and move it to an include (like functions.php), placing it inside a function like so:
function processShopRow($showKeeper, $shop) {
// stuff that used to be in index.php goes here
}
Now, in both index.php and in the code run from the CLI, you will include and run the code as follows:
include('functions.php');
set_time_limit(0);
//connect to database
$msSqlDB = new mySqlConnect('Freewebstore');
$select = "SELECT * FROM FacebookStores";
$run = mysql_query($select);
while($row = mysql_fetch_array($run)){
processShopRow($row['SKID'], 1);
}
your script is not working becuase
exec('/var/www/web/shop_xml/index.php?shopKeeper=$row[SKID]&shop=1');
make it
exec("/var/www/web/shop_xml/index.php?shopKeeper=$row[SKID]&shop=1");
single quotes can't have variables inside them.
Have you tried using this:
exec("/var/www/web/shop_xml/index.php?shopKeeper=$row[SKID]&shop=1");
instead of your original? The single quotes ' don't allow you to evaluate embedded variables or php objects in it. You'll have to use the double quotes " --Could you try that out?
In your exec command you need to either run the script using lynx (command line browser) or the PHP CLI. You also should reference the domain, not the full path:
exec("php http://domain.com/index.php?shopKeeper=$row[SKID]&shop=1");
exec() attempts to invoke a program on your server's file system, exactly as if you'd typed in the exec() parameter at a shell prompt. Shells do not know what to do with a URL, so you'll get a "no matching files" error, as the ? in the url will be seen as a shell wildcard.
If you want to request a url, you'd have to at minimum do
$output = file_get_contents("/var/www/etc....");
and PHP will do a full HTTP request for you.