Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 602
John Dene Avatar asked Sep 10 '15 11:09

John Dene


People also ask

How do I run AWS process in background?

To do this, in your buildspec, use the nohup command to run a command as a task in the background, even if the build process exits the shell. Use the disown command to forcibly stop a running background task. Start a background process and do not wait for it to ever complete: nohup sleep 30 & disown $!

What are the 3 different methods that you connect to a EC2 instance?

Connect using a standalone SSH client. Connect using Session Manager. Connect using browser-based SSH connection.

What happens to EC2 instance when stopped?

When you stop an EC2 instance, the instance will be shutdown and the virtual machine that was provisioned for you will be permanently taken away and you will no longer be charged for instance usage.


1 Answers

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.

like image 168
Indradhanush Gupta Avatar answered Oct 03 '22 00:10

Indradhanush Gupta