URL.biz - where people find experts

 
HOME ARTICLES LIST NOW FOR FREE! ABOUT US CONTACT US LOG IN

Unix Fundamentals Module 5 System Utilities
5th in series of Unix Fundamentals course modules by JayrConsulting Ltd. A FREE copy of this module in MsWord format (including missing diagrams) can be got from john.roberts@jayrconsulting.co.uk

Go to Web Site

UNIX Fundamentals

MODULE 5 – System Utilities



You will cover

* Monitoring Processes

* Stopping Processes

* Queuing Jobs with ‘at' and ‘crontabs'

þ System Shutdown

þ Running background processes





























































































MODULE 5 – CONTENTS

5 SYSTEM UTILITIES 4

5.1 MONITORING PROCESSES 4

5.1.2 UNIX PROCESS HANDLING 6

5.1.3 RUNNING PROCESSES WITH EXEC 6

5.1.4 DYNAMIC MONITORING WITH ‘TOP' COMMAND 7

5.2 STOPPING PROCESSES 8

5.3 QUEUING JOBS 9

5.3.1 ONE TIME OPERATION USING ‘AT' COMMAND 9

5.3.2 QUEUING REPETITIVE JOBS WITH ‘CRONTAB' 10

5.3.3 EXAMPLE CRONTAB ENTRY 10

5.3.4 OTHER OPTIONS WITH ‘CRONTAB' 11

5.3.5 ALTERING EXISTING CRONTAB FILES 12

5.4 RUNNING BACKGROUND PROCESSES 13

5.4.1 STOPPING BACKGROUND PROCESSES 13

5.4.2 KEEPING PROCESSES RUNNING WITH NOHUP 13

5.4.3 STOPPING NOHUP PROCESSES 13

5.5 SYSTEM SHUTDOWN 14





5 System Utilities



There are many system utilities on UNIX systems. Some of the main ones will be highlighted in this module. For further information users should refer to the UNIX ‘man' pages.



5.1 Monitoring Processes



The ‘ps' command is used to return information on currently active processes. By default, the users own processes will be listed. Other options allow listing of another user's processes, processes associated with a particular terminal or all processes running on the system. ( Typical SysV commands are shown below )



ps –u eric lists all processes associated with user ‘eric'





ps –t ttyp5 lists all processes associated with terminal ttyp5





ps –ef full list of all processes running on the system





















UID PID PPID C STIME TTY TIME COMMAND

root 0 0 0 Sep 8 ? 28:04 swapper

root 1 0 0 Sep 8 ? 1:05 init

h24623r 652 519 0 10:02:36 ttyq3 0:00 vi bil_meta5.txt

root 959 1 0 Sep 8 ? 0:20 /usr/sbin/cron

root 1385 987 0 Sep 8 ? 2:21 memlogd

ingres 29699 1 0 Sep 11 ? 0:0 /ingres1/BB/ingres

f9083r 27427 27426 0 09:39:04 ttyp8 0:00 –ksh



UID User identity of process originator



PID Process id number of this process



PPID Process id of the parent process that spawned this process

C CPU utilisation time



STIME Time (or date) process was started



TTY Terminal running the process (? = ‘daemon' or detached process .)



TIME Cumulative execution time for process



5.1.2 UNIX Process handling



All processes that are running on a UNIX system are uniquely numbered with a process identifier number (PID).When commands are entered at the command prompt, the system spawns sub-processes in memory in order to run those programs. The originating process (Parent process) is put into a ‘wait' state while the sub process is running. (Child process). When the child process terminates, it sends a signal to the parent process to reactivate it.





















5.1.3 Running processes with exec



It is possible to start a new process but NOT open a sub shell to run it in, by using the command ‘exec' in front of the process name that is entered.



exec program_name



In this case, the new process overwrites what was the parent process in memory, so that the original parent process no longer exists. This mechanism is commonly used in user's ‘.profile' files to launch them into an application. It means that when the user exits the application, there is no ‘shell' to go back to, and therefore the user is automatically logged off from the system.



5.1.4 Dynamic monitoring with ‘top' command



The display shown by ‘ps' is an instant snapshot of the system and therefore is only true at the moment it is executed. A dynamic display of the most CPU intensive processes can be obtained on most UNIX systems by running the ‘top' command.





The dynamic display from the ‘top' command is stopped, when required, by using ‘q'.

Note that this facility is NOT available on all types of system.



5.2 Stopping Processes



Using the ‘kill' command can stop UNIX processes . By default, this sends a signal 15 (Termination) to the process. Users may terminate their own processes and ‘superuser' can terminate any process.



kill 2789 sends ‘terminate'(signal 15) to process id 2789



Note: Users should always attempt this method of killing processes first, as the ‘terminate' signal gives a process time to ‘tidy up' before terminating. Processes should be killed in reverse order, i.e. sub processes should be stopped before their parent processes

Some processes, such as shells, may have a ‘trap' set such that they will ignore a signal 15. In this case, the process would then have to be killed with a signal 9 (Kill) as follows.



kill –9 23789 sends ‘kill'(signal 9) to process id 23789



Other signals that UNIX recognises can be displayed using the following command.

kill –l



1) HUP 13) PIPE 25) TSTP

2) INT 14) ALRM 26) CONT

