bashunixssh

Run ssh and immediately execute command


I'm trying to find UNIX or bash command to run a command after connecting to an ssh server. For example:

ssh name@ip "tmux list-sessions"

The above code works, it lists the sessions, but it then immediately disconnects. Putting it in the sshrc on the server side works, but I need to be able to type it in client side. I want to be able to run a command, it logs in, opens up the window, then runs the command I've set. Ive tried

[command] | ssh name@ip

ssh name@ip [command]

ssh name@ip "[command]"

ssh -t name@ip [command]

Solution

  • ssh destination -t 'command; bash -l'
    

    will execute the command and then start up a login shell when it completes. For example:

    ssh -t user@domain.example 'cd /some/path; bash -l'