How to let the scripts run in the background [ssh]
For our final year research project in University we had to run CPU intensive simulations in High performance servers in the department. Some of the simulation scripts took around 1-2 days to converge into expected results. So, we wanted to let the scripts run in background even after the ssh session is terminated. After trying few things, it turned out that the ideal method is to use the “screen“ command.
Luckily for us, the screen tool was already installed in the system. But, in case you need to install that by your self use the following command in the terminal. (obviously in the remote computer)
sudo apt install screen
There are plenty of resources in the internet explaining all the screen commands. So, I will let you know the bare minimum commands to (1) create a new screen session, (2) detaching the current screen session leaving whatever your scripts run in the background and (3) resume the detached screen session later.
(1) Create a new screen session
You can just use screen command. But let us use that with an additional argument “screen_script” with -S flag. Here, “screen_script” is the session name we give.
screen -S sesson_script
Now, you can run whatever you want. In this session. In my case, I have let my CPU intensive scripts to run.
(2) To Detach the current screen session
Just press CTRL+a then d
Now, you can terminate the ssh session to the remote server safely.
(3) Resume the detached screen session later
After login to the remote server through ssh. Use the following command to reattach to our screen session.
screen -r session_script
In case you forget the screen session name. You can use following command to list all the available screen sessions.
screen -ls