How to run code with no open page in PHP

Asked

Viewed 942 times

5

I have a PHP code that uses FSOCKOPEN to check the port of a server, and depending on the answer it logs in log... But for this the page has to always stay open, I would like to know if there is any way for me to leave this running automatically on the server without user interface...

Today for it to work I leave an HTML tag updating the page every 10 seconds... But as I said, to run the page always has to be open!

  • I think you have to use crontab, with that you schedule the times to run your script.

2 answers

3

If using Linux, can be using the crontab:

*/12 * * * *  /usr/bin/php -c /etc/php5/apache2/php.ini /var/www/html/seu_php.php

Where:

  • 12 = 60 seconds / 5 seconds (every 5 seconds)
  • /usr/bin/php = path to your PHP executable
  • /etc/php5/apache2/php.ini = path to your php.ini file (PHP configuration)
  • /var/www/html/seu_php.php = Full path of the PHP script you want to run

If using Windows, put in the task scheduler:

c:\WAMP\php\bin\php.exe -c c:\WAMP\php\php.ini c:\WAMP\www\seu_php.php

Where:

  • c: WAMP php bin php.exe = path of your PHP executable
  • c: WAMP php.ini = path to your php.ini file (PHP configuration)
  • c: WAMP www seu_php.php = Full path of the PHP script you want to run
  • So I’m using XAMP on Windows! : ( ?

  • Yes, I updated the answer.

  • I don’t know if that’s the case, but something erases the comment. But if I use a shared server and I don’t have access to these resources, would I have any other solution? I may need this in the future

  • @Murilo, with no open page, believes that using another server (with these resources) to make an HTTP request every 5 seconds. I don’t know any other way... but I don’t rule out the possibility that there might be a better solution.

  • Look, I could not do putting this code in the scheduler of Windows tasks, it even runs the window, but does not execute the code correctly, bypassing I am leaving an open window and minimized there in the canton and working normally... :(

  • You ran the command on MS-DOS (windows terminal), before putting it on the task scheduler, to test?

Show 1 more comment

1


Dear friends, I managed to solve the problem by following the step by step described in the following article: Source: Run PHP script in Windows task scheduler

  • Type in cmd taskschd.msc and click on basic task:

inserir a descrição da imagem aqui

  • Type and title and next:

inserir a descrição da imagem aqui

  • Select daily:

inserir a descrição da imagem aqui

  • Schedule for when you want to start and the number of break days (if want daily leave 1):

inserir a descrição da imagem aqui

  • Select Start Program:

inserir a descrição da imagem aqui

  • Add the php program and in the arguments add the script path php:

inserir a descrição da imagem aqui

  • Click Finish.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.