Commit 8497cf04 authored by LANORE Vincent's avatar LANORE Vincent
Browse files

Added "try again" script

parent cd2dd003
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright or Copr. Centre National de la Recherche Scientifique (CNRS) (2018)
# Contributors:
# - Vincent Lanore <vincent.lanore@gmail.com>
# This software is a computer program whose purpose is to provide a set of scripts for pre and post processing of data for
# convergence detection programs.
# This software is governed by the CeCILL-C license under French law and abiding by the rules of distribution of free software.
# You can use, modify and/ or redistribute the software under the terms of the CeCILL-C license as circulated by CEA, CNRS and
# INRIA at the following URL "http://www.cecill.info".
# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license, users
# are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive
# licensors have only limited liability.
# In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or
# reproducing the software by the user in light of its specific status of free software, that may mean that it is complicated
# to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth
# computer knowledge. Users are therefore encouraged to load and test the software's suitability as regards their requirements
# in conditions enabling the security of their systems and/or data to be ensured and, more generally, to use and operate it in
# the same conditions as regards security.
# The fact that you are presently reading this means that you have had knowledge of the CeCILL-C license and that you accept
# its terms.
"""Run a command and re-run it if it did not stop after some time.
Usage:
try_again.py [-t <time>] <command>
Positional arguments:
command the command to run
Options:
-h, --help show this help message and exit
-t <time>, --timeout <time> time in seconds to wait for
before re-running [default: 10]"""
from diffsel_script_utils import *
#===================================================================================================
print(step("Parsing command line arguments"))
from docopt import docopt
args = docopt(__doc__)
command = args["<command>"]
MESSAGE("Command is " + param(command))
timeout = int(args["--timeout"])
MESSAGE("Timeout is " + param(timeout) + "s")
#===================================================================================================
print(step("Running command"))
import subprocess as sub
import threading
class RunCmd():
def __init__(self, cmd, timeout):
self.cmd = cmd
self.timeout = timeout
def run(self):
self.p = sub.Popen(self.cmd, shell=True)
self.p.wait()
def Run(self):
okay = False
while not okay:
thread = threading.Thread(target=self.run)
thread.start()
thread.join(self.timeout)
if thread.is_alive():
MESSAGE("Timeout. Killed thread. Trying again.")
self.p.kill()
thread.join()
else:
MESSAGE("Command terminated within timeout.")
okay = True
MESSAGE("Running command...")
RunCmd(command, 1).Run()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment