linux Shell学习笔记第五天
linux Shell学习笔记第五天
第五天 函数与任务调度
函数的优势
分而治之f
协同合作
方便管理
维护简单
函数的结构
function 函数名()
{
命令
命令
命令
}
函数的参数传递
向函数传递参数就像在一般脚本中使用特殊变量$ $ $ …$ 一样 函数取得所传参数后将原始参数传回shell脚本 因此最好先在函数内重新设置变量保存所传的参数 这样如果函数有一点错误 就可以通过已经本地化的变量名迅速加以跟踪
函数文件
当你手机一些经常使用的函数时 可以将之放入函数文件中并将文件载入shell
文件头应包含语句#!/bin/bash 文件名可任意选取 但最好与相关任务有某种实际联系
#!/bin/bash
#注释

function ()
{
}
函数文件示例
functions main
#!/bin/bash
#functions main
findit()
{
if [$# lt ];then
echo usage:findit file
return
fi
find / name $ –peint
}
函数使用示例
functions main 载入函数
set 查看是否载入函数
findit 调用函数
findit functions main 调用函数
unset findit 删除findit函数
单次任务调度
at用于在指定时间调度一次性的任务
格式
at [选项] time
f 从文件中读取命令或脚本
m在作业完成后 给用户发电子邮件
v 显示作业呗执行的时间
服务启动与停止
service atd start
service atd stop
删除任务 atrm
单次任务调度示例
at –f mycrontest sh : pm tomorrow
at –f mycrontest sh : am Tuesday
at –f mycrontest sh : pm Feb
at –f mycrontest sh : pm next week
循环调度crontab
crontab可以定期运行一些作业任务 它是一个脚本 每次linux启动时都会自动启动该脚本
格式
crontab [ e [UserName]| l [UserName]| r [UserName]]
e执行文字编辑器来设定时程表
l 列出文字编辑器来设定时程表
r删除目前的时程表
v列出用户cron作业的状态
crontab配置
crontab可以定期运行一些作业任务 它是一个脚本 每次linux启动时都会自动启动该脚本
全局配置文件 /etc/crontab
用户配置文件 /var/spool/cron/
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
*** root run parts /etc/cron daily
** root run parts /etc/cron weekly
** root run parts /etc/cron monthly
五个字段 分 时 日 月 星期
crontab应用场景
每五分钟测试与网关 是否连通 ping */ * * * *
用户alex每个周日中午 点备份samba的配置文件
* * tar –czvf samba tar gz /etc/samba/nf
总结回顾
函数的优势
协同合作
检查方便
高级灵活
任务调度的方式
at
lishixinzhi/Article/program/yxkf/201404/30419