Task management with fg & bg


Task management by Paul Sutton

In Unix / Linux operating system,  you can run a program or task from the command line and in general this will simply run and return you to the command line once completed or if you stop the program running,  this is fine,  but what happens if you want to do something else at the same time.

This is where task management comes in.

On the Raspberry pi I have created a program that controls the pi liter led board.  This consists of a small board that plugs in to the GPIO port and has 8 LEDs that can be controlled with the python for example.

I can set the program going from the command line with

sudo python cylon.py

Which is fine the led s light up and go back and forth.   however i lose control of the command line.;

But I want to carry on and have the leds do their stuff at the same time

two options are available

one is to suspend the task,  background it

so while the program is running do


then type bg  This then back grounds the tasks,

to get the program back to the foreground type

fg which will then allow you to stop the process with crtl-c and do something else like run a different program

while the task is in the background you can type jobs to get a list of currently running jobs

Another way to send a program to the background is to insert an & when you run the task

sudo python cylon.py &

and when you execute it is then running in the background you can foreground the task as described above by using fg

Further reading

There is a lot more to all of this,   you can get more info from  man pages

Note however you can’t background interactive programs, so you can’t for example background a text editor as as soon as you start typing something it will come back to the foreground.

Hope this is helpful.

Creative Commons Licence
mint tutorials by Paul Sutton is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Leave a Reply

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