3) QUIT 15) TERM 27) TTIN

4) ILL 16) USR1 28) TTOU

5) TRAP 17) USR2 29) URG

6) IOT 18) CHLD 30) LOST

7) EMT 19) PWR 31) bad trap

8) FPE 20) VTALRM 32) bad trap

9) KILL 21) PROF 33) XCPU

10) BUS 22) POLL 34) XFSZ

11) SEGV 23) WINCH

12) SYS 24) STOP



5.3 Queuing jobs



5.3.1 One time operation using ‘at' command



Jobs can be queued on a UNIX system using the ‘at' command. This will run the queued job once only, at the designated time. On completion, the job will be removed from the queue. The basic syntax is as follows:



at time [date] command command





warning: commands will be executed using /usr/bin/sh

job 908529480.a at Fri Oct 16 10:18:00 1998



If the output from the command list has not been redirected, the default is to send it to the originator's mail file, when the job is executed.



at –l shows jobs that are currently queued



at –r jobnumber removes designated job from the queue





Note: On most systems, the use of the ‘at' command is restricted to only those users whose user id names are listed in a file /var/adm/cron/at.allow OR whose names are NOT listed in the file /var/adm/cron/at.deny







5.3.2 Queuing repetitive jobs with ‘crontab'



Executing a job at a given time on a regular basis, is done with the ‘crontab' command. To create a crontab for the first time, type in:



crontab

min hr day_of_month month day_of_week command





DO NOT DO THIS IF YOU ARE LOGGED ON AS ROOT !!!!!!



5.3.3 Example crontab entry



When crontabs have been created they are held in a directory /var/spool/cron/crontabs, with one crontab file for each user id that has created crontabs.





# Remove unwanted core files - those that aren't kernel or developers



10 22 * * * /var/adm/tools/bin/decore



#(would run ‘decore' at 22:10 every day )



# Ensure that wtmp files do not become over large.



00 23 * * 0,3 /usr/bin/cp /dev/null /var/adm/wtmp



#( at 23:00 every Sunday and Wednesday, overwrite ‘wtmp' file with ‘null' file, which has the effect of clearing it. )









5.3.4 Other options with ‘crontab'



crontab –l will list contents of your current crontab file



crontab –r will remove ALL entries from your current crontab file.



DO NOT DO THIS IF YOU ARE LOGGED ON AS ROOT !!





Note: On most systems, the use of the ‘crontab' command is restricted to only those users whose user id names are listed in a file /var/adm/cron/cron.allow OR whose names are NOT listed in the file /var/adm/cron/cron.deny





crontab –e allows direct editing of crontab file, using ‘vi' . This option should only be used if you are confident in the use of the ‘vi' editor .





















5.3.5 Altering existing crontab files



If a user already has a crontab file in existence, great care should be taken to make sure that it is not inadvertently overwritten or destroyed. The following is a suggested method of altering existing crontab files, assuming in this case that the user is logged on as ‘root'.



cd /var/spool/cron/crontabs move to correct directory



cp root root.old keep a copy in case of disaster!



cp root root.new make a copy to use for editing



Edit your changes into the file called ‘root.new'. When you are happy with the changed file, type:



crontab < root.new overwrites the ‘root' crontab with the contents of the file ‘root.new' and runs crontab utility.



This means that your original crontab file still exists as ‘root.old' in case you need to revert to this any time in the future.

Note that when you run a program via a crontab, your current environment is not remembered by ‘cron'. It may be necessary therefore to redefine variables etc within the program that is to be run.



NOTE: If you are using the crontab command and do something wrong, DO NOT exit with a , as your existing crontab may be overwritten. Use the ‘interrupt character' instead. (Normally )





5.4 Running background processes



Any command that will take a long time to complete will tie up the users terminal. It is possible to run commands as ‘background' processes, so that the process will run on it's own and the users terminal will be free for other activity, while waiting for the first process to complete. This is achieved by adding an ampersand to the end of the command line, before submitting the command.



find /usr –size +1000c –print > logfile &



The shell will display the process id of the background process, then return to the command prompt.



5.4.1 Stopping background processes



A background process will stop when the process terminates naturally or if the background process id is ‘killed'. The background process can be killed by the originator ( or root ) using the ‘kill' command, or it will be killed if the originator logs off.



5.4.2 Keeping processes running with nohup



There may be occasions when a user wishes to leave a process running, even after they have logged off from the machine. This is done by submitting a background process as before, but this time the command line is started with the command ‘nohup' ( no hang up )



nohup program_name &



5.4.3 Stopping nohup processes



As nohup processes will keep running when the originating user logs off, the process will only stop when it terminates normally or if the system is stopped. This is because when the user logs off, the parenthood of the nohup process is reassigned to a system process, called ‘init' which has a process id of ‘1' The only other way for the nohup process to be stopped is for it to be ‘killed'



5.5 System Shutdown



UNIX systems should always be closed down in a controlled manner, to help prevent corruption of data.

1. Get all users to log off

2. Close any applications where possible

3. Shutdown any databases that are running

4. Shutdown the UNIX operating system. (From the system console)



shutdown –h 60 would take the system down to a halt state, starting in 60 seconds from now.










 
Other Articles Written By This User


Copyright © 2003 - 2012 URL.biz. All rights reserved.