Skip to content

How to Move Running Command to Background in Linux

On this page

In our previous tutorial, you have learned about how to start or run command in background. Now this tutorial will help you to how to move running command in background. This is helpful, if you have started a command on terminal and that command is taking to much time. Now you want to move that in background, so that you can continue with other tasks.

Move Running Command in Background

For example you are taking a backup of large number of files, You have started command on terminal, But after mid of this task you think that this is taking longer time and you have some other pending tasks like below:

root@tecadmin:~$ tar czf log-backup.tar.gz /var/log

Now press CTRL + Z to pause the current running command on terminal.

CTRL + Z

[1]+  Stopped          tar czf log-backup.tar.gz /var/log

Now type bg command on terminal, This will start last paused command in background by appending & in command.

root@tecadmin:~$ bg

[1]+ tar czf log-backup.tar.gz /var/log &

List Running Commands in Background

To list all jobs running in background use jobs command. It will show all running commands with their job id.

root@tecadmin:~$ jobs

[1]+  Running                 tar czf log-backup.tar.gz /var/log &

Move Background Commands to Foreground (Terminal)

Now, If you need to any background jobs to move to foreground. Use fg command with job id and this will move background command with that job id to terminal. Job id can be found using jobs command as showing in above example.

root@tecadmin:~$ fg 1

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

3 Comments

  1. Nice.

    I had a critical headlessVM running that died at the worst time. I spent ages getting it working, but in my investigation, I moved the VM process from being spin up default in the background to the foreground to get extra diag info. Now it’s working I can’t risk shutting it down to dig deeper for about a week until it’s not in use. If my putty window drops I’m might be back in the same situ. The above saved my bacon. Thanks.

Leave a Reply

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


Back to top