When running a python script on a remote cloud machine (e.g. AWS), you may wish to shut down the machine when the script finishes to limit costs. This is a shell script to implement auto-shutdown after your python job has finished. The functionality then becomes a bit like running a job on an HPC system, but without the queue.
This repo has a shell script run_job.sh which implements this workflow:
-
Define maximum time before shutdown
-
Run
script.py -
If
script.pyfinishes, shutdown -
Otherwise, wait until the maximum shutdown time. Once that is reached, kill
script.pyand shut down the instance.
This script will only work on an Ubuntu-based system. You must run from a terminal with sudo access.
-
Create your python script, environment etc. as normal
-
Copy
run_job.shinto the root folder of your python project/repo on your remote machine -
Modify
run_job.shwith the path of your python script by setting thePYTHON_SCRIPTvariable at the top -
You may wish implement some code that will run if your job reaches the time limit (see graceful time limit shutdown below).
Once you have finished the setup instructions, run your script using run_job.sh. You can add the following command line flags:
| Flag | Argument | Default | Description |
|---|---|---|---|
--no-shutdown |
None | (Shutdown enabled) | Disables automatic system shutdown |
--timeout |
<duration> |
1h |
Sets maximum runtime before timeout |
--script |
<path> |
script.py |
Path to Python script to execute |
--job-name |
<name> |
my_python_job |
Custom name for the job and log file |
Examples:
Running normally:
./run_job.sh
Testing without shutdown:
./run_job.sh --no-shutdown
Setting time limit to 45min:
./run_job.sh --timeout 45m
Running a different python script with a time limit of 12 hours:
./run_job.sh --timeout 45m --script anotherscript.py --job-name another_job
This script has been designed for and tested on RONIN using ubuntu instances. It successfully shuts down the instance and the RONIN interface recognises they are shutdown once you click refresh.
If your python script reaches the time limit that you set, it will be killed. If this happens, in the demo script.py there is a function handle_sigterm(signum, frame)which will run before the machine shuts down, so you can put in here anything like saving data, debugging info etc. you would like to output before a time limit shutdown. To implement this in your code:
-
Add in the relevant imports
sysandsignal -
Copy the
handle_sigtermfunction fromscript.pyinto your project and add any code you should like to run in the event your script reaches the time limit -
Add this line to the start of your script where your main code is (e.g. in your
main()function):signal.signal(signal.SIGTERM, handle_sigterm)