[Linux] Who is the background job! ??

While studying Linux, I tried to log out ... Someone was running out of sight and I couldn't log out. I encountered a background job there, so I looked it up and summarized it. [Environment] Studying with Cygwin     Version 3.1.4-1

What is a background job in the first place?

Background = invisible place Job = processing

∴ Processing performed invisible to humans

Why need

Before that, the introduction of the jobs

The active jobs are displayed in a list with this magic command jobs.

Job's view


$ jobs    #List
$ jobs -l #Process ID [PID] is also displayed
$ jobs -p #Display only process ID [PID]
$ jobs -r #Running(running)Show process only
$ jobs -s #Stopping(stopped)Show process only

How to count jobs



$ sleep 120 #This is 1 job
$ sleep 10 & echo “I’m a job“ #This is also 1 job
$ echo “I’m a echo” && echo “me too“ #This is also 1 job

Front stage and back stage for the job

Front stage foreground job

If you run it normally, it will be run as a foreground job. In other words, the part of the job that can be seen in the front. I can see the job (job that I have to watch over) = I can't do other jobs

∴ No other job can be done because the shell is stopped while the foreground job is running.

Foreground job


$ sleep 120 #Watch for 120 seconds

Background job

If you run one job behind the scenes in the background, you can run another job. But since I'm behind the scenes, I don't receive any signals from the keyboard.

∴ Job and shell are running at the same time during background execution

Background job


$ sleep 120 & 
#&Run in the background with

& Is the miso.

Why is it necessary

Humans can only multitask, so ask your computer for multitasking. For example, you cannot read while washing the dishes. So you can read while you are being washed in the dishwasher. Like.

Actually try

5 jobs GO!


$ sleep 120
#ctrl+Job's cancellation with z
$ sleep 120 &
$ sleep 120 &
$ sleep 120 &
$ sleep 120 &

Job list


$ jobs #List display
[1]+Stop sleep 120
[2]Running sleep 120&
[3]Running sleep 120&
[4]Running sleep 120&
[5]-Running sleep 120&
# +Is the job currently running
# -Is the job that was executed immediately before

How to stop

It's okay to run it, but if you try to log out while it's running in the background, you'll get stuck. There are two ways to stop here

  1. Kill the process with the kill command
  2. Use the fg command to make a foreground job and then exit.

1. Kill the process with the kill command

kill command



$ jobs -l #-l Optional process ID[PID]display

#Confirm PID of the job you want to finish
[1]+  1663 Stopped                 sleep 120
[2]1674 running sleep 120&
[3]1675 running sleep 120&
[4]1676 Running sleep 120&
[5]1677 running sleep 120&
[6]1678 Running sleep 120&
[8]1680 running sleep 120&
[9]1681 running sleep 120&
[10]1682 running sleep 120&
[11]-1683 running sleep 120&

$ kill 1674

#Check if finished
$ jobs -l
[1]+  1663 Stopped                 sleep 120
[2]   1674 Terminated              sleep 120
[3]1675 running sleep 120&
[4]1676 Running sleep 120&
[5]1677 running sleep 120&
[6]1678 Running sleep 120&
[8]1680 running sleep 120&
[9]1681 running sleep 120&
[10]1682 running sleep 120&
[11]-1683 running sleep 120&

2. Exit with fg command

Pass the job number to the fg command and end the job with ctrl + c.

fg command


$ sleep 120 &
[2] 1686

$ jobs
[1]+Stop sleep 120
[2]-Running sleep 120&

#Pass the job number to the fg command
$ fg 2
sleep 120
#Here ctrl+End job in C

Jobs that are stopped ...

The job that stopped with ctrl + z remains forever. Processed by resurrection → end.

-Restart as a foreground job with the fg command and end it.

fg command


$ jobs
[1]+Stop sleep 120

$ fg 1 #Pass the job number to the fg command
sleep 120

$ sleep 120

[1]+Stop sleep 120

#ctrl+End with c

-Restart as a background job with the bg command and kill

bg command



$ bg 1 #Pass the job number to the bg command
[1]+ sleep 120 &

$ jobs -l #-l Optional PID confirmation
[1]+1687 running sleep 120&

$ kill 1687

$ jobs
[1]+  Terminated              sleep 120

The difference between a process and a job ...?

kill is the process ID and fg and bg are the job numbers. What is the difference between a process and a job?

[Linux] End of process or job, which is better?

Recommended Posts

[Linux] Who is the background job! ??
What is the Linux kernel?
The one who is not on DVD
What is Linux
There is Linux.
What is Linux
What is Linux
The origin of Manjaro Linux is "Mount Kilimanjaro"
It's a Mac. What is the Linux command Linux?
(Linux beginner) What is the magic word aux?
What is the difference between Unix and Linux?
Linux is something like that in the first place
What is Linux for?
Check if the LAN cable is disconnected on Linux
[Linux] End of process or job, which is better?
What is UNIT-V Linux?
What is Linux? [Command list]
[Linux] Update the package offline
Install the JDK on Linux
After all, who is Embedding?
Understand the Linux audit system Audit
[Linux] Directory under the root
What is the interface for ...
Paste the link on linux
What is the Callback function?
The image is a slug
On Linux, the time stamp of a file is a little past.
[Linux] When the screen is cut off when installing CentOS in VirtualBox
What to do if the inode is exhausted on EC2 Linux