Moving a command or script that you're running on the command line to the background so that you can start another job and managing backgrounded processes requires only a handful of commands. When working on the Linux command line, you can start a task, move it to the background, and, when you’re ready to reverse the process, bring it back to the foreground. When you run a command or script in the foreground, it occupies your time on the command line – until it’s finished. When you need to do something else while still allowing that first task to complete, you can move it to the background where it will continue processing and spend your time working on something else. The easiest way to do this is by typing ^z (hold the Ctrl key and press “z”) after starting the process. This stops the process. Then type “bg” to move it to the background. The jobs command will show you that it is still running. $ mytask ^Z [2]+ Stopped mytask $ bg $ jobs [1]+ Running mytask & Now let’s look at an easy way to try this out. First, put a simple script like this together and make it ready to run. #!/bin/bash while true do date > x sleep 120 done That script will put the current date and time to a file called “x” every 2 minutes. Since it overwrites the file once it exists, it doesn’t use up additional disk space. Next, start the script. If the script is called “loop”, you would use a command like this: $ loop Next, use the ^z trick to stop the process after starting it. $ loop ^Z [1]+ Stopped loop Then use the bg command to move it to the background where it will continue running. $ bg [1]+ loop & This particular script will run forever or until you kill it, log out or reboot the system. You can terminate it by using the kill command followed by the background process id. $ kill %1 [1]+ Terminated loop $ jobs The jobs command with no output tells you that no more backgrounded processes are still running. Start a process in the background You can also put a process in the background when you first start it by following the command or script name with an & character. In the example below, I’m running a second looping script in the background and then using the jobs command to view all backgrounded processes. $ loop2 & [2] 4651 $ jobs [1]- Running loop & [2]+ Running loop2 & Notice the – and + signs in the output above output following the [1] and [2] process numbers. These will only appear for the two most recently backgounded tasks. These jobs can be referred to as %1, %2 or simply as – and + when you want to move them to the foreground with a command like fg +. Bringing backgrounded tasks back to the foreground If you have processes that are running in the background, you use the fg command to bring them back to the foreground. To move the loop task to the foreground, you could use either of the commands shown below. $ fg %1 $ fg – You can use the jobs command to show the backgrounded tasks. $ jobs [2]+ Running loop2 & Notice that only one of the two tasks remained in the background after the first was brought back to the foreground with the fg – command. Wrap-up Depending on what you’re working on, moving tasks to the background so that you can take control of the command line for more pressing tasks while allowing the backgrounded tasks to continue running can save time can be a smart move. Related content how-to Compressing files using the zip command on Linux The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file. By Sandra Henry-Stocker May 13, 2024 4 mins Linux opinion NSA, FBI warn of email spoofing threat Email spoofing is acknowledged by experts as a very credible threat. By Sandra Henry-Stocker May 13, 2024 3 mins Linux how-to The logic of && and || on Linux These AND and OR equivalents can be used in scripts to determine next actions. By Sandra Henry-Stocker May 02, 2024 4 mins Linux how-to Using the apropos command on Linux By Sandra Henry-Stocker Apr 24, 2024 3 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe