<p>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?</p>

[{'answer_id': 38200, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 0, 'selected': False, 'text': '<p>In bash:</p>\n\n<pre><code>if ps aux | grep really_long_script.rb | grep -vq grep\nthen\n    echo Script already running\nelse\n    ruby really_long_script.rb\nfi\n</code></pre>\n'}, {'answer_id': 38209, 'author': 'Philip Reynolds', 'author_id': 1087, 'author_profile': 'https://Stackoverflow.com/users/1087', 'pm_score': 4, 'selected': True, 'text': '<p>The ps is a really poor way of doing that and probably open to race conditions.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>However unlikely, you should try to code to avoid race conditions by using atomic operations on the filesystem. </p>\n\n<p>To save re-inventing the wheel, you might want to look at <a href="http://rubyforge.org/projects/pidify/" rel="noreferrer">http://rubyforge.org/projects/pidify/</a></p>\n'}, {'answer_id': 38243, 'author': 'Aaron Maenpaa', 'author_id': 2603, 'author_profile': 'https://Stackoverflow.com/users/2603', 'pm_score': 1, 'selected': False, 'text': '<p>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 \n/var/run/foo.pid exists and exiting.</p>\n'}, {'answer_id': 31044129, 'author': 'John La Rooy', 'author_id': 174728, 'author_profile': 'https://Stackoverflow.com/users/174728', 'pm_score': 2, 'selected': False, 'text': '<h1><a href="http://www.rubydoc.info/gems/highlander/0.1.2" rel="nofollow">Highlander</a></h1>\n\n<h1>Description</h1>\n\n<p>A gem that ensures only one instance of your main script is running.\nIn short, there can be only one.</p>\n\n<h1>Installation</h1>\n\n<pre><code>gem install highlander\n</code></pre>\n\n<h1>Synopsis</h1>\n\n<pre><code>require \'highlander\' # This should be the -first- thing in your code.\n# Your code here\n</code></pre>\n\n<p>Meanwhile, back on the command line...</p>\n\n<pre><code># First attempt, works. Assume it\'s running in the background.\nruby your_script.rb\n\n# Second attempt while the first instance is still running, fails.\nruby your_script.rb # =&gt; RuntimeError\n</code></pre>\n\n<h1>Notes</h1>\n\n<p>Simply requiring the highlander gem ensures that only one instance\nof that script cannot be started again. If you try to start it again\nit will raise a RuntimeError.</p>\n'}]
['https://Stackoverflow.com/questions/38193', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4082/']