Running a PHP Loop in Background: Using &, nohup, and Signal Handling
This guide shows how to run a simple PHP script that prints a line every second, demonstrates the effects of Ctrl+C (SIGINT) and session termination (SIGHUP), and explains how to use the shell background operator (&), nohup, and their combination to control output and process termination.
Test script
The example PHP script creates an infinite loop that prints Tinywan followed by an incrementing number and then sleeps for one second:
<?php
$a = 1;
while (true) {
echo "Tinywan " . $a++ . "
";
sleep(1);
}Running in the foreground
Executing php tinywan.php prints a line each second, e.g. Tinywan 1, Tinywan 2, … . Pressing Ctrl+C sends a SIGINT signal, which by default terminates the script.
Running with the background operator &
Starting the script with php tinywan.php & launches it as a background job. The shell immediately displays the job number and process ID (e.g., [1] 17153). The script continues to output to the terminal, and pressing Ctrl+C only stops the foreground shell, not the background job. The process can be verified with ps -axu | grep php and terminated later with kill -9 17153.
Running with nohup
Using nohup php tinywan.php detaches the process from the controlling terminal. The shell prints a message that input is ignored and output is appended to nohup.out. No process ID is shown on the terminal. The script’s output is written to nohup.out. The process can be listed with ps -aux | grep php. Sending Ctrl+C now terminates the script because nohup does not trap SIGINT. Closing the terminal sends a SIGHUP signal, but the script ignores it and continues running.
Combining nohup and &
Running nohup php tinywan.php & starts the script in the background while also detaching it from the terminal. The shell shows the job number and PID (e.g., [1] 17947) and prints the same nohup notice about nohup.out. The process persists after the terminal is closed; SIGHUP is ignored, and Ctrl+C has no effect. The process must be stopped explicitly with kill or killall.
Conclusion
Using & alone runs the script in the background, but the process still receives SIGINT and SIGHUP signals.
Using nohup alone detaches the process, redirects output to nohup.out, and makes it immune to SIGHUP while still terminating on SIGINT.
Combining nohup with & gives a truly daemon‑like behavior: the process runs in the background, ignores both SIGINT and SIGHUP, and should log to files rather than standard output.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
