确保脚本一次只运行一次。 [英] Making sure script only runs once instance at a time.

查看:113
本文介绍了确保脚本一次只运行一次。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个脚本,我想确保脚本是

在任何给定时间都不会运行多次。


什么是如果还有另一个版本,那么测试和退出的最佳方法是在这台机器上的某个地方运行这个脚本的



我想我要问的是如何处理系统进程的方式我可以在shell中使用
。我在linux上运行这个。

谢谢


-h


-

Hari Sekhon

I have written a script and I would like to ensure that the script is
never run more than once at any given time.

What is the best way of testing and exiting if there is another version
of this script running somewhere on this machine?

I guess what I''m asking is how to handle system processes the way I can
in shell. I am running this on linux.
Thanks

-h

--
Hari Sekhon

推荐答案

在消息< ma **************** **********************@python.o rg> ;, Hari

Sekhon写道:
In message <ma**************************************@python.o rg>, Hari
Sekhon wrote:

我写了一个脚本,我想确保脚本是

在任何给定时间都不会运行多次。
I have written a script and I would like to ensure that the script is
never run more than once at any given time.



< http://groups.google.co.nz/gr******************* ****************@lust.ihug.co .nz>

<http://groups.google.co.nz/gr***********************************@lust.ihug.co .nz>


Hari Sekhon写道:
Hari Sekhon wrote:

我写了一个脚本,我想确保脚本是

在任何给定时间都不会运行多次。

如果有另一个版本,测试和退出的最佳方法是什么?在这台机器上的某个地方运行这个脚本的



我猜我要问的是如何处理系统进程的方式我可以在shell中处理
。我在linux上运行它。
I have written a script and I would like to ensure that the script is
never run more than once at any given time.

What is the best way of testing and exiting if there is another version
of this script running somewhere on this machine?

I guess what I''m asking is how to handle system processes the way I can
in shell. I am running this on linux.



虽然建议的其他解决方案似乎更一般(我没有

读取代码,只有标题中的信息),以下内容可能足以满足您的需求,并可用作

脚本中的代码。它搜索/ proc / for另一个进程,这是一个由python解释为
的脚本,并且与你的脚本具有相同的名称。所以它可能会失败,如果有另外一个运行相同名称的python脚本,但是它不是与你的脚本相同的
。 OTOH这是一个非常简短的测试:


-----

#!/ bin / env python

import os.path

import linecache

pid = os.getpid()

script = linecache。 getline(os.path.join(''/ proc'',str(pid),''cmdline''),

1).split(''\''')[0 :2]

for filter中的pid(lambda x:x.isdigit()和x!= str(pid),

os.listdir(''/ proc') ')):

other = linecache.getline(os.path.join(''/ proc'',str(pid),''cmdline''),

1).split(''\''')[0:2]

如果脚本[0] ==其他[0]和os.path.basename(脚本[-1] )==

os.path.basename(其他[-1]):

加注已经在运行!

- ---


HTH

Stephan

Although the other solution suggested seems to be more general (I have not
read the code, only the info in the header), the following may be
sufficient for what you want, and can be used as code inside of your
script. It searches /proc/ for another process that is a script interpreted
by python and has the same name as your script. So it can fail, if there is
another python script running with the same name, but which is not
identical to your script. OTOH it''s a very short and simple test:

-----
#!/bin/env python

import os.path
import linecache

pid=os.getpid()
script=linecache.getline(os.path.join(''/proc'', str(pid), ''cmdline''),
1).split(''\0'')[0:2]
for pid in filter(lambda x: x.isdigit() and x != str(pid),
os.listdir(''/proc'')):
other=linecache.getline(os.path.join(''/proc'', str(pid), ''cmdline''),
1).split(''\0'')[0:2]
if script[0] == other[0] and os.path.basename(script[-1]) ==
os.path.basename(other[-1]):
raise "already running!"
-----

HTH
Stephan


一个非常残酷但简单的有效的方法是将bind()绑定到本地主机上的套接字

,例如(127.0.0.1,4711),listen()但绝不接受()。

尝试在同一端口上绑定()的任何其他进程都将失败..

当进程终止时,端口会自动释放,等待som

timedelay ..


但是假设你有一个执行环境,这是可以接受的
。系统管理员可能有异议; o)

A very brutal but simple and effective method is to bind() to a socket
on localhost eg (127.0.0.1, 4711), listen() but never accept().
Any other process trying to to bind() on the same port will fail..
When the process dies, the port is released automatically, pending som
timedelay..

But this assumes you have an execution environment where this is
acceptable. A sysadmin may have objections ;o)


这篇关于确保脚本一次只运行一次。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