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.
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.
Secure Shell informs you that it has added the remote computer to your list of known hosts.
Enter your password on the remote system.
You are now successfully logged on to the remote Linux computer.
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.
Navigate to your Desktop folder on the remote Linux computer.
In our example, you can see the RunMe script listed.
Execute the screen command.
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.
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.
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]
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.
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
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.
You have now successfully used the Linux screen utility.
2020.06.01 dkearney