2

I have a python script which I have to run on AWS 24/7. How can I do that? Should I just run it as I'd do locally:

python3 main.py

Or should I be:

python3 main.py &

?

Is there any difference where I put it: in the home directory? Or in subdirectory? Does it matter at all?

5
  • Actually I'd have a different script that is run by cron every minute that checks to see if your script is not running or hung and kills/restarts it if it needs it before exiting.
    – Tyson
    Jan 31, 2016 at 16:50
  • how can the 2nd script check if the 1st is running? Jan 31, 2016 at 18:46
  • In the case of my script if the size of the output file is the same as it was 1 minute ago then somethings wrong, the output file doesn't really get gigantic but if it's the same as it was 60 seconds ago then somethings broke
    – Tyson
    Jan 31, 2016 at 18:58
  • @Tyson but in my case it doesn't. Feb 1, 2016 at 0:20
  • I don't have any idea what your script does do, or what version of 'ps' you have available. You can already resort to having the .py script write the time to a file and the bash/cron script can check for incrementing time. Here's some other ideas opensourcehacker.com/2012/11/26/…
    – Tyson
    Feb 1, 2016 at 0:38

3 Answers 3

4

What operating system are you running this on? You should strongly consider using the native init system. If you are lucky, you'll be using something with systemd, and then a service file is very simple to write.

[Unit]
Description=My awesome program

[Service]
ExecStart=/usr/local/bin/awesome.py

[Install]
WantedBy=multi-user.target
3

There are more options:

  • you can do as you suggested by using python3 main.py & and then closing the ssh pipe (using without & will make the script exit when you close the ssh connection)

  • you can set a cron job for the script

  • once I did a simple bash script that will listen for new information on a remote git server and run the updated script every time a new change was made

    • this could be a good idea if you're planning to update the script and make different deployment changes;
    • I wold first create a script that runs as a service at boot time, checking that everything is ok and up to date, then it will start my desired script periodically, checking for updates;
    • is not necessary to use git, another example might be using rsync;

EDIT:

Monitoring the script:

  • monitoring the resources that the vm uses from AWS;
  • sending an email with the output of the process currently running on the system or some kind of a log;
  • creating a mini web interface showing what services are running;
  • using a monitoring tool like Sensu, Nagios, Datadog, etc.
2
  • how can I monitor the script to ensure it's running? Feb 1, 2016 at 1:56
  • Install it as a service and start / monitor it with Monit : mmonit.com/monit
    – Tim
    Feb 19, 2016 at 2:04
2

You can use screen (man)

To start your program:

  • Log in with ssh,
  • Launch screen: $ screen
  • Start your program:$ python3 main.py
  • Detach from screen using key combination C-A(Ctrl+A) + D, (what's in your screen session will keep running)
  • Log out from ssh

To check again your program execution:

  • Log in with ssh (with the same user as before)
  • Re-attach to an existing screen session using $ screen -r
1
  • or tmux, for just the one program they are equally useful but tmux is better interactively. Feb 29, 2016 at 12:53

You must log in to answer this question.

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