如何在Linux的Git Hook中重启Lumen Web服务进程
killall php) Hey there! I get it—using killall php feels messy and risky (it could take down other PHP processes you don't want to touch). Let's walk through some cleaner, more reliable approaches to manage your Lumen service when you push code via Git hooks.
Option 1: Track the Lumen Process PID
The most straightforward upgrade is to save the Lumen process ID (PID) to a file when you start it, then use that PID to safely stop only your Lumen server during updates.
Step 1: Modify your startup command to save the PID
When launching Lumen, redirect output to null (to run it in the background) and write the PID to a dedicated file:
php -S localhost:8000 -t public > /dev/null 2>&1 & echo $! > /var/run/lumen.pid
Tip: Choose a path for the PID file that your Git user has write access to (e.g., inside your project directory or
/var/run/if permissions allow).
Step 2: Update your Git post-receive hook
In your central repo's hooks/post-receive script, add logic to read the PID, terminate the old process, then restart Lumen:
#!/bin/bash # Define paths (adjust these to match your setup) LUMEN_PROJECT_PATH="/path/to/your/lumen/project" PID_FILE="/var/run/lumen.pid" # Pull the latest code cd $LUMEN_PROJECT_PATH git pull origin main # Stop the existing Lumen process (if it's running) if [ -f $PID_FILE ]; then PID=$(cat $PID_FILE) # Try graceful shutdown first kill $PID 2>/dev/null # Wait a moment, then force kill if it's still running sleep 2 if ps -p $PID > /dev/null; then kill -9 $PID 2>/dev/null fi rm $PID_FILE fi # Restart Lumen and save the new PID cd $LUMEN_PROJECT_PATH php -S localhost:8000 -t public > /dev/null 2>&1 & echo $! > $PID_FILE
Don't forget to make the hook executable:
chmod +x .git/hooks/post-receive
Option 2: Use a Process Manager (For Production/Stable Environments)
If you're running this in a production-like setup, using a process manager like systemd or supervisord is way more robust. These tools handle auto-restarts if the process crashes, and make service management trivial.
Example with systemd
- Create a systemd service file at
/etc/systemd/system/lumen.service:
[Unit] Description=Lumen Web Service After=network.target [Service] User=www-data # Use the user your web service runs as Group=www-data WorkingDirectory=/path/to/your/lumen/project ExecStart=/usr/bin/php -S localhost:8000 -t public Restart=always # Auto-restart if the process dies [Install] WantedBy=multi-user.target
- Enable and start the service:
systemctl daemon-reload systemctl enable lumen.service systemctl start lumen.service
- Update your Git
post-receivehook to simply restart the service:
#!/bin/bash LUMEN_PROJECT_PATH="/path/to/your/lumen/project" cd $LUMEN_PROJECT_PATH git pull origin main # Restart the Lumen service via systemd systemctl restart lumen.service
Key Notes
- Permissions: Ensure the user running the Git hook has permission to modify the PID file or execute systemd commands (you might need to add them to the
sudoersfile for systemd actions). - Environment Variables: If your Lumen app relies on specific env vars, make sure they're available to the hook process (you can export them in the hook script if needed).
- PHP Built-in Server Limitation: Remember that PHP's built-in server is meant for development—for production, consider using Nginx + PHP-FPM instead, which is more stable and performant.
内容的提问来源于stack exchange,提问作者kaan_atakan

