Search code examples
pythonamazon-web-servicesamazon-ec2cron

How to make a process run on AWS EC2 even after closing the local machine?


I have a script that will take around 10-15 hours to complete. I want to run it on EC2 instance and after say 10 hours stop the process forever.

Approaches--

CRONTAB -if I make a cronjob for the process . How to ensure it runs only once in lifetime and delete after 10 hour?


Solution

  • You need to open a screen session. You can do this by typing screen in the bash prompt. Once you have fired in your command, type CTRL+A followed by d. This will detach your screen session. However the script that you run will continue to run.

    Now, you can disconnect from your SSH session. And once you log back in, you can resume your session by typing in screen -r. This is assuming that you have only one detached session. In case you have more than one, you will have to type in the pid of the session you want to attach back to.

    To check the pid of the session, you can do:

    ps aux | grep screen
    

    This will list out the currently running screen processes. A sample output is:

    dhanush          1327   0.0  0.0  2443312     56 s000  S+    3Sep15   0:00.99 screen -r server -p server
    dhanush         79037   0.0  0.0  2432784    600 s008  S+    5:03PM   0:00.00 grep screen
    

    Here the pid for the relevant screen process is 1327. The second line belongs to the grep command that searched for the keyword screen itself.

    tmux is another alternative for the same.