Keep script running

Asked

Viewed 539 times

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’m not talking about scheduling tasks, I’m talking about keeping a process as soon as it’s out of my system. I saw in a bitcoin mining company that after losing the account the mining process continued to work on my account, I wanted to do this but with my scripts in particular.

2 answers

4


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

  • exec
  • system
  • shell_exec

Because PHP on web is requested via HTTP processes the answer returns the answer as download script execution dies

In other words, the script would not be able to run eternally.

To use exec and related you may need permissions to use CRONTAB, sometimes 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);

4

If you have access to the SSH server, you can run the code below to leave the process in the background (even by dropping from the server).

nohup php -f /path/to/script.php &

Browser other questions tagged

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