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
Background = invisible place Job = processing
∴ Processing performed invisible to humans
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
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
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.
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.
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
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
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&
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
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
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