I have a python script that run for few hours to process each record. Also, at the same time, I want to run the same script but with other arguments for other purpose.    Sometime an exception is raised and cause the script stop. Sometime the OS got restart.   So I would like to have something at the OS level to check.  Such that if the script is stop, restart the script.   What is the best approach to solve this problem?
92759
I'm trying to ensure a script remains running on a development server. It collates stats and provides a web service so it's supposed to persist, yet a few times a day, it dies off for unknown reasons. When we notice we just launch it again, but it's a pain in the rear and some users don't have permission (or the knowhow) to launch it up.  The programmer in me wants to spend a few hours getting to the bottom of the problem but the busy person in me thinks there must be an easy way to detect if an app is not running, and launch it again.  I know I could cron-script ps through grep:  ps -A | grep appname   But again, that's another hour of my life wasted on doing something that must already exist... Is there not a pre-made app that I can pass an executable (optionally with arguments) and that will keep a process running indefinitely?  In case it makes any difference, it's Ubuntu.
22142
I am looking to write a script that takes user input, then makes system wide changes. I need this to be very generic, but simply put, at the top, I need it to check to see if it's being run 'As an Administrator'. If it's not, then I want to display a message to tell them that; if it is, I want it to continue on. Is there a constant way to verify this? I am not looking to start a new session as the Administrator, I just want to detect if it's currently run as admin
118805
I am running Debian 7 Wheezy and I need to start some screens on startup as soon as there is a fully functional internet connection. However, not, if the internet connection broke and was connected again. So only on the first functional internet connection after boot.  Could you please post a dummy script for this and tell me where to put it and make it be executed under the given conditions?  The script only needs to start the screen and then terminate but the screen should continue.    EDIT I have already heard of the /etc/network/if-up.d/ folder. But how can I make sure that the script is not executed again if the internet connection is lost and then re-established?
99535
I'm making an python script which create a lot of object and this could take a lot of time. My question is how to show to the user of my script, the "progression" of the script? For now I print an progress bar in the system console.  But you have to open the system console befor and (apparently) the system console only work with Windows.  Is there an other way to show the progression of my script with python like control the mouse percentage icon (the little icon that replace the mouse while baking) or another idea?
105328
I have a python script that'll be checking a queue and performing an action on each item:  # checkqueue.py while True:   check_queue()   do_something()   How do I write a bash script that will check if it's running, and if not, start it.  Roughly the following pseudo code (or maybe it should do something like ps | grep?):  # keepalivescript.sh if processidfile exists:   if processid is running:      exit, all ok  run checkqueue.py write processid to processidfile   I'll call that from a crontab:  # crontab */5 * * * * /path/to/keepalivescript.sh
116620
What is the most elegant way to solve this:   open a file for reading, but only if it is not already opened for writing open a file for writing, but only if it is not already opened for reading or writing   The built-in functions work like this  &gt;&gt;&gt; path = r"c:\scr.txt" &gt;&gt;&gt; file1 = open(path, "w") &gt;&gt;&gt; print file1 &lt;open file 'c:\scr.txt', mode 'w' at 0x019F88D8&gt; &gt;&gt;&gt; file2 = open(path, "w") &gt;&gt;&gt; print file2 &lt;open file 'c:\scr.txt', mode 'w' at 0x02332188&gt; &gt;&gt;&gt; file1.write("111") &gt;&gt;&gt; file2.write("222") &gt;&gt;&gt; file1.close()   scr.txt now contains '111'.  &gt;&gt;&gt; file2.close()   scr.txt was overwritten and now contains '222' (on Windows, Python 2.4).  The solution should work inside the same process (like in the example above) as well as when another process has opened the file. It is preferred, if a crashing program will not keep the lock open.
59298
Is there a way to check to see if a pid corresponds to a valid process? I'm getting a pid from a different source other than from os.getpid() and I need to check to see if a process with that pid doesn't exist on the machine.   I need it to be available in Unix and Windows. I'm also checking to see if the PID is NOT in use.
74382
i have:  for i in range(2,n):     if(something):        do something     else:        do something else        i = 2 **restart the loop   But that doesn't seem to work. Is there a way to restart that loop?   Thanks
94278
I have a simple Python script that I want to stop executing if a condition is met.  For example:  done = True if done:     # quit/stop/exit else:     # do other stuff   Essentially, I am looking for something that behaves equivalently to the 'return' keyword in the body of a function which allows the flow of the code to exit the function and not execute the remaining code.
65894
I got a message saying script xyz.py returned exit code 0. What does this mean?  What do the exit codes in Python mean? How many are there? Which ones are important?
70096
I wonder how can I write a catch'em all exception handler in the application level which will give the user the option to resume the application flow?
76879
I want to execute something in a linux shell under a few different conditions, and be able to output the execution time of each execution.  I know I could write a perl or python script that would do this, but is there a way I can do it in the shell? (which happens to be bash)
32836
I need to make sure that user can run only one instance of my program at a time. Which means, that I have to check programatically, whether the same program is already running, and quit in such case.  The first thing that came to my mind was to create a file somewhere, when the program starts. Then, each other instance of the program would check for this file and exit if it found it. The trouble is, that the program must always exit gracefully and be able to delete the file it created, for this to work. In case of, say, power outage, the lock file remains in place and the program can't be started again.  To solve this, I decided to store the first program's process ID into the lock file and when another instance starts, it checks if the PID from the file is attached to some running process. If the file doesn't exist, is empty, or the PID doesn't correspond to any existing process, the program continues to run and writes its own PID to the file.  This seems to work quite fine - even after an unexpected shutdown, the chance that the (now obsolete) process ID will be associated with some other program, seems to be quite low.  But it still doesn't feel right (there is a chance of getting locked by some unrelated process) and working with process IDs seems to go beyond the standard C++ and probably isn't very portable either.  So, is there another (more clean and secure) way of doing this? Ideally one that would work with the ISO 98 C++ standard and on Windows and *nix alike. If it cannot be done platform-independently, Linux/Unix is a priority for me.
118452
I have a long running BASH script that I am running under CYGWIN on Windows.  I would like to limit the script to run for 30 seconds, and automatically terminate if it exceeds this limit. Ideally, I'd like to be able to do this to any command.  For example:  sh-3.2$ limittime -t 30 'myscript.sh'   or   sh-3.2$ limittime -t 30 'grep func *.c'   Under cygwin the ulimit command doesn't seem to work.   I am open to any ideas.
50732
I want to use VBScript to catch errors and log them (ie on error "log something") then resume the next line of the script.  For example,   On Error Resume Next 'Do Step 1 'Do Step 2 'Do Step 3   When an error occurs on step 1, I want it to log that error (or perform other custom functions with it) then resume at step 2. Is this possible? and how can I implement it?  EDIT: Can I do something like this?   On Error Resume myErrCatch 'Do step 1 'Do step 2 'Do step 3  myErrCatch: 'log error Resume Next
36970
I have some applications that need to run on my laptop all the time.   I use a bash script to start these applications. In my script I have a loop that looks similar to this:    while true; do     xterm done   This runs an application (xterm in this case) and if the application crashes, the loop starts it again.    This has a disadvantage that there is no "clean" exit from this loop. So even if the intention of the user is to close xterm, the loop starts it again.    Is there a way to start an application from bash script, watch whether it is running, to re-run it if script crashed or do nothing if the user closed it properly ?
48113
Consider this snippet:  stop () {     echo "${1}" 1&gt;&amp;2     exit 1 }  func () {     if false; then         echo "foo"     else         stop "something went wrong"     fi }   Normally when func is called it will cause the script to terminate, which is the intended behaviour. However, if it's executed in a sub-shell, such as in  result=`func`   it will not exit the script. This means the calling code has to check the exit status of the function every time. Is there a way to avoid this? Is this what set -e is for?