Back to blog

如何查看 Python 脚本是否在后台运行

Benjamin Carter

2025-08-31 16:00 · 5 min read

要查看 Python 脚本是否在后台运行,有几种常见的方法。根据你使用的操作系统(Linux、macOS 或 Windows),具体命令会有所不同。

1. Linux / macOS

在 Linux 和 macOS 上,你可以使用 psgrep 命令来检查进程。

  • ps 命令:列出当前正在运行的进程。
  • grep 命令:用于过滤 ps 命令的输出,只显示包含特定关键词(比如你的脚本名称)的行。

最常用的命令组合是:

Bash

ps aux | grep python
  • ps aux:这个命令会列出所有用户(a)、所有终端(x)和详细信息(u)的进程。
  • |:这是一个管道符,它将 ps aux 的输出作为 grep 命令的输入。
  • grep python:这个命令会从输入中查找所有包含“python”关键词的行。

如果你想更精确地查找你的脚本,可以加上脚本文件名:

Bash

ps aux | grep your_script_name.py

提示: 使用 grep -v grep 可以排除 grep 进程本身,让结果更干净。

Bash

ps aux | grep your_script_name.py | grep -v grep

如果命令返回了结果,就说明你的脚本正在运行。如果没有返回任何结果,则说明它没有运行。

Linux 下python进程后台运行、查看及关闭

查看进程

ps -ef |grep python

Linux下后台执行某个python脚本

nohup python -u  xxx.py > out.log 2>&1 &

查看脚本运行日志

tail -1000f out.log

关闭进程

kill -9 26879

2. Windows

在 Windows 系统上,你可以使用 tasklist 命令在命令提示符(CMD)或 PowerShell 中进行检查。

  • tasklist 命令:列出当前正在运行的所有任务和进程。

在 CMD 中:

DOS

tasklist | findstr python.exe
  • tasklist:列出所有进程。
  • |:管道符。
  • findstr python.exe:查找包含“python.exe”的行。

在 PowerShell 中,更推荐使用 Get-Process 或其别名 ps

PowerShell

Get-Process | Where-Object {$_.ProcessName -like "*python*"}

或者,如果你知道你的脚本是通过 Python 解释器运行的,也可以直接搜索解释器进程:

PowerShell

Get-Process python

如果命令返回了包含你的 Python 脚本的进程信息,那么它就在后台运行。

总结:

  • Linux/macOS: 使用 ps aux | grep [你的脚本或关键词]
  • Windows: 使用 tasklist | findstr python.exe 或 PowerShell 中的 Get-Process | Where-Object {$_.ProcessName -like "*python*"}

这些方法可以帮助你快速确认你的 Python 脚本是否正在后台执行。

如何修复 ERR_ADDRESS_UNREACHABLE 错误(10 种方法)

Benjamin Carter 2025-09-04 16:00 · 10 min read

ProxyEmpire vs PyProxy:该如何选择?有没有更优解?

Benjamin Carter 2025-08-28 16:00 · 10 min read

5大IP评分检测工具详解:助你识别风险IP,提升网络安全

Benjamin Carter 2025-07-09 08:01 · 8 min read