Chapter 9 Study Guide: Difference between revisions

From ITCwiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 106: Line 106:
Types of commands:
Types of commands:
Binary programs
Binary programs
examples of binary programs:
*examples of binary programs:
ls, find, grep, jre-1_5_0-linux-i586.bin
*ls, find, grep, jre-1_5_0-linux-i586.bin


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


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


'''Shell functions'''
'''Shell functions'''
Examples:cd, exit, wait
:*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 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.




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.




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.


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'''
'''Running Processes in the Background''' (Processes ran in background eliminate the wait function from the shell)
Processes ran in background eliminate the wait function from the shell
'''How Programs Executed'''
'''How Programs Executed'''
Step 1:
:*Step 1:
From the working terminal using the shell creates a sub shell – this process is forking, then executes a wait function
From the working terminal using the shell creates a sub shell – this process is forking, then executes a wait function
Step 2:
:*Step 2:
Subshell executes the program/script
Subshell executes the program/script
shell is unavailable to the user while executing (foreground process)
shell is unavailable to the user while executing (foreground process)
Step 3:
:*Step 3:
Program/script completes , shell calls its exit function to kill the subshell and return control to original shell
Program/script completes , shell calls its exit function to kill the subshell and return control to original shell


'''To determine if file is binary:'''
'''To determine if file is binary:'''
Use “file” command to determine if file is binary
:*Use “file” command to determine if file is binary
View contents using “od” or “strings” (ch 3)
:*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'''
'''To run a process in the background (freeing up the shell for additional input append “&” to command'''


'''Terminating Background Processes'''
'''Terminating Background Processes'''
Line 161: Line 164:
Step 3: issue fg command with job number
Step 3: issue fg command with job number
Example: fg %1
Example: fg %1
'''Move foreground process to the background using the 'bg' command'''
'''Move foreground process to the background using the 'bg' command'''
Line 168: Line 172:
Example: bg %1
Example: bg %1
Summary: Send processes to foreground with ‘fg’
Summary: Send processes to foreground with ‘fg’


'''Move background process to the foreground using the 'fg' command'''
'''Move background process to the foreground using the 'fg' command'''
Line 176: Line 181:
Summary: Send processes to background with ‘bg’  
Summary: Send processes to background with ‘bg’  


'''Use ‘jobs’ to all jobs running in the background'''
'''Use ‘jobs’ to all jobs running in the background'''

Revision as of 23:38, 22 March 2012

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)

               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]