使用Ctrl+Shift+L锁屏后终端任务暂停,如何让任务后台持续运行?
Hey there! This isn’t a mistake on your end—this issue usually boils down to system settings or how your terminal session is tied to your graphical desktop. Let’s break down the most common reasons and how to get those compile tasks running nonstop in the background:
1. Check Desktop Environment Power Saving Settings
Most desktop environments (like GNOME, KDE, Xfce) include power-saving features that throttle or pause non-essential processes when the screen locks to conserve energy. Here’s how to tweak this:
- GNOME: Open Settings > Power. Disable any "Suspend when inactive" options, and switch the power profile from "Power Saver" to "Balanced" or "Performance".
- KDE: Go to System Settings > Power Management > Energy Saving. Turn off settings that pause applications or trigger system suspend when the screen is locked.
- Xfce: Navigate to Settings > Power Manager > System. Ensure "Suspend when inactive" is set to "Never".
2. Run Tasks in a Detached Terminal Session
If your terminal is linked directly to your graphical session, locking the screen might freeze that entire session. The solution here is to use a tool that keeps your tasks running independently:
- Using
screen:- Launch a new screen session with
screen - Start your compile command (e.g.,
make installornpm run build) - Detach from the session by pressing
Ctrl+Afollowed byD - Lock your screen—your task will keep chugging along. To reattach later, run
screen -r
- Launch a new screen session with
- Using
tmux(a more modern, feature-rich alternative):- Start a tmux session with
tmux - Run your task
- Detach with
Ctrl+BthenD - Reconnect later with
tmux attach
- Start a tmux session with
3. Disable Terminal Inactivity Pausing
Some terminal emulators have a setting that pauses processes when the window isn’t focused or the screen is locked. Check your terminal’s preferences:
- Open your terminal’s settings (usually via the menu bar or right-click)
- Look for options like "Pause processes when terminal is inactive" or "Freeze when not focused"
- Uncheck this setting if it’s enabled
4. Adjust Systemd Login Session Behavior
If you’re using systemd, your login session might be marked as "inactive" when the screen locks, leading to process throttling. Here’s how to fix it:
- Check if your session goes inactive on lock with:
If it returnsloginctl show-session $XDG_SESSION_ID -p ActiveActive=nowhen locked, proceed to the next step. - Edit the systemd logind config file with
sudo nano /etc/systemd/logind.conf - Find the lines for
IdleActionandIdleActionSec, set them to:IdleAction=ignore IdleActionSec=0 - Save the file and restart the logind service with
sudo systemctl restart systemd-logind(note: this might log you out, so save your work first!)
内容的提问来源于stack exchange,提问作者gerdb

