March 19, 2024
Linux Terminal Tutorial

Remote Execute Linux Commands And Scripts With Screen


This Small tutorial For Screen it might be a good idea to have your client setup to the server with a key, that way you don’t need to type your password each time. here’s a tutorial for Passwordless SSH.

Sending command is easy with SSH just add the command in brackets at the end of the ssh command.

Example:

ssh UserName@computerAddress "echo 'hello world!'"

Unfortunately as soon as the session closes the execute command stops. In other words when you return to your your local terminal prompt when ssh done running, any running program or scripts stops too. To keep the session from closing I use Screen on Linux to get round this issue. Simply install Screen on the server side with sudo apt install screen and then run the command like this example.

ssh -f UserName@ComputerAddress "screen -d -m -S YourSessionName SomeCommandYouWantToKeepRunning"

This command creates a session with Screen which is a multiplex session manager. Think of it as a multi-able terminal window program, that can switch between different terminals. Some insight on what the options do here, the first is -d this one detach the session from the terminal, that way when the termainal close’s the session running the command won’t stop. -m makes sure that a new session is always been made. -S is the session name, useful to set in-case you want to reattach to a session.

Screen can do lots more then this too! be sure to check out the manual to see what else it can do.

If you know other ways or better leave a commit!

colby

Computer guru with years working with technology. I find it fun to tinker with computer new and old, and make them do my work for me.

View all posts by colby →

Leave a Reply

Your email address will not be published. Required fields are marked *