top of page
Writer's picturel.cristian

Running a local bash function on a remote machine


Whenever you want to deal with cloud offerings (like #aws et. al.) you interact with the machines through an #ssh session. It is very likely that right after you connect to the machine you want some steps to be performed on the remote host.


For example, if you plan to switch the instance type like described in [[20220128192053]] Switch GPU to CPU using the `aws-cli`, after the machine is up you want to start #jupyer, #htop and some GPU profiling tools (`nvidia-smi` or `gpuwatch`).


This becomes an ideal task for #automation, but writing a setup script on the remote machine may not be in general the best idea because:

  • you may need to have the same functionality locally as well. This means copying the same function on the local machine. Keeping them in sync becomes a problem once your setup evolves (as it often does).

  • the remote machine can fail and it's possible you can lose that init script


So the best approach would be to keep the scrips locally (where you can back it up often) and consider the localhost as the single-source-of-truth for the automation. If you decide to walk on this path you will quickly bump on the question of how can you call a local script on a remote host.


In other words, how can you transfer the definition of the function that you have locally, into the remote session?


Based on this thread, you can use `typedef` (or `declare`) which lists the function definition, register it as a function on the remote host, then execute it

ssh user@host "$(typeset -f myfn); myfn"

You can also issue (it seems)


ssh user@host bash -s < typeset -f localfunction 

but this seems more convoluted and it is not clear if it actually works.

17 views0 comments

コメント


bottom of page