Table of Contents
Every command that is started, is load into the memory by the kernel. The kernel is the thing that handles the hardware and executes programs. When the command is loaded it got a process identification (PID) assigned. The PID is unique within the operating system instance.
Most commands you will start, will only run for a certain time and then terminate, either because you requested so, or because their work is done. But there are processes that run in the background as long as the operating system runs. They are called daemons. Web servers, for instance, most like run as daemons.
To see the list of processes that are running on your system, use the
command ps. On a FreeBSD box invoke it using the
ax options as shown in Example 23, “Output of the ps command on FreeBSD”.
Example 23. Output of the ps command on FreeBSD
$psauxPID TT STAT TIME COMMAND 0 ?? DLs 0:00.04 [swapper] 1 ?? SLs 0:00.02 /sbin/init -- 2 ?? DL 0:01.20 [g_event] 41 ?? DL 0:00.04 [fdc0] 42 ?? DL 0:00.05 [pfpurge] 43 ?? DL 0:00.01 [pagedaemon] 44 ?? DL 0:00.00 [vmdaemon] 45 ?? DL 0:00.00 [pagezero] 396 ?? Ss 0:02.52 /usr/sbin/moused -p /dev/ums0 -t auto -I /var/run/mou 423 ?? Is 0:00.00 /usr/sbin/moused -p /dev/ums1 -t auto -I /var/run/mou 471 ?? Is 0:00.00 /sbin/devd 541 ?? DL 0:00.01 [accounting] 628 ?? Ss 0:00.09 /usr/sbin/syslogd -s 649 ?? Ss 0:00.02 /usr/sbin/rpcbind 664 ?? Ss 0:00.05 /usr/sbin/ypbind 692 ?? Ss 0:00.01 /usr/sbin/rpc.statd 699 ?? Is 0:00.00 /usr/sbin/rpc.lockd
The first column shows the PID of the processes. Processes in square brackets are kernel processes (there is no command for them available, they were created directly by the kernel.
On Sun Solaris and Linux machines, you use the ps with the
-ef, as shown in Example 24, “Output of the ps command on Linux and Sun Solaris
machines”.
Example 24. Output of the ps command on Linux and Sun Solaris machines
$ps-efUID PID PPID C STIME TTY TIME CMD root 0 0 0 10:12:53 ? 0:43 sched root 1 0 0 10:12:58 ? 0:00 /sbin/init root 2 0 0 10:12:58 ? 0:00 pageout root 3 0 0 10:12:58 ? 0:16 fsflush root 319 318 0 10:13:25 ? 0:00 /usr/sadm/lib/smc/bin/smcboot root 7 1 0 10:12:59 ? 0:03 /lib/svc/bin/svc.startd root 9 1 0 10:13:00 ? 0:10 /lib/svc/bin/svc.configd root 529 510 0 10:13:34 ? 0:00 /usr/openwin/bin/fbconsole -d root 82 1 0 10:13:11 ? 0:00 devfsadmd root 255 1 0 10:13:24 ? 0:00 /usr/lib/netsvc/yp/ypbind jack 668 653 0 10:17:40 ? 0:00 /bin/ksh /etc/dt/config/Xsessi root 275 7 0 10:13:25 ? 0:00 /usr/lib/saf/sac -t 300 daemon 155 1 0 10:13:16 ? 0:01 /usr/lib/rcap/rcapd
The first column holds the user name of the user that has started the command. The second column shows the PID of the process.
It is possible to watch the processes in real time using the top command on FreeBSD and Linux. The top will also show their resource usage, such as cpu-time and memory consumption (see Example 25, “The top on FreeBSD and Linux”). In order to leave top press q on the keyboard.
Example 25. The top on FreeBSD and Linux
$ top
last pid: 13481; load averages: 0.14, 0.23, 0.18 up 0+02:53:49 15:45:03
3 processes: 1 running, 2 sleeping
CPU: 0.8% user, 0.0% nice, 2.6% system, 0.2% interrupt, 96.4% idle
Mem: 367M Active, 224M Inact, 98M Wired, 9564K Cache, 112M Buf, 800M Free
Swap: 3015M Total, 3015M Free
PID USERNAME THR PRI NICE SIZE RES STATE C TIME WCPU COMMAND
13275 joe 1 20 0 5504K 2524K pause 1 0:00 0.00% tcsh
13281 joe 1 44 0 3496K 1752K CPU1 1 0:00 0.00% top
13273 joe 1 44 0 8332K 4032K select 0 0:00 0.00% sshdSome Sun Solaris machines do not have the top. Use prstat instead as shown in Example 26, “The prstat command on Sun Solaris”. Prstat will also stay running unless you press the q key.
Example 26. The prstat command on Sun Solaris
$ prstat
PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP
19988 jack 31M 25M sleep 49 0 0:00:41 1.6% emacs-gtk-22.1/1
511 jack 42M 80M run 58 0 0:07:03 1.3% Xorg/1
688 jack 16M 10M sleep 49 0 0:01:46 0.6% gkrellm/1
689 jack 16M 10M sleep 49 0 0:01:05 0.3% gkrellm/1
742 jack 144M 77M sleep 59 0 0:03:54 0.3% firefox-bin/7Sometimes it is necessary to terminate processes that got unresponsive or behave otherwise oddly. On all UNIXes, the kill command can be used to send processes a signal to terminate. Find out the PID of the process in question and use the kill command as shown below.
Replace <pid> with the PID of the
process. Sometimes you want to kill a process, you would use then the
additional -KILL option.
The difference between kill with and without the
-KILL option, is that kill without
that option lets terminate the process more gracefully, whereas
kill with -KILL kills the process
without mercy, so to speak.
If you happen to know the name of the command you want to terminate, you could use also the killall command on Linux, or pkill on FreeBSD and Sun Solaris. They both take the name of the process to terminate, and terminate all processes with that name (see Example 29, “The pkill and killall commands”).
Example 29. The pkill and killall commands
Use the pkill command on Sun Solaris and FreeBSD to kill processes with the same name.
$pkill<procname>
On Linux, you use the killall command.
$killall<procname>
Replace <procname> with the name of the process to
terminate. You optionally could provide the -KILL switch, in
the same manner as with kill.
If you simply want to terminate the command you just started on the command line, you might try pressing Ctrl+C. In most cases this will terminate the active command.
Please keep in mind, that you can only terminate or kill processes you launched. Processes from other users are protected from termination other than the “owner” of the process. As usual, root is the exception, it can terminate any process.
The bash, ksh, and tcsh offer a feature called job
control. Job control allows to start more than one command at
a time from the command line. Job control is invoked using either the
& sign, or by pressing Ctrl+Z. Using
either way, you put the command in the background and you can fire up a
new command. The difference is, that a command invoked with the
& sign is running in the background, whereas a
command is suspended in the background when using Ctrl+Z.
You use the & sign when starting a new command on
the command line as shown in Example 30, “Starting a command in the background”. Simply
but a the end of the line a &, and the command is
started in the background.
If you want to put the active command in background, press Ctrl+Z as in Example 31, “Putting the active command in the background”.
Example 31. Putting the active command in the background
The example shows a file being edited in a text editor. By pressing Ctrl+Z, the text editor is put in the background and the prompt appears.
use warnings;
use List::Util 'shuffle';
# in minutes
my $SLEEP=3;
my $WMSETBG='/opt/csw/bin/wmsetbg';
my @DIRECTORIES= (
'/mnt/media/background'
);
"chbg.pl" 45 lines, 865 characters
Suspended
$
Note the Suspended notice. The command is
still there, but it does not run.
A command that has been suspended with Ctrl+Z can be run in the background calling bg right after pressing Ctrl+Z as shown in Example 32, “Running a suspended job in the background”. If you have more than one background processes, see first the output of the jobs command (Example 33, “Using the jobs command”) and invoke bg with the job number.
A command suspended or running in the background can be brought back to the foreground by calling fg. It uses the same syntax as bg.
Example 33. Using the jobs command
$ jobs
[1] Suspended vi chbg.pl
[2] - Suspended (tty input) cat
[3] + Suspended ls -R /Use the number of the first column when you want to background or activate a command using bg or fg as shown below.
$bg% <jobnum>[...]$fg% <jobnum>
Replace <jobnum> with the number obtained from
jobs.