nohup在后台常驻运行php脚本
To run a PHP script in the background using nohup
, you can follow these steps:
Navigate to the directory containing your PHP script: Open a terminal window and navigate to the directory where your PHP script is located.
Use the nohup
command: Execute the following command, replacing my_script.php
with the actual filename of your PHP script:
nohup php my_script.php &
Explanation of the command:
nohup
: Stands for "no hangup," which means the process will continue to run even if you log out of the terminal session.php
: Invokes the PHP interpreter to execute the script.my_script.php
: The filename of your PHP script.&
: Runs the command in the background and returns you to the prompt.Additional options:
>
. For example, to redirect the output to a file named output.log
, use:
nohup php my_script.php > output.log &
2>&1
. For example, to redirect error output to a file named error.log
, use:
nohup php my_script.php 2>&1 > output.log &
-C
:
nohup php -C /path/to/working/directory my_script.php &
Monitoring the background process:
ps
command to list running processes. Look for the php
process associated with your script.
ps -ef | grep my_script.php
top
command to view detailed information about running processes, including CPU and memory usage. Find the php
process associated with your script and check its resource usage.Troubleshooting:
Remember that running scripts in the background using nohup
can detach them from your terminal session, so you'll need to use other methods to monitor and manage them.