Foreground and background jobs – Linux

Foreground and background jobs – Linux

A tool that is for sure not used enough is fg / bg.

Example: Did you ever start editing a file and suddenly remembered that you needed to copy the values of the previous command? Well this take care of this problem and many others!

First of all, executing a command / job in background when in a shell is as simple as adding an “&” at the end of the command:

watch -d ls -l &

Another way to do this is by using the “bg” command, or by pressing “CTRL+Z”:

watch -d ls -l bg

In order to see all background running jobs use the “jobs” command:

jobs
[1]  + suspended (signal)  nano ca.crt

And to bring one of them back to foreground, or use “CTRL+Z”:

fg %1

Where “%1” is the number of the job. Using this syntax you can also kill jobs:

kill %1

This is a very simple feature which can make your live easier if you learn to use it.

Cheers,

Vincent


Comments are closed.