Softpanorama

May the source be with you, but remember the KISS principle ;-)
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

jobs

News batch command Books Recommended Links jobs atq command

Reference

Examples
Time Specification Options   at.allow/at.deny Unix at command Grid engine SGE Parallel Environment Creating and modifying SGE Queues
date command nohup command Enterprise Job schedulers   Cron Tips Sysadmin Horror Stories

Humor

Etc

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.

1.  Type du -s / > /dev/null & and press Return. This places a fairly long running job in background.
2.  Type vi x and press Return.
3.  Press Ctrl-Z to suspend the vi session.
4.  Now type jobs to display the list of background and suspended jobs.
     cj> jobs
     -[1] 1423    Running        du -s / > /dev/null
     +[2] 1428    Stopped        vi x
5.  To kill the background job type kill %- and press Return. Notice you are notified after a couple of seconds that the process was terminated.
6.  Now type fg to re-enter vi. The suspended vi process is brought back into interactive mode.
7.  Type :q! to exit vi.
8.  Turn to Module 129 to continue the learning sequence.

Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

[Aug 10, 2020] How to Run and Control Background Processes on Linux

Aug 10, 2020 | www.howtogeek.com

How to Run and Control Background Processes on Linux DAVE MCKAY @thegurkha
SEPTEMBER 24, 2019, 8:00AM EDT

A shell environment on a Linux computer.
Fatmawati Achmad Zaenuri/Shutterstock.com

Use 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 PC

https://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 Example

We'll start a simple ping trace running . We're going to ping the How-To Geek domain. This will execute as a foreground process.

ping www.howtogeek.com

ping www.howtogeek.com in a terminal window

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 hit Ctrl+C .

Ctrl+C

ping trace output in a terminal window

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 of Ctrl+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.com
Ctrl+Z

effect of Ctrl+Z on a command running in a terminal window

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 the ps command to see them. Let's use both commands and compare their outputs. We'll use the T 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 the T option.

jobs
ps T

jobs command in a terminal window

The jobs command tells us:

The ps command tells us:

These are common values for the STAT column:

The value in the STAT column can be followed by one of these extra indicators:

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 that ping has been stopped by a job control signal. In this example, that was the Ctrl+Z we used to put it into the background.

The ps T command has a state of R , which stands for running. The + indicates that this process is a member of the foreground group. So the ps T command is running in the foreground.

The bg Command

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 our ping command:

bg

bg in a terminal window

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.

resumed ping background process with output in a terminal widow

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.

Background task ignoring Ctrl+C in a terminal window

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 Command

The fg command will bring a background task into the foreground. Just like the bg 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 our ping command will be brought to the foreground. The characters we type are mixed up with the output from the ping 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

fg command mixed in with the output from ping in a terminal window

And now that we have the ping command running in the foreground once more, we can use Ctrl+C to kill it.

Ctrl+C

Ctrl+C stopping the ping command in a terminal window

We 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:

When you use Ctrl+C and Ctrl+Z , you are sending signals to the process. These are shorthand ways of using the kill command. There are 64 different signals that kill can send. Use kill -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 system

Here are some of the commonly used ones.

We must use the kill command to issue signals that do not have key combinations assigned to them.

Further Job Control

A process moved into the background by using Ctrl+Z is placed in the stopped state. We have to use the bg 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 &

while true; do echo "How-To Geek Loop Process"; sleep 3; done & in a terminal window

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

output of the background loop process interspersed with output from other commands

To stop our process we can use jobs to remind ourselves what the job number is, and then use kill .

jobs reports that our process is job number 1. To use that number with kill we must precede it with a percent sign % .

jobs
kill %1

jobs and kill %1 in a terminal window

kill sends the SIGTERM 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 the kill command you can take it up a notch. Use kill with SIGKILL , signal number 9. Just put the number 9 between the kill command the job number.

kill 9 %1
Things We've Covered

RELATED: How to Kill Processes From the Linux Terminal

[Jul 29, 2020] Linux Commands- jobs, bg, and fg by Tyler Carrigan

Jul 23, 2020 | www.redhat.com
Image

Photo by Andrea Piacquadio from Pexels

More Linux resources

In this quick tutorial, I want to look at the jobs 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.

Jobs

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 500

First, 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 the jobs command to view the newly created job:

[tcarrigan@rhel ~]$ jobs
[1]+  Stopped                 sleep 500

You can see that I have a single stopped job identified by the job number [1] .

Other options to know for this command include:

Background

Next, I'll resume the sleep job in the background. To do this, I use the bg command. Now, the bg command has a pretty simple syntax, as seen here:

bg [JOB_SPEC]

Where JOB_SPEC can be any of the following:

NOTE : bg and fg 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 &
Foreground

Now, 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] 5599

Now, I'll move it to the foreground by using the following command:

[tcarrigan@rhel ~]$ fg %2
sleep 500

The fg command has now brought my system back into a sleep state.

The end

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.

Recommended Links

Google matched content

Softpanorama Recommended

Top articles

Sites



Etc

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 quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard 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 DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting 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-MonthHow 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