且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我怎么知道我的python脚本是否正在运行?(使用Cygwin或Windows Shell)

更新时间:2023-12-05 11:44:52

由于 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.

请注意,根据

在Windows上,尝试删除正在使用的文件会导致提出的例外情况;在Unix上,目录条目已删除但是分配给文件的存储空间直到原始文件已不再使用.

On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.

FileLock

的与平台无关的解决方案.https://***.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.