Linux Shell Learning Notes fifth Day _ Other

Source: Internet
Author: User

Day Fifth: Functions and task scheduling

Advantages of the function

Divide and conquer F

Work together

Easy Management

Easy Maintenance

Structure of the function

function functions name ()

{

Command 1

Command 2

Command 3

}

Parameter passing of a function

Passing arguments to a function is like using a special variable $1,$2,$3...$9 in a generic script, the function gets the parameters passed back to the shell script, so it's a good idea to reset the variables in the function to save the arguments. This way, if the function is a bit of a mistake, it can be tracked quickly by a localized variable name.

function file

When you use some of your phone's frequently used functions, you can put it into a function file and load the file into the shell.

The file header should contain the statement #!/bin/bash, the file name can be selected arbitrarily, but it is best to have some practical connection with the related task.

#!/bin/bash

#注释

Function1 ()

{

}

Example of a function file

Functions.main

#!/bin/bash

#functions. Main

Findit ()

{

If [$#-lt 1];then

echo "Usage:findit file"

Return 1

Fi

Find/-name $1–peint

}

function usage Examples

. Functions.main load function

Set to see if the function is loaded

Findit Call function

Findit Functions.main Call function

unset Findit Delete Findit function

Single Task scheduling

At is used to schedule a one-time task at a specified time.

Format:

At [Options] Time

-F reads a command or script from a file

-M email the user after the job completes

-V Display job time of execution

Service Start and stop

Service ATD Start

Service ATD Stop

Delete Task Atrm

One-time Task scheduling example

At–f mycrontest.sh 10:00pm tomorrow

At–f mycrontest.sh 2:00am Tuesday

At–f mycrontest.sh 2:00pm Feb 11

At–f mycrontest.sh 2:00pm next week

Cyclic scheduling crontab

Crontab can run some job tasks on a regular basis. It's a script that starts automatically every time Linux starts.

Format:

crontab [E [Username]|-l [Username]|-r [UserName]]

-E executes a text editor to set the time table

-l list text editors to set the time table

-R deletes the current schedule table

-V Lists the status of a user cron job

Crontab Configuration

Crontab can run some job tasks on a regular basis. It's a script that starts automatically every time Linux starts.

Global configuration file/etc/crontab

User Profile/var/spool/cron/

User Configuration for Crontab

/etc/cron.allow

/etc/cron.deny

/etc/crontab

Shell=/bin/bash

Path=/sbin;/bin:/usr/sbin;/usr/bin

Mailto=root

home=/

* * * * * Root run-parts/etc/cron.hourly

4*** Root run-parts/etc/cron.daily

4**0 Root run-parts/etc/cron.weekly

41** Root run-parts/etc/cron.monthly

Five fields, time-sharing and Lunar week

Crontab Application Scenario

Test every five minutes to connect to the gateway 192.168.1.254 Ping */5 * * * *

User Alex Backup the Samba profile every Sunday 12 o'clock noon

0 * * * 0 TAR–CZVF samba.tar.gz/etc/samba/smb.conf

Summary review

Advantages of the function

Work together

Easy inspection

Advanced flexibility

how tasks are scheduled

At

Crontab

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.