Scheduling PHP Scripts on Linux and Windows: Cron, Batch Files, and ignore_user_abort Loops
This article explains how to set up PHP scheduled tasks on Linux using crontab, on Windows using batch files, and how to implement custom loops with ignore_user_abort and sleep, while also covering non‑blocking requests with fsockopen and providing full code examples.
Linux Server: Using CronTab to Schedule PHP
On Linux, the command‑line tool crontab -e opens the crontab file in vi; press i to enter insert mode and add a new line for each scheduled task.
Example entry:
00 * * * * curl https://www.yourdomain.com/script.phpThe five fields represent minute, hour, day of month, month, and day of week respectively. For instance:
00 */2 * * * /usr/local/bin/php /home/www/script.phpThis runs /usr/local/bin/php /home/www/script.php every two hours at minute 00, executing the script directly on the server for higher efficiency.
Windows Server: Using a BAT File to Schedule PHP
Create a cron.bat file containing the command to run PHP:
D:\php\php.exe -q D:\website\test.phpSave the file, then open the Windows Task Scheduler (Start → Control Panel → Task Scheduler → Create Basic Task) and configure the trigger, credentials, and action to run cron.bat . The task will execute the PHP script at the scheduled times.
Using ignore_user_abort(true) and a Sleep Loop
Place the following code at the beginning of a PHP file to keep it running even if the client disconnects:
<?php ignore_user_abort(true); set_time_limit(0); date_default_timezone_set("PRC"); $run_time = strtotime("+1 day"); // first execution tomorrow $interval = 3600*12; // every 12 hours if (!file_exists(dirname(__FILE__)."/cron-run")) exit(); do { if (!file_exists(dirname(__FILE__)."/cron-switch")) break; // switch file to stop $gmt_time = microtime(true); $loop = isset($loop) && $loop ? $loop : $run_time - $gmt_time; $loop = $loop > 0 ? $loop : 0; if (!$loop) break; sleep($loop); // execute task here @unlink(dirname(__FILE__).'/cron-run'); $loop = $interval; } while (true);The loop runs until a cron-switch file is removed, allowing you to stop the task safely.
Non‑Blocking Requests with fsockopen
To trigger the scheduled PHP script without waiting for a response, use the following helper function:
// Remote request (no content retrieval) function _sock($url) { $host = parse_url($url, PHP_URL_HOST); $port = parse_url($url, PHP_URL_PORT) ?: 80; $scheme = parse_url($url, PHP_URL_SCHEME); $path = parse_url($url, PHP_URL_PATH); $query = parse_url($url, PHP_URL_QUERY); if ($query) $path .= "?".$query; if ($scheme == "https") $host = "ssl://".$host; $fp = fsockopen($host, $port, $error_code, $error_msg, 1); if (!$fp) return array("error_code"=>$error_code, "error_msg"=>$error_msg); else { stream_set_blocking($fp, true); stream_set_timeout($fp, 1); $header = "GET $path HTTP/1.1\r\n"; $header .= "Host: $host\r\n"; $header .= "Connection: close\r\n\r\n"; fwrite($fp, $header);
usleep(1000); // small delay for nginx compatibility
fclose($fp);
return array("error_code"=>0);
}
}
_sock("www.yourdomain.com/script.php");
Calling
_sock
sends a fire‑and‑forget request to the PHP script, activating the background task without blocking the user’s page.
For larger systems, task lists are often stored in a database (e.g., WordPress), but you can also manage them with simple file‑based approaches such as the open‑source
php‑cron
project on GitHub.Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
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.