Have you ever found yourself in a situation where some code needs to be executed on a remote host? What you will usually do is just ssh to the machine, and then execute your script there.
The issue with this workflow is that, more often than not, you may want just to run the code and disconnect from the ssh session. Or simply generate an event that will trigger the execution of some script remotely (and you want to output logging information to a screen, for instance). Nevertheless, unless the code is run in the background, disconnecting from the host machine will also kill the process running your script (moreover, running in the background may suppress output to the screen).
In this post I will share a very simple way to run code on a remote machine, use an additional Terminal buffer to run and display your code’s output, and hopefully provide you with peace of mind :).
GNU Screen is an amazing tool. It effectively creates Terminal “screens” with their own buffer, and more importantly, detached from the default user. That is, even if you access the host machine via ssh, you can create a Screen and run your code, disconnect, and then reconnect to the host machine, enter into the same Screen and see how your execution is going.
Suppose you are working within a network. You have physical access to Computer A, but Server B is located in another building, but reachable through the network.
If Server B has GNU Screen installed, you can execute script.sh (stored at Server B) in what I will call as a “Screen background” by issuing the following command from Computer A:
:~$ ssh <userB@ServerB> "screen -S <session name> -X stuff './script.sh <arguments> ^M'"
Values between “<>” are input values that you should provide (obviously, without the “<>”). And also, notice that quotes and double-quotes are needed. To top it all, the “^M” represent an “enter” key stroke. All should be included in the command.
Let’s see how userB can execute “script.sh -h -b” at Server B with IP address “192.168.168.1” using Screen:
:~$ ssh userB@184.108.40.206 "screen -S test -X stuff './script.sh -h -b ^M'"
I highly recommend Linode’s post about Screen and several of its features. Go check it out.
Do you think this could be used to boost your prototyping/experiment? Let me know in the comments.