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.
Comments - most recent first
(Please feel free to answer questions posted by others!)
I think what you are doing is really fantastic
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 ;-))
no one can create onother process with same cmd.
If they all have the same name, this will do the trick
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.
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.
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?
Thanks in advance.
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?
Thanks a lot! Searched this information for a long time.
NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Copyright © by -
All rights reserved - Redistribution is allowed only with permission.