When running programs from an terminal window in Ubuntu, the program process will run in the foreground, occupying the terminal session that started the program. Until the program ends or is closed, you can not start other applications as long as this process is running in the foreground. While you can open another terminal window or use the screen command, Bash shell provides an easier function known as job control that will allow multiple programs to run in the background and return control to the terminal window on Ubuntu.
To use job control and allow programs to return control back to the terminal window, commands entered should be followed with the ampersand character &
For example, you want to open Firefox. From the command line you would type:
firefox &
firefox &
Firefox will open in the background and the terminal window control will be returned allowing you to continue working in the terminal window. If you want to open another program or run a script, you can enter the command follow by &
For longer running programs started in the background, which do not need user input, job control commands can be executed that can show status, suspend, bring a program to the foreground, and return to background.
In the screen shot below, Firefox and Terminal server client applications were started in the background. Executing the jobs command displays all running jobs started in the background.
To bring the first program started in the background – Firefox, to the foreground and then suspend the process, execute
fg %1 followed by Ctrl+Z
To resume Firefox in the background, execute bg %1
Below are all options that can be used with job control:
jobs [-lnprs] [jobspec]
jobs -x command [arguments]
jobs -x command [arguments]
The first form lists the active jobs. The options have the following meanings:
-l
List process IDs in addition to the normal information.
List process IDs in addition to the normal information.
-n
Display information only about jobs that have changed status since the user was last notified of their status.
Display information only about jobs that have changed status since the user was last notified of their status.
-p
List only the process ID of the job's process group leader.
List only the process ID of the job's process group leader.
-r
Restrict output to running jobs.
Restrict output to running jobs.
-s
Restrict output to stopped jobs.
Restrict output to stopped jobs.
If jobspec is given, output is restricted to information about that job. If jobspec is not supplied, the status of all jobs is listed.
If the `-x' option is supplied, jobs replaces any jobspec found in command or arguments with the corresponding process group ID, and executes command, passing it arguments, returning its exit status.
bg [jobspec]
Resume the suspended job jobspec in the background, as if it had been started with `&'. If jobspec is not supplied, the current job is used. The return status is zero unless it is run when job control is not enabled, or, when run with job control enabled, if jobspec was not found or jobspec specifies a job that was started without job control.
fg [jobspec]
Resume the suspended job jobspec in the background, as if it had been started with `&'. If jobspec is not supplied, the current job is used. The return status is zero unless it is run when job control is not enabled, or, when run with job control enabled, if jobspec was not found or jobspec specifies a job that was started without job control.
fg [jobspec]
Resume the job jobspec in the foreground and make it the current job. If jobspec is not supplied, the current job is used. The return status is that of the command placed into the foreground, or non-zero if run when job control is disabled or, when run with job control enabled, jobspec does not specify a valid job or jobspec specifies a job that was started without job control.
Ctrl +Z Suspend (stop, but not quit) a process running in the foreground (suspend).
Ctrl +C Interrupt (terminate and quit) a process running in the foreground.
No comments:
Post a Comment