1

Context :

I have a web server (my hosting provider is OVH).

I have a large folder with many files in many sub-directories (100k+ files).

I need to move this directory from one place to anuther on my hosting.

Following things I've read on internet and StackOverflow, I think the best way is to use the mv command.

Indeed, I just need to move the entire folder and all its content (kind of "renaming" it), and I believe that my destination path is on the same filesystem (but I can't ensure this).

So I plan to do this :

mv /home/www/folder1/myfolder /home/www/folder2/myfolder &

(I will use the "&" to execute the command in background without blocking)

To execute this command, the only way I have is to connect to my web hosting throught SFTP using the ssh command :

ssh username@address

Problems :

When I do that (I'm from Windows 10), I end with an error after a few minutes (I guess it is because of my inactivity) :

Connection reset by <IP_ADDRESS> port 22

My fear is that after I launch my mv command, as it should take a while, my ssh connection will be closed with this error...

To avoid the connection to be closed, I tried to create a ~/.ssh/config file on my client with the following content, as explained here :

Host *
ServerAliveInterval 240

I also did a chmod

chmod 600 ~/.ssh/config

Then, when I try to launch my ssh connection from Windows PowerShell, I have an error

Bad owner or permissions on C:\\Users\\antoi/.ssh/config

Then I tried from git bash, and I don't have this error, the ssh connection opens without errors.

BUT then I have another error after a few minutes and my connection is also closed :

attente de données expirée : déconnexion automatique

I don't have the message in english, but it should be something like that

expired data wait: automatic logout

Questions :

Can you tell me if my mv command will continue to execute on the server even if my SSH connection has been lost?

If the answer is NO, do you know how can I ensure to keep my ssh alive during the move operation ?

thank you in advance, I can't achieve my migration without these answers as it would be too dangerous if my data are corrupted or in an unknown state!

4 Answers4

2

Can you tell me if my mv command will continue to execute on the server even if my SSH connection has been lost?

It depends on the Linux distribution, but most likely - no. As you run the process in the background, it gets killed once the shell is terminated.

If the answer is NO, do you know how can I ensure to keep my ssh alive during the move operation ?

The best way in my opinion is to not try to guess how long it will be running and just run the command in foreground (remove &):

mv /home/www/folder1/myfolder /home/www/folder2/myfolder

Why are you running the command in background if you won't be doing anything else and you'll leave your shell inactive?


Another solution to the second question is using keep-alive as you've done in your config. However, the error you get for bad owner or permissions is most likely because the owner of the file is wrong, so just try doing:

chown antoi:antoi ~/.ssh/config

(where antoi is your username)

Fanatique
  • 5,153
1

If your goal is to "move the files", not to "keep the connection alive", the easy way is to use nohup, ie. change your command to

nohup mv /home/www/folder1/myfolder /home/www/folder2/myfolder &

That way you can disconnect the SSH session and the command will keep running on the server, albeit you may need to use ps to check whether the move operation has completed later.

If you want to keep the connection alive automatically, you should check the relevant settings of the ssh client you are using, some have the ability to periodically send pinging requests to keep the connection alive.

0

If I need to do an operation which takes some time over ssh I usually use screen and launch a command from within it. Other similar tool is tmux.

Both should survive a disconnection and after reestablishing a connection you should still be able to reconnect to a session (for screen -r option will do that).

Tomek
  • 1,288
-1

If you're going through SSH, you should look into scp. This will not remove the existing files, but it absolutely will copy the files and maintain the ssh connection:

scp -r /local/directory/ username@to_host:/remote/directory/

If you're running the copy from a windows box, I'd recommend using something like WinSCP. It's GUI driven, but it has a robust command line and you can script transfers with a few simple flags.

Satanicpuppy
  • 7,205