You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

systemd服务未实际运行排查:状态active但screen进程不可见

Troubleshooting: systemd Shows Active Service But Screen Session/Service Isn't Running

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 .service file (default is root). If you run screen -ls as 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/script crashes 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:

  1. Add logging to the top of path/to/script to capture output:
    # Add this at the start of your script
    exec > /var/log/my-service-script.log 2>&1
    
  2. Restart the service and check the log:
    sudo systemctl restart your-service.service
    cat /var/log/my-service-script.log
    
  3. 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:

  1. Reload systemd to pick up the service file edits:
    sudo systemctl daemon-reload
    
  2. Restart the service:
    sudo systemctl restart your-service.service
    
  3. Check the screen session:
    sudo screen -ls  # Or as the service user
    
  4. Confirm the script is running by checking its process or logs.

内容的提问来源于stack exchange,提问作者bezzoon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:14:09