Body
It is sometimes desireable to connect to a remote Linux computer to run a script. However, it is not always practical to remain connected to the remote Linux computer waiting for the script to complete.
This tutorial aims to describe how to use Secure Shell (ssh) to connect to a Linux computer, execute a script, and use the Linux screen utility to detach the running script. This allows you to logout of the remote computer without terminating the running script. You can then later use ssh to re-connect to the remote computer, re-attach the script session and access the output.
Prerequisites: You must have openssh server installed and running on the remote computer and you must have an account with permissions to connect to the remote computer via Secure Shell.
On your local computer, launch the Terminal.
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=7cbafb64-2dc5-4874-8d3f-35098cdbaf12.PNG)
Connect to the remote Linux computer.
If this is the first time you are connecting to the remote Linux computer, the Secure Shell client will show you the ECDSA fingerprint of the remote Linux computer.
Secure Shell does this so that you can be sure that you are connecting securely to the remote computer you want, and are not being subjected to spoofing or a man-in-the-middle interception.
Before connecting for the first time, you should sit down in front of the computer that you later intend to connect remotely and while logged on locally, you can execute the command
ssh-keygen -l -f /etc/ssh/ssh_host_ecdsa_key.pub
The output of the command is the fingerprint you want to match in the screen shot below when you later connect to the computer remotely. If the fingerprints match, then you are ensured that you are connecting to the computer you want, and not something else.
You ar eonly presented with this prompt the first time you connect to a new computer. When you select Yes, the fingerprint is saved. You'll only getr prompted if the fingerprint no longer matches at a later connection attempt.
For the purposes of this tutorial, I am sure the fingerprint is good, so I enter Yes. You must spell out the word.
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=c5963e95-2f6d-472a-91a1-520f912813ba.PNG)
Secure Shell informs you that it has added the remote computer to your list of known hosts.
Enter your password on the remote system.
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=44163960-c38f-4f27-81c6-54cea7f94ed4.PNG)
You are now successfully logged on to the remote Linux computer.
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=a6a00f6f-6ae2-4410-8919-2da57af60fc5.PNG)
Now you can navigate to the location of your script. In this tutorial I have a simple script located in the Desktop folder of the remote Linux computer.
I've created the script named RunMe on the remote computer and changed the file permissions to make it executable.
The script simply creates a text file on the desktop named OutputFile. The script sleeps for 10 seconds, then writes a line of text to OutputFile, then sleeps for another 10 seconds and repeats a total of 10 times. Therefore, it takes a bit more than 1 and a half minutes for the script to complete.
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=b4b1b559-8e18-4add-9f7d-2258ae92f58d.PNG)
Navigate to your Desktop folder on the remote Linux computer.
In our example, you can see the RunMe script listed.
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=e133486d-cb44-4b94-b239-315222fedda2.PNG)
Execute the screen command.
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=16422032-a3e3-40a8-a1ad-4ae02f247326.PNG)
The screen utility splash screen appears. Hit the <ENTER> key to dismiss it. There is no further feedback from screen as it operates in the background.
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=2c4b783e-0798-4b6f-9f53-a43ccd05cbd8.PNG)
You're now ready to execute your script and then logoff from the remote computer while the script continues to run.
The basic process is:
1. Launch screen
2. Launch your script
3. Press CTRL+A, then D.
4. Logoff the remote session.
5. At a later time, re-connect to the remote computer via ssh
6. Execute the command screen -r to re-attach your detached session.
7. Find the results of your script.
In our example, I am now launching the RunMe script from my Desktop folder.
Since the running script takes control of the session, the command prompt does not return.
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=7a7fc525-0d60-47ec-b158-f6d5a1da8c33.PNG)
I now press CTRL+A D, and screen reports the identification of the detached session and creates a new shell prompt.
Note the line: [detached from 13614.pts-0.Portal]
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=7e7ce32f-4766-4485-8112-7791d4189aed.PNG)
My script is now running and the script's session is detached and I am returned to the shell prompt.
Now I end the remote Secure Shell session by logging out.
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=665cdd0e-b45b-476c-8b92-5a48c46b3f54.PNG)
In our example, I expect my script to take less than two minutes to run. In a normal case, this process is suitable when scripts are expected to take hours or days to complete.
Reconnect to the remote Linux computer via a new Secure Shell session
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=5d2493e6-abab-4614-8a86-ea352225f5b4.PNG)
In the example below, I've reconnected to the remote Linux computer and checked my expected output folder for the results of my script.
The file named OutputFile is the expected output of my script.
![](https://helpcenter.mines.edu/TDPortal/Images/Viewer?fileName=fed29319-3a62-42a4-83c9-730979533d57.PNG)
You have now successfully used the Linux screen utility.
2020.06.01 dkearney