CSC115 COMPUTER SYSTEM ADMINISTRATION Process controlling Unit 8 Process Controlling UNIX protection scheme uses - users: identified UID - groups: identified GID Specified in the file : /etc/passwd. Commands to check the logged users - w: show who is logged in and what they are doing. Syntax: w [-husfV] [user] -h: don’t print the header -u: ignores the username while figuring out the current process and cpu times. -s: use the short format. -f: toggle printing the from (remote hostname) field. - -V: display version information who: show who is logged on who –a: all - last : show listing of last logged in users Process - Process is just a program in execution. Since Linux/UNIX is a multi-tasking operating system at any one time there can be tens, hundreds even thousands of processes running. - The first process is init with PID of 1, which would be started when UNIX begins. Process Identifier (PID): - For every process UNIX tracks information including process identifier (PID). - Every process has a unique PID. - real UID and GID: Belong to the user who ran the program. - effective UID and GID: Used to determine if process is "allowed to do things" - status of the process R – run-able, S - sleeping, D - uninterruptible sleep, T - stopped/traced, Z - zombie - process priority: -20 to 20, the lower the value the higher the priority - parent process: There are no children processes. 1 CSC115 COMPUTER SYSTEM ADMINISTRATION Create new process - fork would create a new process from the existing process. - The new process is the children process, and the old process is the parent process. The children process inherits all environment setting from its parent process. When the user logged on, the shell is the first children process of init, and all other processes are the children process of shell. Command to find out which processes are currently running: ps [option] --> Provides information of the processes which are currently running. [option] -u specify user -l long information -x all processes controlling other ttys(terminal) -a all tty --help on-line help --sort sort pids specify PID Example: $ps -->the format is PID TTY TIME CMD PID: Process ID TTY: the terminal process is running on Time: time process ran CMD: process name $ps –uax -->display complete information of all processes ran on the system Format: USER PID %CPU %MEM RSS TTY STAT START TIME COMMAND RSS: usage of memory in kb STAT: status of process R: Running S: Sleeping T: Stopped <: with the high priority W: have no fixed pages 2 CSC115 COMPUTER SYSTEM ADMINISTRATION # ps –l -->The field NI expresses the precedence of the processes. top Provides a full screen, updated view of the current processes. Format of output: PID USER PRI NI SIZE RSS SHARE STAT LIB %CPU %MEM TIME COMMAND example: $ top - q: stop top command. h: list function manual for top k PID: kill the process - u username: list all processes for the specified user. r PID: reset the precedence for process with the given PID. Two ways to create processes from the command line Foreground Normal approach, command line only comes back once it is finished. Example $ locate lib | less - Background Use the & character. Command line comes back straight away. Examples: $sleep 500 $sleep 500 & $locate lib |less& - nohup keep the processes alive and executive after the user logs out. Example: $ nohup ls –R & process signal - All works are finished by processes. - Only processes can send signals. Methods to do this include: using key strokes and the shell 3 CSC115 COMPUTER SYSTEM ADMINISTRATION CTRL-Z – suspend CTRL-C – interrupt or using the kill command(s) kill - sends a signal to a process killall - sends a signal to all processes running a command Example: $ kill 1022 -->kill process with PID as 1022 $ kill –9 1020 -->the powerful kill command $ kill –l -->display the kill signal. The default value is 15. Commands to change the priority $ nice -n command -n: -20 to 19.The bigger the number is, the lower priority the process is set. The default value is 10. Example: $ nice less /etc/passwd& $ps –l Ni presents the priority of nice F S UID PID PPID C PRI NI ADDR 100 S 2003 20295 20294 0 72 0 000 T 2003 20402 20295 0 74 10 - SZ WCHAN TTY TIME CMD 624 wait4 pts/0 00:00:00 bash 504 do_sig pts/0 00:00:00 less 000 R - 2003 20403 20295 0 [1]+ Stopped 75 0 773 - pts/0 00:00:00 ps nice less /etc/passwd $nice -5 less /etc/passwd & -->set the priority as 15(10+5) . Decrease the priority. $ nice --10 less /etc/passwd& -->set the priority as 0(-10+10). Only root can increase the priority of a process. $ ps –l NI presents the priority of nice $ renice n [-p PID] [-u user] [-g pgrp] -->reset the priority for the processes on the system. User can specify the PID, 4 CSC115 COMPUTER SYSTEM ADMINISTRATION user ID or group ID. Positive n means decrease priority, negative n means increase priority. Only root can increase the priority. Example: $ renice 5 –u user1 -->decrease the priority of all processes for user1 $ renice –5 1921 -->increase the priority of process 1921 at and batch at and batch read commands from standard input or a specified file which are to be executed at a later time Syntax: at [option] time -d n delete the job with the job number as n -f specify the file from which the command would read -l list the jobs scheduled -m when the job finished, send a email to the user time has the following format: format hh:mm hh:mm mouth day year now + NumberofHour midnight noon teatime example 17:30 or 1730 1730 nov 11 1999 Now + 2 hour 1600 Examples: 1. read form standard input(screen): at teatime <enter> 2. read from file job at 1600 –f job <enter> or at 1600 < job 3. display the scheduled jobs at –l or atq 4. delete the scheduled jobs at –d 5 or atrm 5 cron and crontab - They are daemon processes to execute scheduled commands. 5 CSC115 COMPUTER SYSTEM ADMINISTRATION - cron should be started from /etc/rc or /etc/rc.local. The scheduled jobs are maintained in crontab files for individual users. - cron check this file every minutes to see if there are any commands should be executed. Users can have their own crontab which would be saved under /var/spool/cron. - Syntax: crontab [options] [file] Options: -e -l -r edit crontab list crontab for the current user remove user’s crontab An example of crontab check disk /home on 1st and 15th each month 0 0 1, 15 * * fsck /home 30 6 * * 07 quota –a run “quota –a” at 6:30am each sunday Each line has five fields with the format of: minutes hours day month weekday (00-59) (0-24) (01-31) (01-12)(01-07) More examples: $ crontab –e edit crontab $ crontab –l check the crontab list $ crontab –r delete crontab Exercise: 1. What is "fork"? What is child process? What is parent process? 2. What is "init"? 3. What is daemon? 4. Execute "ps". Write down the processes and process ID that you see. 5. Execute "ps −aux". Write down some other peoples' processes, %CPU, process ID, and status. 6. Execute "ps −u st02". 7. Execute "locate lib | less &" Write down one of the processes the user st02 has. and then "ps" 6 quickly. What do you see? CSC115 COMPUTER SYSTEM ADMINISTRATION 8. "nohup ls −R /usr &" and then "logout". Then quickly login again. Now quickly execute "ps". Can you see "ls −R /usr" ? Now quickly execute "ps −x". Can you see "ls −R /usr" ? Now quickly execute "ps −aux | grep '%CPU' ; ps −aux | grep $USER". Can you see how much %CPU it uses? 9. "nice −5 ls −R /usr > file.list &" Now quickly execute "ps −l". Write down the priority (PRI) and the nice priority (NI) of "ls −R /usr" 10. Try to kill "nice −5 ls −R /usr > file.list &". Execute "ps" to find out the process ID of the above "ls −R" 11. What is "top"? How to kill process in "top"? 12. What is the purpose of “at”? Show it usage by an example. 13. Suppose there is a file called myfile contains the followings: 45 07 How to kill it ? * * 45 13 1,15 * 5,6,7 echo "Weekend Morning :-) " * echo "1st or 15th" Every Friday/Saturday/Sunday 07:45am send a message "Weekend Morning :-) " Every 1st or 15th of Month 13:45pm send a message "1st or 15th" How to let it automatically execute? 7