0% found this document useful (0 votes)
4 views

Unix Fundamentals Chapter 08

Chapter Eight of the UNIX Fundamentals Workshop covers printing files and process management. It details commands for managing background and foreground processes, job control, and printing tasks, including the use of 'nohup' to maintain processes after logout. The chapter also provides examples and exercises to reinforce the concepts discussed.

Uploaded by

gauravsom789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unix Fundamentals Chapter 08

Chapter Eight of the UNIX Fundamentals Workshop covers printing files and process management. It details commands for managing background and foreground processes, job control, and printing tasks, including the use of 'nohup' to maintain processes after logout. The chapter also provides examples and exercises to reinforce the concepts discussed.

Uploaded by

gauravsom789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

UNIX Fundamentals Workshop 177

______________________________________________________________________________

Chapter Eight

Printing and Processes


Printing Files
Process Management
Background Processing
Using nohup
Job Control
List Top Processes
178 UNIX Fundamentals Workshop
______________________________________________________________________________
UNIX Fundamentals Workshop 179
______________________________________________________________________________

In This Chapter

Command Action

bg %job_number move job_number to the background

fg %job_number move job_niumber to the foreground

jobs display all current jobs

kill id_number terminate process

kill -9 id_number terminate process and bypass any shutdown routines

lp file_name print file on default printer

lp -d printer_name file_name print file on different printer

lpstat display contents of default printer print queue

lpstat -d display name of default printer

lpstat -d printer_name display contents of different printer print queue

nohup command & run a command in the background and maintain


the process even after the user logs out

ps -ef list processes

top display processes using the most CPU power

Internal Note: For internal use on the SAS HP network, the


commands related to printing are “nlp” and “nlpstat” instead of
“lp” and “lpstat”, respectively.
180 UNIX Fundamentals Workshop
______________________________________________________________________________
UNIX Fundamentals Workshop 181
______________________________________________________________________________

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

nlp -d printer_name file_name print file on different printer

nlp -l list all printer names

nlp -l string list all printer names containing string

nlp -i printer_name list information about a printer

(Alert: At a customer site, the command would “lp” or “lpr”. “nlp” is a sas-ified version of the
print command )

Example

Send master.ps to default printer.

prompt>nlp master.ps Enter


request id is 001login047 (1 file)

Send master.ps to specific printer.

prompt>nlp -d chpljh13 master.ps Enter


request id is 002login047 (1 file)

Display list of all printers.

prompt>nlp -l Enter
a4245b21 ahplj11 ahplj12 ahplj13 ahplj21
ahplj22 ahplj31 ahplj32 ainpr1 alhplj1
.
.
.

Display list of all printer names containing "h21".

prompt>nlp -l h21 Enter


c3816h21 c4213h21 chpljh21 ctpxh21 czbrah21
182 UNIX Fundamentals Workshop
______________________________________________________________________________

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).

Process management commands include:

Command Action

ps -ef display all current processes (second column is PID)

jobs display all current jobs (more on this a little later)

kill PID terminate process (with signal 15 -SIGTERM, by default)

kill -9 PID terminate process (with signal 9 – SIGKILL) and bypass


any shutdown routine

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.

List all processes owned by sssims.

prompt>ps –ef | grep sssims Enter


sssims 17726 1427 0 17:25:42 pts/63 0:00 vi foo.txt
sssims 12914 12912 0 Mar 24 pts/45 0:00 -ksh
sssims 1427 1425 0 14:24:53 pts/63 0:01 -ksh
sssims 18507 1427 0 17:26:27 pts/63 0:00 grep sssims

Attempt to kill the PID associated with “vi foo.txt”:

prompt>kill 17726 Enter

Check to see if the process was indeed terminated by the kill command:

prompt>ps –ef | grep sssims Enter


sssims 17726 1427 0 17:25:42 pts/63 0:00 vi foo.txt
sssims 12914 12912 0 Mar 24 pts/45 0:00 -ksh
sssims 1427 1425 0 14:24:53 pts/63 0:01 -ksh
sssims 18507 1427 0 17:26:27 pts/63 0:00 grep sssims

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:

prompt>kill -9 17726 Enter


[1] + Killed vi foo.txt

A ps listing no longer shows the 17726 PID. It has successfully been terminated:

prompt>ps –ef | grep sssims Enter


sssims 12914 12912 0 Mar 24 pts/45 0:00 -ksh
sssims 1427 1425 0 14:24:53 pts/63 0:01 -ksh
sssims 18507 1427 0 17:26:27 pts/63 0:00 grep sssims
184 UNIX Fundamentals Workshop
______________________________________________________________________________

Running Jobs in the Background

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.

the job: ps –ef | grep sssims


process 1: ps –ef
process 2: grep sssims

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.

The & operator


The & operator is the shell‘s operator that allows you to run a process in the background. The parent
process does not wait for the child process(es) to terminate. Control will immediately be returned to
the shell.

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.

prompt>sleep 10 & Enter


[3] 16656
prompt>ps Enter
PID TTY CMD
16656 pts/63 sleep
1427 pts/63 ksh

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>nohup sas –sysin start_shr5.sas –log start_shr5.log & Enter


[2] 1787
Sending output to nohup.out
prompt>

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

fg brings job to foreground

bg moves job to background

suspend suspends a process

Control-Z suspends the current foreground job

jobs lists active jobs (running and suspended) of current user

kill kills a given job

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>nohup sas –sysin start_shr5.sas & Enter


[2] 1787
Sending output to nohup.out

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.

To terminate Job 1, issue the following command:

prompt> kill %1 Enter


[1] + Terminated find /usr/local -name a.out –print &
188 UNIX Fundamentals Workshop
______________________________________________________________________________

Displaying Top Processes

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.

Note: To exit the program, press q.


UNIX Fundamentals Workshop 189
______________________________________________________________________________

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:

sleep 2000; echo longsleep done

4. Make the file executable with the chmod command.

5. Issue the following commands at your shell prompt:

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.

8. Consider the following output from the jobs command:

[2] Running find / -name foo –print


[3] Stopped vi $HOME/.profile

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
______________________________________________________________________________

You might also like