I am new Linux/Unix shell user. How do I bring a background process to the foreground on Linux or Unix-like system? How do I run jobs in the foreground on Linux/Unix bash/ksh or POSIX based shell?
Job control is nothing but the ability to stop/suspend the execution of processes (command) and continue/resume their execution as per your requirements. This is done using your operating system and shell such as bash/ksh or POSIX shell.[donotprint]
|fg command details|
|Description||Bring a background process|
to the foreground
|Estimated completion time||10m|
Place job in the foreground, and make it the current job.
The basic syntax is as follows:
fg jobID1 jobID2 ... jobIDN
Understanding the job number (jobID)
There are a various ways to refer to a job in the shell. The character % introduces a job specification. The JobID can be a process ID (PID) number, or you can use one of the following symbol combinations:
- %Number : Use the job number such as %1 or %2.
- %String : Use the string whose name begins with suspended command such as %commandNameHere or %ping.
- %+ OR %% : Refers to the current job.
- %- : Refers to the previous job.
Before you start using fg command, you need to start couple of jobs on your system for demonstration purpose. Type the following commands to start jobs:
xeyes & gnome-calculator & gedit fetch-stock-prices.py &
Finally, run ping command in foreground:
To suspend ping command job hit the Ctrl-Z key sequence.
Type the following jobs command:
$ jobs -l
The output of the jobs -l command shows the following job running in the background:
+ 6138 Stopped ping cyberciti.biz
To bring a background or suspended process called ping to the foreground, enter:
$ fg %4
$ fg %ping
The screen displays ping command as follows:
From the bash(1) command man page or run help fg command to see all options:
fg: fg [job_spec] Move job to the foreground. Place the job identified by JOB_SPEC in the foreground, making it the current job. If JOB_SPEC is not present, the shell's notion of the current job is used. Exit Status: Status of command placed in foreground, or failure if an error occurs.
A note about /usr/bin/fg and shell builtin
Type the following type command to find out whether fg is part of shell, external command or both:
$ type -a fg
fg is a shell builtin fg is /usr/bin/fg
In almost all cases you need to use the fg command that is implemented as a BASH/KSH/POSIX shell built-in. The /usr/bin/fg command can not be used in the current shell session. The /usr/bin/bg command operates in a different environment and does not share the parent bash/ksh’s shells understanding of jobs.
This tutorial is also available in a quick video format:
- bash(1) Linux/Unix command man page
- ksh(1) Linux/Unix command man page
|Category||List of Unix and Linux commands|
|Firewall||Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04|
|Network Utilities||dig • host • ip • nmap|
|OpenVPN||CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04|
|Package Manager||apk • apt|
|Processes Management||bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time|
|Searching||grep • whereis • which|
|User Information||groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w|
|WireGuard VPN||Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04|