• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Techgoeasy

Techgoeasy

Learn Oracle, PHP, HTML,CSS,Perl,UNIX shell scripts

  • Home
  • Oracle
    • Oracle database
    • Oracle Ebusiness Suite
    • Oracle weblogic
    • Oracle Performance Tuning
    • Oracle Hyperion
    • Oracle Cloud Tutorials
  • SQL
  • interview questions
  • Linux
  • PHP and HTML
  • Downloads
  • General
Home » Unix command and scripts » Bg, Fg, CtrlZ, Jobs Unix command : Unix Background Jobs

Bg, Fg, CtrlZ, Jobs Unix command : Unix Background Jobs

April 12, 2020 by techgoeasy Leave a Comment

Bg, Fg, CtrlZ  Unix command : Unix Background Jobs

There are several commands which allow you to manage the processes that belong to you. Off course You cannot do anything with processes belonging to other users unless you are root user. Here is the detail on Bg, Fg, CtrlZ  Unix command  to Manage Unix Background Jobs with Examples

Some definition

Table of Contents

  • What is foreground process
  • What is background process
  • How to cancel the foreground process that is currently running
  • To suspend a foreground process
  • To run a foreground process as a background job:
  • To run a process in the background as a job 
  • To run a process in the background as a job with Nohup
  • How to list  the background jobs running
  • To bring a specific background job into the foreground
  • How to kill background jobs and Kill them if required

What is foreground process

When you enter a command at the shell prompt, your shell forks a child process in which to execute the command. This process is called a foreground process.

What is background process

You can send the foreground process to the background process so that you can execute other command on the prompt.

How to cancel the foreground process that is currently running

To cancel a foreground process that is currently running enter : Ctrl-c

$ find /tmp -mtime -1 > /tmp/1.txt 
 $ [CTRL-C]
 [2]+ killed                 find /tmp -mtime -1 > /tmp/1.txt

To suspend a foreground process

Enter : Ctrl-z
If you are using the Bourne shell you will then have to kill the process. Other shells provide you with a facility to restart a process in the foreground or as a background job.

$ find /tmp -name “123 > /tmp/1.txt 
 $ [CTRL-Z]
 [2]+ stopped                 find /tmp -name “123 > /tmp/1.txt

To run a foreground process as a background job:


1. Suspend the foreground process.
2. Enter the bg (background) command to move the process into the background.

$find_old_logs.ksh 
 $ [CTRL-Z]
 [2]+ stopped                 find_old_logs.ksh
 $ bg

To run a process in the background as a job 

We need carry on working add an & (ampersand) at the end of the command line.

The shell forks a child process to run the command and displays the job number ([n]) and the PID (Process IDentifier) number. The shell prompt returns and you can enter further commands. Redirect the standard output for a command that is being run in the background to a file. This prevents the output from the command appearing on your screen and interrupting your current work

For example:

 find /tmp -name “*/log” -print > log.txt &  
 [1] 123 

To run a process in the background as a job with Nohup

Background jobs get killed if you logged out of your session. Suppose you want your command to continue even after log logged out, then we need to add nohup in front of the command

For example:
nohup find /tmp -name “*/log” -print > log.txt &   
[1] 123

How to list  the background jobs running

You can use the command jobs [-l] to list the background jobs.

it displays the process number.

 $ jobs  
 [111] + Running             x.sh > x.log
 [112] - Running             y.sh > y.log
 [113]   Running            z.sh > z.log
  

To bring a specific background job into the foreground

fg job_number
If there is only one job running in the background just enter the fg command.

 $ jobs  
 [1] + Running             x.sh > x.log
 [2] - Running             y.sh > y.log
 [3]   Running             z.sh > z.log
 $ fg 3
 $ z.sh > z.log 

This starts a process to print a file as a Unix background job. The user then checks the current active jobs and brings the print job – job number three (3) – into the foreground.

How to kill background jobs and Kill them if required

ps [-option]ps used without options this produces a list of all the processes owned by you and associated with your terminal.
kill [-signal] (PID)Use the ps command if you need to find the PID of a process. Always try to kill a process with a simple kill command.
kill (PID)This is the cleanest way to kill a process and has the same effect as canceling a process.
kill -1 (PID)This tells the process to hang-up just as though you were logging out. The system will attempt to kill any child processes.
kill -9 (PID)This will stop the process dead in its tracks but it may leave any child processes still running.

I hope you like the explanation for  Bg,Fg,CtrlZ  Unix command. Please do comments

Related articles

split command in Linux
Unix commands pdf
Functions in Shell Script
loop statement in Unix
https://ss64.com/bash/
how to tar a directory in Linux

Filed Under: Unix command and scripts Tagged With: &, Bg, Ctrl-Z Unix command to Manage Unix Background Jobs with Examples, Fg, Unix Background Jobs

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar



Subscribe to our mailing list

Enter your email address to subscribe to this blog and receive notifications of new posts by email

Recent Posts

  • How to list parameter set at session level in Oracle
  • How to generate tkprof in EBS in 19c
  • Oracle tkprof utility
  • What is Skew in Database?
  • how to find file based on size in linux

Copyright © 2023 : TechGoEasy

  • Hire me
  • Privacy Policy
  • Contact Us
  • New? Start Here
  • About Us