Chapter 9 Study Guide: Difference between revisions

From ITCwiki
Jump to navigation Jump to search
(Created page with " '''Scheduling commands in Linux.''' at daemon (atd) – used to schedule a command to be executed one time in the future. cron daemon (crond) – used to schedule a command...")
 
No edit summary
 
(24 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''LINUX PROCESSES'''


:*User process: a process begun by a user and which runs on a terminal.
:*Daemons process: a system process and it’s not associated with a terminal.
:*Process ID (PID): a unique identifier assigned to every process as it begins and it allows the kernel to identify it uniquely.
:*Parent process ID (PPID): a process that has started other processes (child processes)
'''VIEWING PROCESS COMMANDS'''
:*ps display a list of processes in the current shell
:*ps –e displays all processes running on terminals as well as processes that do not run on a terminal (daemons)
:*ps –f displays a full list of information about each process, including the UID, PID, PPID, CPU utilization, start time, terminal, processor time, and command name
:*ps –l displays a long list of information about each process, including the flag, state, UID, PID, PPID, CPU utilization, priority, nice value, address, size, WCHAN, terminal, and command name
:*ps –a displays all processes running on terminals
:*ps –x displays all processes that do not run on terminals
:*ps –ef display an entire list of processes across all terminals and including daemons
:*ps –ef | grep bash display only the BASH shells
:*ps ax | head -11 display all processes across terminals, and the x option, which lists processes that do not run on a terminal
:*ls /proc display all process information subdirectories
:*pstree | head -20 displays the lineage of a process
:*top display an interactive screen listing processes
:*h to get help
'''KILLING PROCESSES COMMANDS'''
:*kill send to kill signals
:*kill –l displays different kill signal names
Example:[root@server1 ~]# kill -l
1)SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5)SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9)SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13)SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD
18)SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN
22)SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26)SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO
30)SIGPWR 31) SIGSYS 33) SIGRTMIN 34) SIGRTMIN+1
35)SIGRTMIN+2 36) SIGRTMIN+3         37) SIGRTMIN+4  38) SIGRTMIN+5
39)SIGRTMIN+6 40) SIGRTMIN+7         41) SIGRTMIN+8         42) SIGRTMIN+9
43)SIGRTMIN+10         44) SIGRTMIN+11 45) SIGRTMIN+12 46) SIGRTMIN+13
47)SIGRTMIN+14         48) SIGRTMIN+15 49) SIGRTMAX-15 50) SIGRTMAX-14
51)SIGRTMAX-13         52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55)SIGRTMAX-9         56) SIGRTMAX-8         57) SIGRTMAX-7         58) SIGRTMAX-6
59)SIGRTMAX-5         60) SIGRTMAX-4         61) SIGRTMAX-3         62) SIGRTMAX-2
63)SIGRTMAX-1         64) SIGRTMA
:*kill -3 9999 (3 = SIGQUIT, 9999 = PID) use to send a kill signal to a process
:*killall -3 SAMPLE (3=SIGQUIT, SAMPLE=process name) use to send a kill signal to process




'''Scheduling commands in Linux.'''  
'''Scheduling commands in Linux.'''  
at daemon (atd) – used to schedule a command to be executed one time in the future.  
at daemon (atd) – used to schedule a command to be executed one time in the future.  


Line 14: Line 64:
Schedule a job.  
Schedule a job.  


at (timespec)  
at (timespec) - Example: at noon or at 12:01am July 1


                 at>date
                 at>date
                 at>time
                 at>time
                 CTRL-Z to end.  
                 CTRL-Z to end.  


Line 25: Line 73:


Display a list of AT jobs:
Display a list of AT jobs:
at –l
:*at –l


Display variables for Job ID:  
Display variables for Job ID:  
at –c 1
:*at –c 1


Delete a AT job:
Delete a AT job:
at –d 1
:*at –d 1


To process a script:
To process a script:
at 12:01am July 4 –f /home/user1/Happy4th.sh
:*at 12:01am July 4 –f /home/user1/Happy4th.sh
 


Limiting access to the at daemon:
Limiting access to the at daemon:
 
:*Access to scheduling at jobs can be limited with the /etc/at.allow and /etc/at.deny files.  
§  Access to scheduling at jobs can be limited with the /etc/at.allow and /etc/at.deny files.  
:*If the files do not exist, only root can schedule tasks with the at daemon.   
ú  If the files do not exist, only root can schedule tasks with the at daemon.   
:*To allow users, create the /etc/at.allow file and add the username, one per line.  
ú  To allow users, create the /etc/at.allow file and add the username, one per line.  
:*To deny users, create the /etc/at.deny file. Anyone not in the file is then allowed to use the at daemon.
ú  To deny users, create the /etc/at.deny file. Anyone not in the file is then allowed to use the at daemon.
Note: if both files exist, the system only processes the /etc/at.allow file and ignores /etc/at.deny.  
Note: if both files exist, the system only processes the /etc/at.allow file and ignores /etc/at.deny.  


Line 50: Line 96:
The table is separated into 6 fields by a space or tab. The first five field specify the times to run the command and the sixth field is the absolute path to the command to be run.  
The table is separated into 6 fields by a space or tab. The first five field specify the times to run the command and the sixth field is the absolute path to the command to be run.  
   
   
Access to scheduling at jobs can be limited with the /etc/cron.allow and /etc/cron.deny files.  
Access to scheduling at jobs can be limited with the /etc/cron.allow and /etc/cron.deny files.  
§  On a newly installed Fedora system, all users have the ability to schedule tasks with cron daemon as the /etc/cron.deny contains only a single blank line.  
:*On a newly installed Fedora system, all users have the ability to schedule tasks with cron daemon as the /etc/cron.deny contains only a single blank line.  
ú  To allow users, create the /etc/cron.allow file and add the username, one per line. This would then limit the system compared to a clean install to only those users.  
:*To allow users, create the /etc/cron.allow file and add the username, one per line. This would then limit the system compared to a clean install to only those users.  
ú  To deny users, create the /etc/cron.deny file. Anyone not in the file is then allowed to use the at daemon.  
:*To deny users, create the /etc/cron.deny file. Anyone not in the file is then allowed to use the at daemon.  
Note: if both files exist, the system only processes the /etc/cron.allow file and ignores /etc/cron.deny.  
Note: if both files exist, the system only processes the /etc/cron.allow file and ignores /etc/cron.deny.  
   
   


To create or edit a user cron table use the –e option. The editor for crontab entries is the vi editor
To create or edit a user cron table use the –e option. The editor for crontab entries is the vi editor
crontab –e  
:*crontab –e  
Note: The crontab information is stored in the /var/spool/cron/username directory. Username is the name of the user that executed the crontab –e command.  
Note: The crontab information is stored in the /var/spool/cron/username directory. Username is the name of the user that executed the crontab –e command.  


Line 67: Line 111:


To list the contents of your user cron table use the –l option.  
To list the contents of your user cron table use the –l option.  
§  crontab –l
:*crontab –l


To remove a cron table and all the scheduled jobs you can use the –r option.  
To remove a cron table and all the scheduled jobs you can use the –r option.  
§  crontab –r
:*crontab –r
 
Note: all the previous commands are specific for the user running them.  
Note: all the previous commands are specific for the user running them.  
Note: root can list, edit or remove other users cron tables by using the -u option with the username.  
Note: root can list, edit or remove other users cron tables by using the -u option with the username.  
Line 86: Line 129:
# |  |  |  |  |
# |  |  |  |  |
# *  *  *  *  *  command to be executed
# *  *  *  *  *  command to be executed
:*The fields are separated by a space and * indicates a wild card for all time periods.
:*If you want more than one consecutive day, you can specify the day range with a –(dash).
:*If you want more than one time period non-consecutive you can separate them by a comma.
:*If you want to specify a particular time period, every 10 min, you can specify the *(every minute) /10.


§ The fields are separated by a space and * indicates a wild card for all time periods.
Administrative tasks that are performed on an hourly, daily, weekly or monthly basis can be scheduled simply by placing the shell script in the following directories.
§  If you want more than one consecutive day, you can specify the day range with a –(dash).
:*Scripts to be executed hourly - /etc/cron.hourly directory. 
§  If you want more than one time period non-consecutive you can separate them by a comma.  
:*Scripts to be executed daily  - /etc/cron.daily directory.
§ If you want to specify a particular time period, every 10 min, you can specify the *(every minute) /10.
:*Scripts to be executed weekly - /etc/cron.weekly directory.
:*Scripts to be executed monthly - /etc/cron.monthly directory.
The cron daemon runs the /etc/cron.d/Øhourly script which executes the files in the directory /etc/cron.hourly one minute past the hour, every hour.
For troubleshooting purposes: The /etc/cron.hourly/Øanacron file starts the anacron daemon which then executes the contents of the /etc/cron.daily, /etc/cron.weekly, and the /etc/cron.monthly directories at the times specified in the /etc/anacrontab file.
 
 
 
 
'''Process Execution'''
Types of commands:
Binary programs
*examples of binary programs:
*ls, find, grep, jre-1_5_0-linux-i586.bin
 
Shell scripts
*Example: scripts created in editor, made executable
 
Binary programs
*Example:Executable file created in a text editor
 
'''Shell functions'''
:*Examples:cd, exit, wait
:*A shell function is a compound command that has been given a name. It stores a series of commands for later execution. The name becomes a command in its own right and can be used in the same way as any other command. Its arguments are available in the positional parameters, just as in any other script. Like other commands, it sets a return code.
 
 
:*A function is executed in the same process as the script that calls it. This makes it fast, because no new process has to be created. All the variables of the script are available to it without having to be exported, and when a function changes those variables, the changes will be seen by the calling script. That said, you can make variables local to the function so that they do not affect the calling script; the choice is yours.
 
 
:*Not only do functions encapsulate code for reuse in a single script, but they can make it available to other scripts. They make top-down design easy and improve legibility. They break scripts into manageable chunks that can be tested and debugged separately.
 
 
:*At the command line, functions can do things that an external script cannot, such as change directories. They are much more flexible and powerful than aliases, which simply replace the command you type with a different command. Chapter 11 presents a number of functions that make working at the prompt more productive.
 
 
'''Running Processes in the Background''' (Processes ran in background eliminate the wait function from the shell)
'''How Programs Executed'''
:*Step 1: From the working terminal using the shell creates a sub shell this process is forking, then executes a wait function
:*Step 2:Subshell executes the program/script shell is unavailable to the user while executing (foreground process)
 
:*Step 3:Program/script completes , shell calls its exit function to kill the subshell and return control to original shell
 
'''To determine if file is binary:'''
:*Use “file” command to determine if file is binary
:*View contents using “od” or “strings” (ch 3)
 
 
'''To run a process in the background (freeing up the shell for additional input append “&” to command'''
 
 
'''Terminating Background Processes'''
:*1. issue 'jobs' command to locate job num ber
:*2. issue 'kill -2 %[job number]
:*** example: assume your killing job #2
:*** kill -2 %2
 
 
'''To run jobs in the background prefixed job with an & character'''
:*Example: jre-1_5_0-linux-i586.bin &
 
'''Moving Processes Between Background and Foreground'''
:* Step 1: Pause foreground processes with Ctrl+z
:* Step 2: locate job number
:* Step 3: issue fg command with job number
:** Example: fg %1
 
'''Move foreground process to the background using the 'bg' command'''
:* Step 1: Pause foreground processes with Ctrl+z
:* Step 2: locate job number
:* Step 3: issue bg command with job command
:** Example: bg %1
 
:*Summary: Send processes to foreground with ‘fg’
 
 
'''Move background process to the foreground using the 'fg' command'''
:* Step 1: locate job number with the job command
:* Step 2: Pause processes with Ctrl+z
:* Step 3: issue bg command with job number
:** Example: fg %1
:*Summary: Send processes to background with ‘bg’
 
 
'''Use ‘jobs’ to all jobs running in the background'''
 
'''Process Priorities'''
 
:*Time Slice – amount of time available to a process
:* More time slices a process has, the more CPU it has, therefore the faster it executes
 
:*PRI (process priority) ranges:
:** 0 = highest
:** 127 lowest
 
'''Adjust priorities with ‘nice’'''
:* Users can lower their priority
:** only the root user has the ability to increase the priority
:* Example: nice -n 19 ps -l
:* Processes start out with nice value of 0 by default
 
'''Changing priority of a running process using ‘renice’'''
:*“renice +[nice value] [PID]


§  Administrative tasks that are performed on an hourly, daily, weekly or monthly basis can be scheduled simply by placing the shell script in the following directories.
'''Adjusting Priority''
ú  Scripts to be executed hourly - /etc/cron.hourly directory. 
:* Use ‘top’ to change nice value, press r ,the enter the PID
ú  Scripts to be executed daily  - /etc/cron.daily directory.
:* Root can ‘renice’ a user or a group’s processes
ú Scripts to be executed weekly - /etc/cron.weekly directory.
:* renice [+/-] [priority] –u [user]
ú  Scripts to be executed monthly - /etc/cron.monthly directory.
:* renice [+/-] [priority] –g [group]
§  The cron daemon runs the /etc/cron.d/Øhourly script which executes the files in the directory /etc/cron.hourly one minute past the hour, every hour.
§  For troubleshooting purposes: The /etc/cron.hourly/Øanacron file starts the anacron daemon which then executes the contents of the /etc/cron.daily, /etc/cron.weekly, and the /etc/cron.monthly directories at the times specified in the /etc/anacrontab file.

Latest revision as of 00:52, 23 March 2012

LINUX PROCESSES

  • User process: a process begun by a user and which runs on a terminal.
  • Daemons process: a system process and it’s not associated with a terminal.
  • Process ID (PID): a unique identifier assigned to every process as it begins and it allows the kernel to identify it uniquely.
  • Parent process ID (PPID): a process that has started other processes (child processes)


VIEWING PROCESS COMMANDS

  • ps display a list of processes in the current shell
  • ps –e displays all processes running on terminals as well as processes that do not run on a terminal (daemons)
  • ps –f displays a full list of information about each process, including the UID, PID, PPID, CPU utilization, start time, terminal, processor time, and command name
  • ps –l displays a long list of information about each process, including the flag, state, UID, PID, PPID, CPU utilization, priority, nice value, address, size, WCHAN, terminal, and command name
  • ps –a displays all processes running on terminals
  • ps –x displays all processes that do not run on terminals
  • ps –ef display an entire list of processes across all terminals and including daemons
  • ps –ef | grep bash display only the BASH shells
  • ps ax | head -11 display all processes across terminals, and the x option, which lists processes that do not run on a terminal
  • ls /proc display all process information subdirectories
  • pstree | head -20 displays the lineage of a process
  • top display an interactive screen listing processes
  • h to get help


KILLING PROCESSES COMMANDS

  • kill send to kill signals
  • kill –l displays different kill signal names

Example:[root@server1 ~]# kill -l

1)SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5)SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9)SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13)SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD 18)SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22)SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26)SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30)SIGPWR 31) SIGSYS 33) SIGRTMIN 34) SIGRTMIN+1 35)SIGRTMIN+2 36) SIGRTMIN+3 37) SIGRTMIN+4 38) SIGRTMIN+5 39)SIGRTMIN+6 40) SIGRTMIN+7 41) SIGRTMIN+8 42) SIGRTMIN+9 43)SIGRTMIN+10 44) SIGRTMIN+11 45) SIGRTMIN+12 46) SIGRTMIN+13 47)SIGRTMIN+14 48) SIGRTMIN+15 49) SIGRTMAX-15 50) SIGRTMAX-14 51)SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55)SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59)SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63)SIGRTMAX-1 64) SIGRTMA

  • kill -3 9999 (3 = SIGQUIT, 9999 = PID) use to send a kill signal to a process
  • killall -3 SAMPLE (3=SIGQUIT, SAMPLE=process name) use to send a kill signal to process


