Put php file in the background on Linux

2019-07-30 02:49发布

问题:

I have a PHP file that checks if a field in MySQL database is empty. If the field is empty, PHP is an update. I want to color this file in the background on Linux. If a user creates a record, the PHP file in the background will be updated. My PHP code:

<?php
$mysqli = mysqli_connect("localhost","name","pass","database");
$query = 'UPDATE  users SET  permissions =  "content" WHERE permissions = ""';
$n = $mysqli->query($query);
$query = "SELECT * from users";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC);
$subdominio = $row["name"];
$created_at = $row["created_at"];
?>

How can I do this? I can have problem with it file PHP in the background on Linux? Performance, slow down and etc?

Thanks very much!

回答1:

  1. Create PHP file and put your code there. (For example myfile.php)
  2. Install 'screen' :

Ubuntu & Debian: apt-get -y install screen

CentOS & Fedora: yum -y install screen

**** Follow steps 3 & 4 to execute myfile.php all the time. And follow steps 5 & 6 to execute myfile.php every 1 minute or every X time using crontab. ****

  1. Type ' screen ' .
  2. Type ' php myfile.php ' . And minimize/hide it by pressing Ctrl + A + D. And get back to it by typing ' screen -r ' .

  3. Type ' crontab -e ' .

  4. To execute every minute, you can type ' * * * * * php myfile.php ' ... If you don't want every minute, Google about crontab.


回答2:

You should be able to launch any program in the background by using the & symbol in Linux:

php myscript.php &

To see that it is running you use:

jobs

It will list a number for the job so you can call it back to the foreground to interact/kill it:

fg 1

F.Y.I If your script prints anything you will want to redirect output or it will go to the console