Home > Software design >  bash code to run python file continuously
bash code to run python file continuously

Time:09-16

  1. The python file in question collects data from API and the server is down once in a while so it stops running. to solve this, I've written this batch file that would keep running the file even if it encounters an error, so in few minutes it would be running as usual.

    :1

    python3 datacollection.py

    goto 1

Now, how do I achieve the same with bash file?

Note: I want to run the python file on AWS EC2 Ubuntu.

CodePudding user response:

while sleep 1; do python3 datacollection.py; done

Don't forget the sleep, or you will be annoyed when it falls into a crash loop with python3 aborting immediately.

CodePudding user response:

Just to add to @Willian Pursell answer. You could run screen command on your AWS server, then run while sleep 1; do python3 datacollection.py; done in your newly created session. Finally detach to the session using Ctrl ad keys. With that, you will be able to exit your session. Exit the server, the command will keep running on the background. Cheers.

CodePudding user response:

Use some kind of process manager e.g pm2 for this. It will be more stable than that.

  • Related