确定ruby脚本是否已在运行

时间:2021-07-11 07:41:27

Is there an easy way to tell if a ruby script is already running and then handle it appropriately? For example: I have a script called really_long_script.rb. I have it cronned to run every 5 minutes. When it runs, I want to see if the previous run is still running and then stop the execution of the second script. Any ideas?

是否有一种简单的方法来判断ruby脚本是否已经在运行,然后对其进行适当处理?例如:我有一个名为really_long_script.rb的脚本。我每隔5分钟就跑一次。当它运行时,我想查看上一次运行是否仍在运行,然后停止执行第二个脚本。有任何想法吗?

4 个解决方案

#1


9  

The ps is a really poor way of doing that and probably open to race conditions.

ps是一种非常糟糕的方式,可能会对比赛条件开放。

The traditional Unix/Linux way would be to write the PID to a file (typically in /var/run) and check to see if that file exists on startup.

传统的Unix / Linux方式是将PID写入文件(通常在/ var / run中)并检查该文件是否在启动时存在。

e.g. the pidfile being located at /var/run/myscript.pid then you'd check to see if that exists before running the program. There are a few tricks to avoid race conditions involving using O_EXCL (exclusing locking) to open the file and symbolic links.

例如pidfile位于/var/run/myscript.pid,然后在运行程序之前检查是否存在。有一些技巧可以避免竞争条件,包括使用O_EXCL(排除锁定)来打开文件和符号链接。

However unlikely, you should try to code to avoid race conditions by using atomic operations on the filesystem.

不太可能,您应该尝试编码以通过在文件系统上使用原子操作来避免竞争条件。

To save re-inventing the wheel, you might want to look at http://rubyforge.org/projects/pidify/

为了节省重新发明*,您可能需要查看http://rubyforge.org/projects/pidify/

#2


1  

Highlander

Description

A gem that ensures only one instance of your main script is running. In short, there can be only one.

一个gem,它确保只运行一个主脚本实例。简而言之,只有一个。

Installation

gem install highlander

Synopsis

require 'highlander' # This should be the -first- thing in your code.
# Your code here

Meanwhile, back on the command line...

同时,回到命令行......

# First attempt, works. Assume it's running in the background.
ruby your_script.rb

# Second attempt while the first instance is still running, fails.
ruby your_script.rb # => RuntimeError

Notes

Simply requiring the highlander gem ensures that only one instance of that script cannot be started again. If you try to start it again it will raise a RuntimeError.

只需要highlander gem就可以确保只能再次启动该脚本的一个实例。如果您尝试再次启动它将引发RuntimeError。

#3


0  

In bash:

if ps aux | grep really_long_script.rb | grep -vq grep
then
    echo Script already running
else
    ruby really_long_script.rb
fi

#4


0  

You should probably also check that the process is actually running, so that if your script dies without cleaning itself up, it will run the next time rather than simply checking that /var/run/foo.pid exists and exiting.

您可能还应该检查进程是否实际正在运行,这样如果您的脚本在没有自行清理的情况下死亡,它将在下次运行,而不是简单地检查/var/run/foo.pid是否存在并退出。

#1


9  

The ps is a really poor way of doing that and probably open to race conditions.

ps是一种非常糟糕的方式,可能会对比赛条件开放。

The traditional Unix/Linux way would be to write the PID to a file (typically in /var/run) and check to see if that file exists on startup.

传统的Unix / Linux方式是将PID写入文件(通常在/ var / run中)并检查该文件是否在启动时存在。

e.g. the pidfile being located at /var/run/myscript.pid then you'd check to see if that exists before running the program. There are a few tricks to avoid race conditions involving using O_EXCL (exclusing locking) to open the file and symbolic links.

例如pidfile位于/var/run/myscript.pid,然后在运行程序之前检查是否存在。有一些技巧可以避免竞争条件,包括使用O_EXCL(排除锁定)来打开文件和符号链接。

However unlikely, you should try to code to avoid race conditions by using atomic operations on the filesystem.

不太可能,您应该尝试编码以通过在文件系统上使用原子操作来避免竞争条件。

To save re-inventing the wheel, you might want to look at http://rubyforge.org/projects/pidify/

为了节省重新发明*,您可能需要查看http://rubyforge.org/projects/pidify/

#2


1  

Highlander

Description

A gem that ensures only one instance of your main script is running. In short, there can be only one.

一个gem,它确保只运行一个主脚本实例。简而言之,只有一个。

Installation

gem install highlander

Synopsis

require 'highlander' # This should be the -first- thing in your code.
# Your code here

Meanwhile, back on the command line...

同时,回到命令行......

# First attempt, works. Assume it's running in the background.
ruby your_script.rb

# Second attempt while the first instance is still running, fails.
ruby your_script.rb # => RuntimeError

Notes

Simply requiring the highlander gem ensures that only one instance of that script cannot be started again. If you try to start it again it will raise a RuntimeError.

只需要highlander gem就可以确保只能再次启动该脚本的一个实例。如果您尝试再次启动它将引发RuntimeError。

#3


0  

In bash:

if ps aux | grep really_long_script.rb | grep -vq grep
then
    echo Script already running
else
    ruby really_long_script.rb
fi

#4


0  

You should probably also check that the process is actually running, so that if your script dies without cleaning itself up, it will run the next time rather than simply checking that /var/run/foo.pid exists and exiting.

您可能还应该检查进程是否实际正在运行,这样如果您的脚本在没有自行清理的情况下死亡,它将在下次运行,而不是简单地检查/var/run/foo.pid是否存在并退出。