Scheduling commands in Linux.

at daemon (atd) – used to schedule a command to be executed one time in the future.

cron daemon (crond) – used to schedule a command to be executed repeatedly in the future.


AT: The shells environment and scheduled commands are stored in the /var/spool/at directory.


Schedule a job.

at (timespec) - Example: at noon or at 12:01am July 1

               at>date
               at>time
               CTRL-Z to end. 


Display a list of AT jobs:

  • at –l

Display variables for Job ID:

  • at –c 1

Delete a AT job:

  • at –d 1

To process a script:

  • at 12:01am July 4 –f /home/user1/Happy4th.sh

Limiting access to the at daemon:

  • Access to scheduling at jobs can be limited with the /etc/at.allow and /etc/at.deny files.
  • If the files do not exist, only root can schedule tasks with the at daemon.
  • To allow users, create the /etc/at.allow file and add the username, one per line.
  • To deny users, create the /etc/at.deny file. Anyone not in the file is then allowed to use the at daemon.

Note: if both files exist, the system only processes the /etc/at.allow file and ignores /etc/at.deny.


CRON: The table is separated into 6 fields by a space or tab. The first five field specify the times to run the command and the sixth field is the absolute path to the command to be run.

Access to scheduling at jobs can be limited with the /etc/cron.allow and /etc/cron.deny files.

  • On a newly installed Fedora system, all users have the ability to schedule tasks with cron daemon as the /etc/cron.deny contains only a single blank line.
  • To allow users, create the /etc/cron.allow file and add the username, one per line. This would then limit the system compared to a clean install to only those users.
  • To deny users, create the /etc/cron.deny file. Anyone not in the file is then allowed to use the at daemon.

