I have a long-running python script that I want to run on my AWS instance (with Ubuntu), and leave it running after I close the SSH connection so that I can come the next day and look at the results.
I usually do python long_running_script.py &
which sometimes works, but sometimes the process ends abruptly. The script writes to a log file and there are no errors reported in the log file, it just stops logging (I have also redirected stderr to the log file so any error should show up there, and it does if it stops due to a bug in the code or keyboard interrupt).
The script has multiprocessing code, but it only uses 4 out of the 16 cores available and ~40% memory, so I don't think it stops due to excessive resource usage. I use the in-built terminal of VSCode to execute the above-mentioned command and run my script.
The instance itself does not shut down and is always running. The EC2 management console monitoring does not show any crashes and the usage simply drops to 0. The AWS account is not my personal account and is provided by my university through cloudbank if that makes a difference.
I want to know what is the reason for this behavior, especially the fact that sometimes 3-4 day long scripts also finish successfully and sometimes the process terminates very soon after I close my SSH connection. Also, what is the recommended way of running long scripts like this on a remote server?