Keep script running

3

I wanted to know if there is a way to keep a php script running internally on the server even with the browser turned off. I do not mean scheduling tasks, but maintaining a process as soon as it gets off my system. I saw in a bitcoin mining company that after mining the account the mining process continued to work on my account, I wanted to do this however with my particular scripts.

    
asked by anonymous 11.12.2017 / 21:31

2 answers

3

Even if the launcher is a browser that requested your page, you still have to schedule, which can be done by functions such as:

  • exec
  • system
  • shell_exec

Because web PHP is requested via HTTP ⇢ processes the response ⇢ returns the response as download ⇢ script execution dies

In other words, the script would not run forever.

To use exec and the like you may need permissions to use CRONTAB, sometimes it is only allowed for ROOT, an example to schedule:

$resposta = shell_exec('25 15 * * * /var/www/cronjob/meuscriptcontinuo.php > /var/www/cronjob/meuscriptcontinuo.log');

var_dump($resposta);

And to check the ones that are already scheduled:

<?php
$resposta = shell_exec('crontab -l');
var_dump($resposta);
    
11.12.2017 / 21:44
3

If you have access to the SSH server, you can run the code below to leave the process in the background (even if you are logged out from the server).

nohup php -f /path/to/script.php &
    
12.12.2017 / 00:16