Note: if both files exist, the system only processes the /etc/cron.allow file and ignores /etc/cron.deny.


To create or edit a user cron table use the –e option. The editor for crontab entries is the vi editor

  • crontab –e

Note: The crontab information is stored in the /var/spool/cron/username directory. Username is the name of the user that executed the crontab –e command.

Examples: /var/spool/cron/dave or /var/spool/cron/root


To list the contents of your user cron table use the –l option.

  • crontab –l

To remove a cron table and all the scheduled jobs you can use the –r option.

  • crontab –r

Note: all the previous commands are specific for the user running them. Note: root can list, edit or remove other users cron tables by using the -u option with the username.

               Examples: crontab –l –u dave or crontab –r –u randy


  1. Example of job definition:
  2. .---------------- minute (0 - 59)
  3. | .------------- hour (0 - 23)
  4. | | .---------- day of month (1 - 31)
  5. | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
  6. | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
  7. | | | | |
  8. * * * * * command to be executed
  • The fields are separated by a space and * indicates a wild card for all time periods.
  • If you want more than one consecutive day, you can specify the day range with a –(dash).
  • If you want more than one time period non-consecutive you can separate them by a comma.
  • If you want to specify a particular time period, every 10 min, you can specify the *(every minute) /10.


Administrative tasks that are performed on an hourly, daily, weekly or monthly basis can be scheduled simply by placing the shell script in the following directories.

  • Scripts to be executed hourly - /etc/cron.hourly directory.
  • Scripts to be executed daily - /etc/cron.daily directory.
  • Scripts to be executed weekly - /etc/cron.weekly directory.
  • Scripts to be executed monthly - /etc/cron.monthly directory.

