Linux Classes
Share This With a Friend  
LINUX CLASSES - LINUX BASICS

Linux Processes

What Are Linux Processes?

Listing Processes

Linux is a multitasking operating system, which means that more than one task can be active at once. To find out what tasks are running on your system concurrently, use the command ps -f
UID PID PPID STIME TTY TIME COMD
hermie 24 1 00:35:28 tty1 0:01 bash
hermie 137 24 00:36:39 tty1 0:00 ps -f

The output here shows, for each active task, the UID (owning user), PID (process ID), PPID (parent process ID), STIME (when the task started), TIME (how long the task has been active), and CMD (the actual command line used to start the task). If you exa mine the PIDs and PPIDs, you can see that bash invoked the ps -f command, because the PPID of the latter matches the PID of the former.

Launching Tasks in the Foreground and Background

Suppose you have a long-running task (for example, compiling a large program) that you need to run, but you also want to get some other work done. Linux lets you start a task in the background and keep on doing other things from the command prompt. By a dding the ampersand (&) to the end of any command, you can launch it in the background and get your command prompt back right away. For example,

cc hugepgm.c > outlist &

will start cc (the C compiler) as a background task, executing it in parallel with other tasks on your system.

Note: It's a good idea to redirect the output of background tasks to a file, as shown here, since the background task still shares the console with foreground tasks. If you don't, the background task will splash any output it might produce all over your screen while you're editing a file or typing another command.

If you start a long-running task and forget to add the ampersand, you can still swap that task into the background. Instead of pressing ctrl-C (to terminate the foreground task) and then restarting it in the background, just press ctrl-Z after the command starts, type bg, and press enter. You'll get your prompt back and be able to continue with other work. Use the fg command to bring a background task to the foreground.

You might wonder why you'd ever want to swap programs between the foreground and background, but this is quite useful if for example you're doing a long-running compile and you need to issue a quick command at the shell prompt. While the compilation is running, you could press ctrl-Z and then enter the bg command to put the compiler in the background. Then do your thing at the shell prompt and enter the fg command to return the compiler task to the foreground. The ctrl-Z trick also works with the Emacs text editor and the Pine email program. You can suspend either program and then return t o your work in progress with the fg command.

Of course, in the X Windows environment, all these unnatural gyrations are not necessary. Just start another shell window and run the other command there. You can watch both processes running in separate windows at the same time, and you don't have to w orry about adding ampersands, piping output to files, or keeping track of foreground versus background processes.

Previous Lesson: Pipelines
Next Lesson: Stopping a Program

[ RETURN TO INDEX ]


   

Comments - most recent first
(Please feel free to answer questions posted by others!)

Raana Jabeen     (04 Nov 2012, 11:45)
Why does a shell process terminate when you press <Ctrl + D> at the beginning of new line??
ben     (08 Oct 2012, 07:33)
useful info
haneen     (06 Oct 2012, 23:52)
how to do this task on fedora :Assume a directory contains many .mp3 files taken from albumes. If you are asked to sort them out into separate folders by artist. How would you do that?
ouali     (13 Mar 2012, 05:47)
thank you sir,
I think what you are doing is really fantastic
Renegade     (06 Jun 2011, 06:53)
Thank your for the Overview. It was very interesting and i've learned some new things, thanks!

P.S. (to azeem) I don't want to do something, so please help me. I want to eat but i don't want to chomp, so please feed me ;-))
azeem     (02 Apr 2011, 14:56)
Hiiii,i want to make a vmware for android as my project.Plz...help me how to start with kernal programming
CymnLyncmit     (14 Mar 2011, 17:52)
Thank you, I've been on this age, your topic information seaching is the best I have ever found.
nice one     (31 Dec 2010, 05:38)
can any one tell me how to create one process with one cmd.
no one can create onother process with same cmd.
Brad Doty     (19 Nov 2010, 14:22)
Hany,

If they all have the same name, this will do the trick
pkill name
Of course there are a gillion cmd line options, and you can fiddle with pgrep or ps -ef | grep to refine the set of jobs you want to kill.
Brad Doty     (19 Nov 2010, 14:19)
Chandra,

Yes, the fg, bg, and ^z or stop (unless you need to be root on your system for stop) are an algebra. Type the command >jobs
This will give you a list of background and stopped jobs in your current Unix session/window, with job #s (single digits, not the pids).
>fg %1 or just >fg will bring job 1 into the foreground. ^z stops a foreground job.
>bg %2 will run stopped job #2 in the backgd.
Hany Harraz     (18 Oct 2010, 15:57)
Nicee!!
but what if I start a couple of scripts that runs in an infinite loop for a purpose, and then I want to kill them all, how do I do that?
Piyush Kumar Jha     (12 Oct 2010, 03:12)
Can anyone let me know how the malicious processes running in Linux Server can be found?

Thanks in advance.
Bob Rankin     (26 Aug 2010, 15:51)
Yes... the answer is in the article.
chandrasekhar     (26 Aug 2010, 00:16)
Hi,

I have one doubt here. Suppose after pressing ctrl+z and bg, our process runs in background. Is there any way to get that process foreground?
amit     (21 Jul 2010, 10:59)
i want to learn more about linux can u help me
r3velate     (19 Jul 2010, 20:49)
Yep thanks for the help. Very useful.
yuga     (04 Jun 2010, 00:49)
Cool!
Thanks a lot! Searched this information for a long time.
Ubay Oramas     (14 May 2010, 07:48)
Great and classic tips. Thanks!
just     (29 Apr 2010, 13:31)
Thanks for this, it's really useful especially about ctrl+z bg, fg modes.

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.