I'm currently trying to ssh into a remote machine and run a script, then leave the node with the script running. Below is my script. However, when it runs, the script is successfully run on the machine but ssh session hangs. What's the problem?
ssh -x $username@$node 'rm -rf statuslist
mkdir statuslist
chmod u+x ~/monitor/concat.sh
chmod u+x ~/monitor/script.sh
nohup ./monitor/concat.sh &
exit;'
There are some situations when you want to execute/start some scripts on a remote machine/server (which will terminate automatically) and disconnect from the server.
eg: A script running on a box which when executed
I would use the following command:
@CKeven, you may put all those commands on one script, push it to the remote server and initiate it as follows:
Hope this works ;-)
Edit: You can also use ssh user@host 'screen -S SessionName -d -m "/path/to/executable"'
Which creates a detached screen session and runs target command within it
For me, only this worked:
This, of course, depends on screen, and lets you attach later, if you ever want stdin or stdout. Screen will terminate itself when my-script.sh ends.
It could be the standard input stream. Try
ssh -n ...
orssh -f ...
.Below is a much more common decision that required some efforts to find, and it really works for me:
It sends $theStuff list of commands to the tab No $theTabNumber of the screen session $theScreenSessionName preliminarily created at the 'server' on behalf of 'user'.
Please be aware of a trailing whitespace after -X stuff \" that is sent to overcome a 'stuff' option's glitch. The whitespace and $theStuff in the next line are appended by 'Enter' (^M) keystrokes. DON'T MISS 'EM!
The "this is a test" message is echoed in the initial terminal, and $theStuff commands are really executed inside the mentioned screen/tab.
What do you think about using
screen
for this? You could runscreen
via ssh to start the command (concat.sh
) and then you'd be able to return to the screen session if you wanted to monitor it (could be handy, depending on what concat does).To be more specific, try this:
You should find that the prompt returns immediately, and that
concat.sh
is running on the remote machine. I'll explain some of the options:ssh -t
makes a TTY. screen needs this.screen -dm
makes it start in "detached" mode. This is like "background" for your purposes.-S
testing gives your screen session a name. It is optional but recommended.Now, once you've done this, you can go to the remote machine and run this:
This will attach you to the screen session which contains your program. From there you can control it, kill it, see its output, and so on. Ctrl-A, then d will detach you from the screen session.
screen -ls
will list all running sessions.