- 1 year ago
PHP on Windows, how to run a script in the background?
Comment
1 / 1
To run a PHP script in the background on a Windows machine, you can use the Windows Task Scheduler or create a Windows Service. Here are two approaches you can consider:
Windows Task Scheduler:
- Open the Task Scheduler by searching for "Task Scheduler" in the Start menu.
- Click on "Create Basic Task" or "Create Task" on the right side panel.
- Follow the wizard to set up the task. Specify the script path, set the trigger (e.g., daily, on startup), and choose "Start a program" as the action.
- In the "Program/script" field, provide the path to the PHP executable (e.g.,
C:\php\php.exe
). - In the "Add arguments" field, include the path to your PHP script (e.g.,
C:\path\to\script.php
). - In the "Start in" field, specify the directory where your PHP script resides.
- Complete the wizard, and the task will be scheduled to run in the background as per your configuration.
Windows Service:
- Create a new Windows Service using a tool like NSSM (Non-Sucking Service Manager).
- Download NSSM from https://nssm.cc/ and extract it to a directory.
- Open a command prompt with administrative privileges and navigate to the NSSM directory.
- Execute the following command to create a service:
nssm install ServiceName "C:\php\php.exe" "C:\path\to\script.php"
Replace "ServiceName" with the desired name for your service. - A window will open for configuring the service. Set the startup directory and other options as needed.
- Click the "Install Service" button, and the service will be created.
- Start the service either by using the Services console (
services.msc
) or by executingnssm start ServiceName
in the command prompt.
With these approaches, you can run a PHP script in the background on a Windows machine using either the Task Scheduler or by creating a Windows Service. Choose the method that suits your requirements and environment.