Running a command in the background can be useful when the command will run for a long time and does not need supervision. It leaves the screen free so you can use it for other work. In addition, it doesn’t stop the next processes or commands in queue which doesn’t depend on previous command’s result.
Running a wget command in background
wget -bqc https://reeteshghimire.com.np
where:
- -b : Go to background immediately after startup. If no output file is specified via the -o, output is redirected to wget-log.
- -q : Turn off Wget’s output (saves some disk space)
- -c : Resume broken download i.e. continue getting a partially-downloaded file. This is useful when you want to finish up a download started by a previous instance of Wget, or by another program.
index.php
<?php function __callURL($url){ file_get_contents($url); } function __callURLinBackground($url){ exec("wget -bqc ".$url); echo 'Running in background'; } //call a URL normally __callURL('https://localhost/background/url.php'); //call a URL in background __callURLinBackground('http://localhost/background/url.php');
url.php
<?php sleep(10); $contents = file_get_contents('https://reeteshghimire.com.np/'); file_put_contents('html.html', $contents);
In the above example, we have used sleep() PHP function.
sleep(10) will make the PHP script sleep for 10 seconds. We used this in order to verify that when we call this PHP script in background, the execution doesn’t wait for these 10 seconds and everything will be executed in background however long it may take for the execution.
Alternative Method
We can use nohup like this :
nohup wget http://domain.com/dvd.iso & exit
Follow this video for complete guidance :