I am uploading files using a python script, non-interactively and usually in a screen session. For big uploads, it sometimes happens that the script stops because it is killed (by python or the OS, I'm not sure):

uploading file000100 ...
uploading file000101 ...

The error code coming from the script is 137 -- I'm not sure how to get more 'diagnostic' information.

To keep uploading, I put the script in a while loop:

python3 upload_files.py; \
while [[ $? == 137 ]]; do \
      python3 upload_files.py; \
      sleep 1; \

This trick does not work though, because the while loop also ends when the script is killed. Does that make sense?

  • May I ask why python for the uploads? What protocol to you use? Is e.g. rsync an option? It has a resume option. Or are "big uploads" rather many files instead of large files?
    – FelixJN
    Jun 10, 2021 at 10:17
  • Note that you don't need any of the \ you have there. This isn't causing you any problems, it's just unnecessary.
    – terdon
    Jun 10, 2021 at 10:40
  • @FelixJN I'm uploading to a site that has a python interface. I do have a file "succesful_uploads.txt" to check what still needs to be done. So a working while loop would fit the bill!
    – alle_meije
    Jun 10, 2021 at 12:32

1 Answer 1


If you want to always restart the same script until it succeeds, you could do the following:

while ((1)) ; do
   python3 script.py && break

I.e. only break the infinite loop upon successfully finishing python. Otherwise the infinite loop will take over for restarting the script.

  • Thanks, will try! So in your example killing the python script does not break the while loop -- I wonder why it does in mine?
    – alle_meije
    Jun 10, 2021 at 12:22
  • @alle_meije I strongly assume that the error code for killing is different from the one for problems during execution. You are checking for 137, but you might have to check for another one, too. Try executing python3 upload_files.py, abort via CTRL+C, then check echo $?. Is it really 137 in that case?
    – FelixJN
    Jun 10, 2021 at 19:44
  • 1
    Hi @FelixJN -- I'll mark your answer, because I found the problem thanks to your hints! In my while loop I first ran the python script -whcch returns an error code, quite consistently 137- and then sleep -which returns 0! Putting those the other way round works, as does your solution.
    – alle_meije
    Jun 14, 2021 at 7:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .