【问题标题】:Set a python process name设置一个python进程名
【发布时间】:2013-06-08 18:47:57
【问题描述】:

我的 python 脚本需要每小时杀死一次,并且在我需要重新启动它之后。我需要这样做,因为有时(我创建屏幕截图)浏览器窗口可能会因为用户登录弹出窗口或其他原因而挂起。无论如何。我创建了 2 个文件“reload.py”和“screenshot.py”。我通过 cronjob 运行 reload.py。

我认为这样的事情会起作用

    # kill process if still running
try :   
        os.system("killall -9 screenshotTaker");
except :
        print 'nothing to kill'

# reload or start process
os.execl("/path/to/script/screenshots.py", "screenshotTaker")

问题是,我读到的 execl 的第二个参数(给定的进程名称)不起作用?如何为其设置进程名称以使 kill 起作用?

提前致谢!

【问题讨论】:

标签: python process kill


【解决方案1】:

os.execl 的第一个参数是可执行文件的路径。其余参数被传递给该可执行文件,就好像它们在命令行中键入的位置一样。

如果你想让“screenshotTaker”成为进程的名字,那就是“screenshots.py”的职责。你在那个脚本中做了什么特别的事情吗?

顺便说一句,更常见的方法是跟踪(通常在 /var/run/ 中)正在运行的程序的 PID。并通过PID杀死它。这可以用 Python 来完成(使用os.kill)。例如,在 Debian 上有start-stop-daemon。这是man的摘录:

start-stop-daemon(8)            dpkg utilities            start-stop-daemon(8)

NAME
       start-stop-daemon - start and stop system daemon programs

SYNOPSIS
       start-stop-daemon [options] command

DESCRIPTION
       start-stop-daemon  is  used  to control the creation and termination of
       system-level  processes.   Using   one   of   the   matching   options,
       start-stop-daemon  can  be  configured  to find existing instances of a
       running process.

【讨论】:

  • 感谢您的解释!我有一个包含超过 100 万条网站名称记录的数据库。我需要创建这些网站的屏幕截图,这发生在“screenshots.py”中,从数据库中获取记录并拍摄。我将使用 os.kill,谢谢,但我应该在 screenshot.py 中设置一个进程名称?我该怎么做呢?我发现目前用python真的不可能?
  • 现在,我创建了一个小解决方法。我将 screenshots.py 中的 pid id 存储到 mysql 数据库中。 reload.py 从数据库中获取 pid id 并杀死/删除它。但是设置一个要杀死的进程名称会更干净
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多