|
Home | Switchboard | Unix Administration | Red Hat | TCP/IP Networks | Neoliberalism | Toxic Managers |
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and bastardization of classic Unix |
The command jobs is one of the three commands that consititute simple batch scheduler in Unix. The other two are batch command and atq command . There is also script atrun which allow to specify the load threshold below which batch jobs are allowed to run.
The internal jobs command displays all processes in background or in a suspended state. Jobs are processes you have started in foreground, background, or have suspended. There are three states of jobs:
* foreground | A process whose I/O is attached to your terminal. If a process is executing in foreground, you have to wait until it completes to execute the next command. |
* background | A process detached from your terminal. The process is executing while you continue to work at your terminal. |
* suspended | A process that is not executing. It is waiting to be moved to foreground or background to continue execution. |
|
If you end a command line with an ampersand (&) the process is immediately placed in background for processing.
|
You will need to use the jobs command to list any processes you have suspended or placed in the background. When a background job completes, the shell notifies you between commands. If you try to log out while you have suspended jobs, the shell warns you and does not log you out.
If you start a command in foreground, you can suspend it by typing Ctrl-Z. You can place it in background with the bg command. The fg command allows you to reattach to the process in the foreground. A suspended job does not execute. It remains at the same instruction where you suspended it.
Following is the general format of the jobs command.
jobs [ -lnp ]
Options
The following options may be used to control how jobs functions.
-l | Lists the process IDs in addition to the normal output. |
-n | Displays jobs that have stopped or exited since last notified. |
-p | Displays only the process IDs. Not implemented on some systems. |
The output of the jobs command provides you with the information you need to control jobs. We'll use the following output to explain the information provided by jobs.
jobs -l +[4] 5931 Stopped vi myfile -[3] 5917 Running du -s / > /tmp/du.all [2] 5898 Done(0) ls -R | lp
The first column contains the job number enclosed in square brackets. The plus (+) signifies the current job. The current job is the last job placed in background or stopped. The minus (-) signifies the previous job.
The second column contains the process ID (PID). Some systems display this as a default; others require the -l option.
The third column contains the current state of the job. The possible states are:
|
|
State | Description |
---|---|
|
|
Done(n) | The job has completed with return code n. |
Running | The job is executing (processing) in background. |
Stopped | The job has been suspended from execution. |
Terminated | The job has been aborted via a signal. |
|
Column four contains the name of the command you entered.
Refer to the bg, fg, kill, stop and suspend commands described in modules 9, 51, 70, 125, and 129.
In this activity you use the jobs command to display all suspended and background jobs. Begin at the shell prompt.
cj> jobs -[1] 1423 Running du -s / > /dev/null +[2] 1428 Stopped vi x
|
Switchboard | ||||
Latest | |||||
Past week | |||||
Past month |
Aug 10, 2020 | www.howtogeek.com
How to Run and Control Background Processes on Linux DAVE MCKAY @thegurkha
SEPTEMBER 24, 2019, 8:00AM EDTUse the Bash shell in Linux to manage foreground and background processes. You can use Bash's job control functions and signals to give you more flexibility in how you run commands. We show you how.
How to Speed Up a Slow PChttps://imasdk.googleapis.com/js/core/bridge3.401.2_en.html#goog_863166184 All About Processes
Whenever a program is executed in a Linux or Unix-like operating system, a process is started. "Process" is the name for the internal representation of the executing program in the computer's memory. There is a process for every active program. In fact, there is a process for nearly everything that is running on your computer. That includes the components of your graphical desktop environment (GDE) such as GNOME or KDE , and system daemons that are launched at start-up.
Why nearly everything that is running? Well, Bash built-ins such as cd , pwd , and alias do not need to have a process launched (or "spawned") when they are run. Bash executes these commands within the instance of the Bash shell that is running in your terminal window. These commands are fast precisely because they don't need to have a process launched for them to execute. (You can type
help
in a terminal window to see the list of Bash built-ins.)Processes can be running in the foreground, in which case they take over your terminal until they have completed, or they can be run in the background. Processes that run in the background don't dominate the terminal window and you can continue to work in it. Or at least, they don't dominate the terminal window if they don't generate screen output.
A Messy ExampleWe'll start a simple
ping
trace running . We're going toping
the How-To Geek domain. This will execute as a foreground process.ping www.howtogeek.com
We get the expected results, scrolling down the terminal window. We can't do anything else in the terminal window while
ping
is running. To terminate the command hitCtrl+C
.Ctrl+C
The visible effect of the
Ctrl+C
is highlighted in the screenshot.ping
gives a short summary and then stops.Let's repeat that. But this time we'll hit
Ctrl+Z
instead ofCtrl+C
. The task won't be terminated. It will become a background task. We get control of the terminal window returned to us.ping www.howtogeek.comCtrl+Z
The visible effect of hitting
Ctrl+Z
is highlighted in the screenshot.This time we are told the process is stopped. Stopped doesn't mean terminated. It's like a car at a stop sign. We haven't scrapped it and thrown it away. It's still on the road, stationary, waiting to go. The process is now a background job .
The
jobs
command will list the jobs that have been started in the current terminal session. And because jobs are (inevitably) processes, we can also use theps
command to see them. Let's use both commands and compare their outputs. We'll use theT
option (terminal) option to only list the processes that are running in this terminal window. Note that there is no need to use a hyphen-
with theT
option.jobsps T
The
jobs
command tells us:
- [1] : The number in square brackets is the job number. We can use this to refer to the job when we need to control it with job control commands.
- + : The plus sign
+
shows that this is the job that will be acted upon if we use a job control command without a specific job number. It is called the default job. The default job is always the one most recently added to the list of jobs.- Stopped : The process is not running.
- ping www.howtogeek.com : The command line that launched the process.
The
ps
command tells us:
- PID : The process ID of the process. Each process has a unique ID.
- TTY : The pseudo-teletype (terminal window) that the process was executed from.
- STAT : The status of the process.
- TIME : The amount of CPU time consumed by the process.
- COMMAND : The command that launched the process.
These are common values for the STAT column:
- D : Uninterruptible sleep. The process is in a waiting state, usually waiting for input or output, and cannot be interrupted.
- I : Idle.
- R : Running.
- S : Interruptible sleep.
- T : Stopped by a job control signal.
- Z : A zombie process. The process has been terminated but hasn't been "cleaned down" by its parent process.
The value in the STAT column can be followed by one of these extra indicators:
- < : High-priority task (not nice to other processes).
- N : Low-priority (nice to other processes).
- L : process has pages locked into memory (typically used by real-time processes).
- s : A session leader. A session leader is a process that has launched process groups. A shell is a session leader.
- l : Multi-thread process.
- + : A foreground process.
We can see that Bash has a state of
Ss
. The uppercase "S" tell us the Bash shell is sleeping, and it is interruptible. As soon as we need it, it will respond. The lowercase "s" tells us that the shell is a session leader.The ping command has a state of
T
. This tells us thatping
has been stopped by a job control signal. In this example, that was theCtrl+Z
we used to put it into the background.The
The bg Commandps T
command has a state ofR
, which stands for running. The+
indicates that this process is a member of the foreground group. So theps T
command is running in the foreground.The
bg
command is used to resume a background process. It can be used with or without a job number. If you use it without a job number the default job is brought to the foreground. The process still runs in the background. You cannot send any input to it.If we issue the
bg
command, we will resume ourping
command:bg
The
ping
command resumes and we see the scrolling output in the terminal window once more. The name of the command that has been restarted is displayed for you. This is highlighted in the screenshot.
But we have a problem. The task is running in the background and won't accept input. So how do we stop it?
Ctrl+C
doesn't do anything. We can see it when we type it but the background task doesn't receive those keystrokes so it keeps pinging merrily away.
In fact, we're now in a strange blended mode. We can type in the terminal window but what we type is quickly swept away by the scrolling output from the
ping
command. Anything we type takes effect in the foregound.To stop our background task we need to bring it to the foreground and then stop it.
The fg CommandThe
fg
command will bring a background task into the foreground. Just like thebg
command, it can be used with or without a job number. Using it with a job number means it will operate on a specific job. If it is used without a job number the last command that was sent to the background is used.If we type
fg
ourping
command will be brought to the foreground. The characters we type are mixed up with the output from theping
command, but they are operated on by the shell as if they had been entered on the command line as usual. And in fact, from the Bash shell's point of view, that is exactly what has happened.fg
And now that we have the
ping
command running in the foreground once more, we can useCtrl+C
to kill it.Ctrl+CWe Need to Send the Right Signals
That wasn't exactly pretty. Evidently running a process in the background works best when the process doesn't produce output and doesn't require input.
But, messy or not, our example did accomplish:
- Putting a process into the background.
- Restoring the process to a running state in the background.
- Returning the process to the foreground.
- Terminating the process.
When you use
Ctrl+C
andCtrl+Z
, you are sending signals to the process. These are shorthand ways of using thekill
command. There are 64 different signals thatkill
can send. Usekill -l
at the command line to list them.kill
isn't the only source of these signals. Some of them are raised automatically by other processes within the systemHere are some of the commonly used ones.
- SIGHUP : Signal 1. Automatically sent to a process when the terminal it is running in is closed.
- SIGINT : Signal 2. Sent to a process you hit
Ctrl+C
. The process is interrupted and told to terminate.- SIGQUIT : Signal 3. Sent to a process if the user sends a quit signal
Ctrl+D
.- SIGKILL : Signal 9. The process is immediately killed and will not attempt to close down cleanly. The process does not go down gracefully.
- SIGTERM : Signal 15. This is the default signal sent by
kill
. It is the standard program termination signal.- SIGTSTP : Signal 20. Sent to a process when you use
Ctrl+Z
. It stops the process and puts it in the background.We must use the
Further Job Controlkill
command to issue signals that do not have key combinations assigned to them.A process moved into the background by using
Ctrl+Z
is placed in the stopped state. We have to use thebg
command to start it running again. To launch a program as a running background process is simple. Append an ampersand&
to the end of the command line.Although it is best that background processes do not write to the terminal window, we're going to use examples that do. We need to have something in the screenshots that we can refer to. This command will start an endless loop as a background process:
while true; do echo "How-To Geek Loop Process"; sleep 3; done &
We are told the job number and process ID id of the process. Our job number is 1, and the process id is 1979. We can use these identifiers to control the process.
The output from our endless loop starts to appear in the terminal window. As before, we can use the command line but any commands we issue are interspersed with the output from the loop process.
ls
To stop our process we can use
jobs
to remind ourselves what the job number is, and then usekill
.
jobs
reports that our process is job number 1. To use that number withkill
we must precede it with a percent sign%
.jobskill %1
kill
sends theSIGTERM
signal, signal number 15, to the process and it is terminated. When the Enter key is next pressed, a status of the job is shown. It lists the process as "terminated." If the process does not respond to thekill
command you can take it up a notch. Usekill
withSIGKILL
, signal number 9. Just put the number 9 between thekill
command the job number.kill 9 %1Things We've Covered
- Ctrl+C : Sends
SIGINT
, signal 2, to the process -- if it is accepting input -- and tells it to terminate.- Ctrl+D : Sends
SISQUIT
, signal 3, to the process -- if it is accepting input -- and tells it to quit.- Ctrl+Z : Sends
SIGSTP
, signal 20, to the process and tells it to stop (suspend) and become a background process.- jobs : Lists the background jobs and shows their job number.
- bg job_number : Restarts a background process. If you don't provide a job number the last process that was turned into a background task is used.
- fg job_number : brings a background process into the foreground and restarts it. If you don't provide a job number the last process that was turned into a background task is used.
- commandline & : Adding an ampersand
&
to the end of a command line executes that command as a background task, that is running.- kill % job_number : Sends
SIGTERM
, signal 15, to the process to terminate it.- kill 9 % job_number : Sends
SIGKILL
, signal 9, to the process and terminates it abruptly.
Jul 23, 2020 | www.redhat.com
More Linux resources
- Download Now: Linux Commands Cheat Sheet
- Advanced Linux Commands Cheat Sheet for Developers
- Download Red Hat Enterprise Linux Server 8 Trial
- Linux System Administration Skills Assessment
In this quick tutorial, I want to look at the
Jobsjobs
command and a few of the ways that we can manipulate the jobs running on our systems. In short, controlling jobs lets you suspend and resume processes started in your Linux shell.The
jobs
command will list all jobs on the system; active, stopped, or otherwise. Before I explore the command and output, I'll create a job on my system.I will use the
sleep
job as it won't change my system in any meaningful way.[tcarrigan@rhel ~]$ sleep 500 ^Z [1]+ Stopped sleep 500First, I issued the
sleep
command, and then I received the Job number [1]. I then immediately stopped the job by using Ctl+Z . Next, I run thejobs
command to view the newly created job:[tcarrigan@rhel ~]$ jobs [1]+ Stopped sleep 500You can see that I have a single stopped job identified by the job number [1] .
Other options to know for this command include:
Background
- -l - list PIDs in addition to default info
- -n - list only processes that have changed since the last notification
- -p - list PIDs only
- -r - show only running jobs
- -s - show only stopped jobs
Next, I'll resume the
sleep
job in the background. To do this, I use thebg
command. Now, thebg
command has a pretty simple syntax, as seen here:bg [JOB_SPEC]Where JOB_SPEC can be any of the following:
- %n - where n is the job number
- %abc - refers to a job started by a command beginning with abc
- %?abc - refers to a job started by a command containing abc
- %- - specifies the previous job
NOTE :
bg
andfg
operate on the current job if no JOB_SPEC is provided.I can move this job to the background by using the job number [1] .
[tcarrigan@rhel ~]$ bg %1 [1]+ sleep 500 &You can see now that I have a single running job in the background.
[tcarrigan@rhel ~]$ jobs [1]+ Running sleep 500 &ForegroundNow, let's look at how to move a background job into the foreground. To do this, I use the
fg
command. The command syntax is the same for the foreground command as with the background command.fg [JOB_SPEC]Refer to the above bullets for details on JOB_SPEC.
I have started a new
sleep
in the background:[tcarrigan@rhel ~]$ sleep 500 & [2] 5599Now, I'll move it to the foreground by using the following command:
[tcarrigan@rhel ~]$ fg %2 sleep 500The
The endfg
command has now brought my system back into a sleep state.While I realize that the jobs presented here were trivial, these concepts can be applied to more than just the
sleep
command. If you run into a situation that requires it, you now have the knowledge to move running or stopped jobs from the foreground to background and back again.
Google matched content |
Society
Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers : Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism : The Iron Law of Oligarchy : Libertarian Philosophy
Quotes
War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda : SE quotes : Language Design and Programming Quotes : Random IT-related quotes : Somerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose Bierce : Bernard Shaw : Mark Twain Quotes
Bulletin:
Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 : Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law
History:
Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds : Larry Wall : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOS : Programming Languages History : PL/1 : Simula 67 : C : History of GCC development : Scripting Languages : Perl history : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history
Classic books:
The Peter Principle : Parkinson Law : 1984 : The Mythical Man-Month : How to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite
Most popular humor pages:
Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor
The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D
Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.
This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...
|
You can use PayPal to to buy a cup of coffee for authors of this site |
Disclaimer:
The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.
Last modified: August, 10, 2020