The cron daemon runs the /etc/cron.d/Øhourly script which executes the files in the directory /etc/cron.hourly one minute past the hour, every hour. For troubleshooting purposes: The /etc/cron.hourly/Øanacron file starts the anacron daemon which then executes the contents of the /etc/cron.daily, /etc/cron.weekly, and the /etc/cron.monthly directories at the times specified in the /etc/anacrontab file.



Process Execution Types of commands: Binary programs *examples of binary programs: *ls, find, grep, jre-1_5_0-linux-i586.bin

Shell scripts *Example: scripts created in editor, made executable

Binary programs *Example:Executable file created in a text editor

Shell functions :*Examples:cd, exit, wait

  • A shell function is a compound command that has been given a name. It stores a series of commands for later execution. The name becomes a command in its own right and can be used in the same way as any other command. Its arguments are available in the positional parameters, just as in any other script. Like other commands, it sets a return code.


  • A function is executed in the same process as the script that calls it. This makes it fast, because no new process has to be created. All the variables of the script are available to it without having to be exported, and when a function changes those variables, the changes will be seen by the calling script. That said, you can make variables local to the function so that they do not affect the calling script; the choice is yours.


  • Not only do functions encapsulate code for reuse in a single script, but they can make it available to other scripts. They make top-down design easy and improve legibility. They break scripts into manageable chunks that can be tested and debugged separately.


  • At the command line, functions can do things that an external script cannot, such as change directories. They are much more flexible and powerful than aliases, which simply replace the command you type with a different command. Chapter 11 presents a number of functions that make working at the prompt more productive.


