Unix Fundamentals Chapter 08
Unix Fundamentals Chapter 08
______________________________________________________________________________
Chapter Eight
In This Chapter
Command Action
Printing Files
The command nlp file_name submits a file to a user's default printer, the latter as specified by
the LPDEST shell variable. Useful options include:
Command Action
(Alert: At a customer site, the command would “lp” or “lpr”. “nlp” is a sas-ified version of the
print command )
Example
prompt>nlp -l Enter
a4245b21 ahplj11 ahplj12 ahplj13 ahplj21
ahplj22 ahplj31 ahplj32 ainpr1 alhplj1
.
.
.
Process Management
Each command generates at least one machine process. When a process becomes stuck (or runaway),
the program may stop accepting input or producing output. Runaway processes should be terminated
(or killed), as they continue to tax the system‘s resources. Each process has its own unique identifier,
commonly referred to as the PID (Process ID).
Command Action
kill -list lists all available signals for the kill command
The kill command, without any options, sends a process (PID) a ―polite‖ signal to terminate. Since it
is a polite request, the kill command will utilize any built in shutdown routines the process may have.
The default signal number sent (the polite one) is signal 15, which is SIGTERM. It is possible for
programs to ―trap‖ the SIGTERM signal – effectively ignoring your kill command. If this happens,
you can use the ―kill -9 PID‖ command to terminate a process immediately. The shutdown routines
built into this process will be ignored, but the process should terminate immediately.
You, as a user, can only kill processes that you started. You can not kill another user‘s process. On-
ly the superuser (root) can do that.
Be careful when using the kill command. If you accidentally kill the shell process you are currently
using, you will effectively log yourself out.
Note: In the beginning, kill was used only to terminate a process. There are now so many signals
available to the kill command that it can be used for much more than process termination. Kill can
also suspend a process or change it from the foreground to background, though it is used less fre-
quently for those tasks. To view a complete list of the signals available on your system, use the ―kill
–list‖ command. View the man page for the kill command for further information on its uses with
the additional signals available.
UNIX Fundamentals Workshop 183
______________________________________________________________________________
Examples:
Remember, the second column in the ―ps –ef‖ output contains the PID. The third column contains
the PID of the PARENT PROCESS, which is usually not the one you want to associate with a kill
command.
Check to see if the process was indeed terminated by the kill command:
Hmmm…it wasn‟t. This is an example of when you would have to use the “-9” op-
tion on the kill command in order to terminate the process:
A ps listing no longer shows the 17726 PID. It has successfully been terminated:
All shells understand a job as a group of processes. Consider the pipeline ―ps –ef | grep sssims‖; it is
a single job comprised of two processes.
When you enter a command, or set of commands, at your shell prompt - you have to wait for them to
complete before your terminal is freed up for additional input. In UNIX-speak, the parent process
waits for the child process (or processes) to die before returning control to the shell.
There will be times when you want to run a job in the background, allowing your shell to be freed up
to do additional tasks concurrently. You can accomplish this in two ways: with the shell‘s & opera-
tor and with the nohup command.
Example
The effect of the & operator can clearly be seen with the sleep command. In the following
example, your terminal is “asleep” for 10 seconds while it waits for the sleep command to
complete. After the 10 seconds have passed, your prompt returns.:
prompt>sleep 10 Enter
If you add the & operator to the command, the process id (PID) of the command will be out-
put and your prompt will return immediately. The sleep command is still running. It has
been placed in the background by the shell – freeing up your command prompt for additional
work.
Note: When you logout, any jobs you have started with the & operator will be terminated.
UNIX Fundamentals Workshop 185
______________________________________________________________________________
nohup
When a command is run with nohup (no hangup), the process will continue to run even after the user
is logged out. This is actually not required in the C-shell or Bash shell because background processes
started in these shells continue to run even after the user has logged out. However, nohup is re-
quired for the Bourne and Korn shells if you want your processes to continue past the life of your
shell. When using nohup, you should also use the & operator.
Example
Consider you have a SAS program, start_shr5.sas, that starts a SAS/SHARE server that you
want to persist beyond your login session. You can add nohup to the command that invokes
SAS to accomplish this:
prompt>ps Enter
PID TTY CMD
1787 pts/63 sas
1428 pts/63 ksh
You can now safely logout of the system, and your SAS/SHARE server will continue to run.
186 UNIX Fundamentals Workshop
______________________________________________________________________________
Job Control
Job control in UNIX allows you to move jobs (collections of processes) between the foreground and
background. You can also use job control to kill, suspend, or continue a job. Most shells support job
control.
Command Action
Each job is identified by a unique Job ID. This is different than the Process ID (PID) we have been
using.
UNIX Fundamentals Workshop 187
______________________________________________________________________________
Examples
Consider you have a SAS program, start_shr5.sas, that starts a SAS/SHARE server that you
want to persist beyond your login session. Add nohup to the command that invokes SAS to
accomplish this:
prompt>ps Enter
PID TTY CMD
1787 pts/63 sas
1429 pts/63 ksh
Use the “jobs” command to see a list of jobs that are available to use with job control:
prompt>jobs Enter
[2] + Running sas –sysin start_shr5.sas &
[1] - Running find /usr/local -name a.out –print &
prompt> fg %1 Enter
The find command associated with Job 1 is now in the foreground. I can suspend it by using
Control-Z:
[Control Z]
[1] Stopped (SIGSTP) find /usr/local -name a.out –print &
prompt>
Control-Z does not terminate the process. It merely puts it in a suspended state. Using fg
with the Job‟s ID would bring it back to the foreground and the find command would contin-
ue its work.
Top
The command top displays processes ranked by resource use. By default, the list is refreshed every
five seconds.
Top is available on most Unix systems. When not provided with the operating system, it is often
downloaded and installed by the System Administrator.
Exercises Chapter 8
1. Let‘s say you start a command that is taking too long and you want to terminate it, but the typ-
ical interrupt sequence Control-C doesn‘t terminate the process. No matter how many keys
you bang on your keyboard, you can‘t get a prompt to return so you can actually DO some-
thing. What steps could you go through to determine the PID (process ID) of the offending
command so you can terminate it? (hint, since your current windows isn‘t of much use to
you….you might need a new one).
2. What command would you then use to terminate the stubborn process?
3. Using vi, create a file in your ~/unix_intro/temp directory named ―longsleep‖ with the follow-
ing line:
longsleep &
longsleep &
Your shell will display the job numbers followed by the PID of each instance of the longsleep
script you have running in the background.
6. Issue the command to bring one of your longsleep jobs into the foreground. That will now be
your ―active‖ process. Issue ―Control-C‖ to terminate it.
7. Repeat the sequence for the other longsleep process until you are left with no longsleep ―jobs‖
still running.
What command would you use to terminate the find command listed above?
9. What command would you use to view the processes using up the most resources on a sys-
tem?
190 UNIX Fundamentals Workshop
______________________________________________________________________________