Day 5: function and Task Scheduling
Function advantages
Divide and conquer f
Collaboration
Convenient Management
Easy to maintain
Function Structure
Function Name ()
{
Command 1
Command 2
Command 3
}
Function parameter transfer
Passing parameters to a function is like using special variables $1, $2, $3… in a general script... $9 is the same. The function gets the passed parameters and returns the original parameters to the shell script. Therefore, it is best to reset the variable in the function to save the passed parameters. In this way, if the function has a slight error, you can quickly track it through the localized variable name.
Function File
When you use some frequently-used functions on your mobile phone, you can put them into the function file and load the file into shell.
The file header should contain statements #! /Bin/bash: the file name can be selected at will, but it is best to have some practical relationship with related tasks.
#! /Bin/bash
# Note
Function1 ()
{
}
Function file example
Functions. main
#! /Bin/bash
# Functions. main
Findit ()
{
If [$ #-lt 1]; then
Echo "usage: findit file"
Return 1
Fi
Find/-name $1-peint
}
Function example
. Functions. main load function
Set to check whether the function is loaded
Findit call Function
Findit functions. main calls the Function
Unset findit Delete findit Function
Single Task Scheduling
At is used to schedule a one-time task at a specified time.
Format:
At [Option] time
-F Read commands or scripts from files
-M sends an email to the user after the job is completed.
-V: display the job execution time
Start and Stop a service
Service atd start
Service atd stop
Delete task atrm
Example of one-time Task Scheduling
At-f mycrontest. sh 10: 00 pm tomorrow
At-f mycrontest. sh 2: 00 am Tues
At-f mycrontest. sh 2: 00 pm Feb
At-f mycrontest. sh 2: 00 pm next we
Cyclic Scheduling crontab
Crontab can regularly run some job tasks. It is a script that is automatically started every time linux is started.
Format:
Crontab [-e [UserName] |-l [UserName] |-r [UserName]
-E. Execute the text editor to set the time table.
-L list text editors to set time tables
-R: Delete the current time table.
-V: list the statuses of user cron jobs
Crontab Configuration
Crontab can regularly run some job tasks. It is a script that is automatically started every time linux is started.
Global configuration file/etc/crontab
User configuration file/var/spool/cron/
Crontab User Configuration
/Etc/cron. allow
/Etc/cron. deny
/Etc/crontab
SHELL =/bin/bash
PATH =/sbin;/bin:/usr/sbin;/usr/bin
MAILTO = root
HOME =/
01 *** root run-parts/etc/cron. hourly
02 4 *** root run-parts/etc/cron. daily
22 4 ** 0 root run-parts/etc/cron. weekly
42 41 ** root run-parts/etc/cron. monthly
Five fields: hour, day, month, week
Crontab Application Scenario
Test whether to connect to the gateway 192.168.1.254 every five minutes */5 ****
User alex backs up samba configuration files at every Sunday.
0 12 ** 0 tar-czvf samba.tar.gz/etc/samba/smb. conf
Summary
Function advantages
Collaboration
Easy to check
Advanced and flexible
Task Scheduling Method
At
Crontab