Running Processes in the Background (Processes ran in background eliminate the wait function from the shell)

How Programs Executed

  • Step 1: From the working terminal using the shell creates a sub shell – this process is forking, then executes a wait function
  • Step 2:Subshell executes the program/script shell is unavailable to the user while executing (foreground process)
  • Step 3:Program/script completes , shell calls its exit function to kill the subshell and return control to original shell

To determine if file is binary:

  • Use “file” command to determine if file is binary
  • View contents using “od” or “strings” (ch 3)


To run a process in the background (freeing up the shell for additional input append “&” to command


Terminating Background Processes

  • 1. issue 'jobs' command to locate job num ber
  • 2. issue 'kill -2 %[job number]
      • example: assume your killing job #2
      • kill -2 %2


To run jobs in the background prefixed job with an & character

  • Example: jre-1_5_0-linux-i586.bin &

Moving Processes Between Background and Foreground

  • Step 1: Pause foreground processes with Ctrl+z
  • Step 2: locate job number
  • Step 3: issue fg command with job number
    • Example: fg %1


Move foreground process to the background using the 'bg' command

  • Step 1: Pause foreground processes with Ctrl+z
  • Step 2: locate job number
  • Step 3: issue bg command with job command
    • Example: bg %1
  • Summary: Send processes to foreground with ‘fg’


Move background process to the foreground using the 'fg' command

  • Step 1: locate job number with the job command
  • Step 2: Pause processes with Ctrl+z
  • Step 3: issue bg command with job number
    • Example: fg %1
  • Summary: Send processes to background with ‘bg’


Use ‘jobs’ to all jobs running in the background

Process Priorities

  • Time Slice – amount of time available to a process
  • More time slices a process has, the more CPU it has, therefore the faster it executes
  • PRI (process priority) ranges:
    • 0 = highest
    • 127 lowest

Adjust priorities with ‘nice’

  • Users can lower their priority
    • only the root user has the ability to increase the priority
  • Example: nice -n 19 ps -l
  • Processes start out with nice value of 0 by default

Changing priority of a running process using ‘renice’

  • “renice +[nice value] [PID]

'Adjusting Priority

  • Use ‘top’ to change nice value, press r ,the enter the PID
  • Root can ‘renice’ a user or a group’s processes
  • renice [+/-] [priority] –u [user]
  • renice [+/-] [priority] –g [group]