Running the mv command in the background

One of the most annoying things that can happen to you is to disconnect your laptop from the network, then realize that there was a file moving job that was running, the command is going to get disconnected before mv gets the chance to delete the source files, the new copy is a hassle, i personally use rsync to continue such a copy with the delete source files flag

But it does not have to be this way, you can move the mv command to the background, the steps are simple

First, you need to hit CTRL+z to suspend the job, once suspended, you should see a job number in the suspend acknowledgement

Now, the next step is to disown the job, because right now, if you close your terminal window, the job will still be terminated

disown -h %1 (replace one with your own job number which may be 1)

Now to get the job running again but in the background

bg 1

That is it, you can now close (logout) your terminal window

So the summery
disown -h %1
bg 1

Now mind you, the output to stdout will not display (In most cases), you will need to use process status ps x to see the process.

If you want to bring back the command into the foreground, all you need to do is execute the command jobs (to find the ID), then run the command fg 1 (if it was job number one), then to hide it again, you can ctrl+z then bg 1 again (No need to disown it this time)

Leave a Reply

Your email address will not be published. Required fields are marked *