2

我的 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 起作用?

提前致谢!

4

1 回答 1

1

The first argument to os.execl is the path to the executable. The remaining arguments are passed to that executable as if their where typed on the command-line.

If you want "screenshotTaker" become the name of the process, that is "screenshots.py" responsibility to do so. Do you do something special in that sense in that script?

BTW, a more common approach is to keep track (in /var/run/ usually) of the PID of the running program. And kill it by PID. This could be done with Python (using os.kill) At system-level, some distribution have helpers for that exact purpose. For example, on Debian there is start-stop-daemon. Here is a excerpt of the 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.
于 2013-06-12T16:19:50.500 回答