systemd服务未实际运行排查:状态active但screen进程不可见
Let’s walk through fixing this frustrating scenario—where systemctl status says your service is active, but screen -ls can’t find the session, and your actual workload isn’t running. This is a common pain point when combining systemd with screen, so let’s break down the most likely issues and fixes.
1. First, Confirm the Basics
Before diving into complex fixes, rule out these simple oversights:
- You’re checking the wrong user: Systemd runs services under the user specified in the
.servicefile (default is root). If you runscreen -lsas your regular user, you won’t see sessions owned by root or another service user. Try:sudo screen -ls # Or if your service uses a dedicated user: su - your-service-user -c "screen -ls" - The screen session exited immediately: Even if systemd starts screen, if your script
path/to/scriptcrashes or exits right away, the screen session will close too.
2. Fix Your Systemd Service File
Most issues stem from incorrect service configuration. Here’s what to check:
Common Mistake 1: Wrong Screen Startup Command
Screen needs to run in detached mode to persist after systemd starts it. If you’re using screen -S session-name /path/to/script, systemd will see the parent screen process exit (once it attaches) and think the service is done. Instead, use the -dmS flags to force a detached session:
# Correct ExecStart line ExecStart=/usr/bin/screen -dmS your-session-name /path/to/script
Common Mistake 2: Incorrect Service Type
If you set Type=forking, systemd expects the process to fork and exit the parent. The screen -dmS command does this correctly, so Type=forking is the right choice here. Avoid Type=simple unless your script runs indefinitely without forking.
Example Working Service File
Here’s a robust template you can adapt:
[Unit] Description=My Screen-Managed Service After=network.target # Adjust based on your script's dependencies [Service] User=your-service-user # Use a non-root user when possible Group=your-service-group Type=forking ExecStart=/usr/bin/screen -dmS my-service-session /path/to/script ExecStop=/usr/bin/screen -S my-service-session -X quit # Cleanly stop the session Restart=on-failure # Auto-restart if the script crashes RestartSec=5 [Install] WantedBy=multi-user.target
3. Check for Script Errors
If the screen session is starting but immediately exiting, your script is likely failing. To debug this:
- Add logging to the top of
path/to/scriptto capture output:# Add this at the start of your script exec > /var/log/my-service-script.log 2>&1 - Restart the service and check the log:
sudo systemctl restart your-service.service cat /var/log/my-service-script.log - Alternatively, view systemd’s detailed logs for the service:
journalctl -u your-service.service -f # -f follows live logs
4. Verify the Fix
After making changes:
- Reload systemd to pick up the service file edits:
sudo systemctl daemon-reload - Restart the service:
sudo systemctl restart your-service.service - Check the screen session:
sudo screen -ls # Or as the service user - Confirm the script is running by checking its process or logs.
内容的提问来源于stack exchange,提问作者bezzoon

