How to run script via SSH that doesn't end when I close connection?
You can run the command with the nohup command before it. You can also run it in 'screen', which will allow you reattach the terminal.
For example:
ssh mySever 'nohup bash myscript.sh'
Or just ssh into and run the nohup command. It should keep running even when you disconnect. This is because the nohup will intercept the SIGHUP singal (hangup).
Screen is a bit more involved, but for the 20 minutes it might take you to learn the basics, it is one of the most useful tools out there. Here is a tutorial.
I always just use nohup. If I care about the output, I redirect it to a log file, otherwise I send it to /dev/null.
Example:
nohup scp file.tar.gz root@someserver:/var/tmp > /dev/null 2>&1 &
That puts it in the background, with output going to /dev/null, and it is immune to the HUP signal if you log out. You can also "disown" the job with certain shells (like bash) so that it's not connected to your session.
disown %1