本文介绍了我怎么知道我的python脚本是否正在运行?(使用Cygwin或Windows Shell)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 sudoserver.py 的python脚本,该脚本通过以下操作在 CygWin 外壳程序中启动:

I have a python script named sudoserver.py that I start in a CygWin shell by doing:

python sudoserver.py

我正计划创建一个shell脚本(我尚不知道是否要使用Windows shell脚本或CygWin脚本),该脚本需要知道此 sudoserver.py python脚本是否正在运行.但是,如果我在CygWin中这样做(在 sudoserver.py 运行时):

I am planning to create a shell script (I don't know yet if I will use Windows shell script or a CygWin script) that needs to know if this sudoserver.py python script is running.But if I do in CygWin (while sudoserver.py is running):

$ ps -e | grep "python" -i
    11020   10112   11020       7160  cons0   1000 00:09:53 /usr/bin/python2.7

以及在 Windows shell 中:

C:\>tasklist | find "python" /i
python2.7.exe                 4344 Console                    1    13.172 KB

所以看来我没有有关正在执行的 .py 文件的信息.我所知道的是python正在运行某物.
CygWin上'ps'的 -l (长)选项找不到我的 .py 文件.在 tasklist 上的/v (详细)开关也不是.
什么是合适的shell(Windows或CygWin shell足够;如果可能的话,两者都可以)以编程方式查找特定的python脚本现在是否正在执行?

So it seems I have no info about the .py file being executed. All I know is that python is running something.
The -l (long) option for 'ps' on CygWin does not find my .py file. Nor does it the /v (verbose) switch at tasklist.
What should be the appropriate shell (Windows or CygWin shell would enough; both if possible would be fine) way to programmatically find if an specific python script is executing right now?

注意:python进程可能由另一个用户启动.甚至来自未登录GUI Shell的用户,还有甚至更多的"SYSTEM" (特权)Windows用户.

NOTE: The python process could be started by another user. Even from a user not logged in a GUI shell, and, even more, the "SYSTEM" (privileged) Windows user.

推荐答案

由于 sudoserver.py 是您的脚本,因此您可以对其进行修改,以在启动时在可访问的位置创建文件并删除该文件.文件完成时.然后,您的Shell脚本可以检查该文件是否存在,以查找 sudoserver.py 是否正在运行.

Since sudoserver.py is your script, you could modify it to create a file in an accessible location when it starts and to delete the file when it finishes. Your shell script can then check for the existence of that file to find out if sudoserver.py is running.

(编辑)

感谢评论者建议,尽管文件的存在与否是不可靠的指示器,但文件的锁定状态不是.

Thanks to the commenters who suggested that while the presence or absence of the file is an unreliable indicator, a file's lock status is not.

我编写了以下Python脚本 testlock.py :

I wrote the following Python script testlock.py:

f = open ("lockfile.lck","w")
for i in range(10000000):
    print (i)
f.close()

...,然后在Windows PC上的Cygwin控制台窗口中运行它.同时,我在同一目录中打开了另一个Cygwin控制台窗口.

... and ran it in a Cygwin console window on my Windows PC. At the same time, I had another Cygwin console window open in the same directory.

首先,我启动 testlock.py :

Simon@Simon-PC ~/test/python
$ ls
lockfile.lck  testlock.py

Simon@Simon-PC ~/test/python
$ rm lockfile.lck
rm: cannot remove `lockfile.lck': Device or resource busy

...然后使用 Ctrl-C 关闭 testlock.py 后:

... then after I had shut down testlock.py by using Ctrl-C:

Simon@Simon-PC ~/test/python
$ rm lockfile.lck

Simon@Simon-PC ~/test/python
$ ls
testlock.py

Simon@Simon-PC ~/test/python
$

因此,看来Windows在 testlock.py 脚本运行时正在锁定文件,但是在通过 Ctrl-C 停止运行时,它将被解锁.可以使用以下脚本在Python中进行等效测试:

Thus, it appears that Windows is locking the file while the testlock.py script is running but it is unlocked when it is stopped with Ctrl-C. The equivalent test can be carried out in Python with the following script:

import os
try:
    os.remove ("lockfile.lck")
except:
    print ("lockfile.lck in use")

...正确报告:

$ python testaccess.py
lockfile.lck in use

...当 testlock.py 正在运行时,但是当 testlock.py Ctrl-C .

... when testlock.py is running but successfully removes the locked file when testlock.py has been stopped with a Ctrl-C.

请注意,根据 Python文档:

在 FileLock 的与平台无关的解决方案.https://stackoverflow.com/questions/489861/locking-a-file-in-python>在Python中锁定文件.

A platform-independent solution using an additional Python module FileLock is described in Locking a file in Python.

(进一步编辑)

看来,OP不一定需要Python解决方案.一种替代方法是在 bash 中执行此操作.这是 testlock.sh :

It appears that the OP didn't necessarily want a solution in Python. An alternative would be to do this in bash. Here is testlock.sh:

#!/bin/bash
flock lockfile.lck sequence.sh

脚本 sequence.sh 仅运行耗时的操作:

The script sequence.sh just runs a time-consuming operation:

#!/bin/bash
for i in `seq 1 1000000`;
do
    echo $i
done

现在,在运行 testlock.sh 时,我们可以使用 flock 上的另一个变体来测试锁定状态:

Now, while testlock.sh is running, we can test the lock status using another variant on flock:

$ flock -n lockfile.lck echo "Lock acquired" || echo "Could not acquire lock"
Could not acquire lock

$ flock -n lockfile.lck echo "Lock acquired" || echo "Could not acquire lock"
Could not acquire lock

$ flock -n lockfile.lck echo "Lock acquired" || echo "Could not acquire lock"
Lock acquired

$

前两次尝试锁定文件的尝试均失败,因为 testlock.sh 仍在运行,因此文件已被锁定.上一次尝试成功,因为 testlock.sh 已完成运行.

The first two attempts to lock the file failed because testlock.sh was still running and so the file was locked. The last attempt succeeded because testlock.sh had finished running.

这篇关于我怎么知道我的python脚本是否正在运行?(使用Cygwin或Windows Shell)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 11:54
查看更多