Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  50] [ 0]  / answers: 1 / hits: 209136  / 2 Years ago, thu, june 23, 2022, 7:05:35

I want to execute a script, start.sh on a remote server which runs this:



nohup node server.js &


Naively, I call SSH like this:



ssh myserver <<EOF
./start.sh &
EOF


This starts the script, but leaves the session connected. I want to follow this step with other commands in a script, so that's no good.



How can I SSH to the remote machine, launch a nohup command into the background, then disconnect? I suppose I could put the SSH process itself into the background, but that doesn't seem right.


More From » ssh

 Answers
0

You have already found the right way, here document.




NOTE: you can put the ssh (client) into background by placing a & at the end, but you will not see the output. If you really want to do this, redirect the stdout/stderr to a file in case you need to check the response from the remote host.




Basically you can do it in either way:



Directly run the command{,s}



ssh user@host "nohup command1 > /dev/null 2>&1 &; nohup command2; command3"



OR



ssh user@host "$(nohup command1 > /dev/null 2>&1 &) && nohup command2 >> /path/to/log 2>&1 &"




NOTE: && requires the first command to return 0 before executing the second




Use Here document



ssh user@host << EOF
nohup command1 > /dev/null 2>&1 &
nohup command2 >> /path/to/command2.log 2>&1 &
......
EOF


The above 3 options should work for you.



In addition, take a look at the answer here: https://askubuntu.com/a/348921/70270


[#29305] Friday, June 24, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ousear

Total Points: 395
Total Questions: 114
Total Answers: 89

Location: Jordan
Member since Thu, Aug 5, 2021
3 Years ago
;