The server periodically performs scheduled tasks and periodically accesses the page of windows and linux

  • 2020-05-24 06:33:10
  • OfStack

Through this site selected 1 kind and simple 1 sentence to solve the problem

explorer "https://www.ofstack.com?opt=make"

Save the above code as dositemap.bat. Then add to the scheduled task

1. windows plans tasks

1. Write an PHP program, named test.php, as follows:


<?  
  $fp = fopen("test.txt", "a+");  
  fwrite($fp, date("Y-m-d H:i:s") . "  Success! Success! \n");  
  fclose($fp);  
?> 

Program bold write, what include\require use, no problem

2. Create a new Bat file and name it test.bat, as shown below:

D:\php\php.exe -q D:\website\test.php

Open the bat file you wrote, and close the ie browser


explorer.exe open=https://www.ofstack.com
Ping -n 80 127.1>nul
Taskkill /f /im "iexplore.exe"

3. Set up WINDOWS planning tasks:
Start � > Control panel. > Mission planning questions: > Add task plan
Browse the folder and select the bat file above
Set time and password (log in WINDOWS)
Save it.
4, over! You can try "run" by right-clicking the schedule task

2. Script implementation of Linux

1. Use PHP in Crontab to execute scripts
Just as normal shell script 1 is called in Crontab (specific Crontab usage), the PHP program is used to call the PHP script.
myscript.php is executed every 1 hour as follows:


**# crontab -e** 
00 * * * * /usr/local/bin/php /home/john/myscript.php 

/ usr local/bin php as PHP application path.

2. Execute the script using URL in Crontab
If your PHP script can be triggered via URL, you can configure your Crontab using lynx or curl or wget.
The following example USES the Lynx text browser to access URL to execute PHP scripts per hour. The Lynx text browser opens URL in dialog mode by default. However, as shown below, we use the -dump option on the lynx command line to convert the output of URL to standard output.


00 * * * * lynx -dump http://www.centos.bz/myscript.php 

The following example USES CURL to access URL to execute the PHP script every 5 cents. Curl displays output on standard output by default. You can also dump the output of the script to a temporary file using the "curl-o" option.


*/5 * * * * /usr/bin/curl -o temp.txt http://www.centos.bz/myscript.php 

The following example USES WGET to access URL to execute PHP scripts every 10 cents. -q for quiet mode." -O temp.txt "indicates that the output will be sent to a temporary file.


*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.centos.bz/myscript.php 

3. PHP realizes scheduled execution of planned tasks
Using php to refresh the browser requires several issues to be resolved
1.PHP script execution time limit, default is 30m solution: set_time_limit(); Or modify PHP.ini to set max_execution_time time (not recommended)
2. If the client browser is closed, the program may be forced to terminate. Solution: ignore_user_abort will still execute normally even if the page is closed
3. If the direct execution of program 1 is likely to consume a large amount of resources, the solution USES sleep to use program sleep 1 will then be executed
PHP code to be executed regularly:


<?php  
  ignore_user_abort();// Close the browser, PHP The script can also continue to execute .  
  set_time_limit(3000);//  through set_time_limit(0) You can let the program run indefinitely   
   $interval=5;//  every 5s run   

   // methods 1-- Infinite loop   
   do{  
    echo ' test '.time().'<br/>';  
    sleep($interval);//  Waiting for the 5s    
  }while(true);  

  // methods 2---sleep  Regularly perform   
   require_once './curlClass.php';// The introduction of the file   

   $curl = new httpCurl();// instantiation   
   $stime = $curl->getmicrotime();  
  for($i=0;$i<=10;$i++){  

    echo ' test '.time().'<br/>';  
    sleep($interval);//  Waiting for the 5s  

  }  
  ob_flush();  
  flush();  
  $etime = $curl->getmicrotime();  
  echo '<hr>';  
  echo round(($etime-stime),4);// Program execution time 

When I tested it, I found that the efficiency was not very high
Conclusion:
It is suggested that the work of regularly executing tasks should be entrusted to shell, which is more the king's way.


Related articles: