shell 构建脚本基础

时间:2023-03-08 20:10:45

bash -v test.sh 启用 verbose 调试模式

bash -n test.sh  启用语法检查调试模式

bash -x test.sh  遍历脚本执行过程

一、基础参数

1.shell 中()  {}   []  (())  [[]]

shell 中()  {}   []  (())

在 bash shell 中,$( ) 与` ` (反引号) 都可用做命令替换用。
${ }用于变量替换。一般情况下,$var 与${var} 并没有什么不一样,但是用 ${ } 会比较精确的界定变量名称的范围。
$[]和$(())是一样的,都是进行数学运算的。支持+ - * / %(“加、减、乘、除、取模”)。但是注意,bash只能作整数运算,对于浮点数是当作字符串处理的。
[[]] 双方括号命令提供了针对字符串比较的高级特性。

2.命令替换

#!/bin/bash
#命令输出负载给变量的两种方式
#反引号字符(`)
#$()格式
d1=`date`
d2=$(date) echo "The date d1 and time are: $d1"
echo "The date d2 and time are: $d2"

3.  >/dev/null 2>&1

    ls /etcfsdfdsfs   >/dev/null  >&  //将标准错误输出导入标准正确输出,然后将标准正确输出重定向到   /dev/null

----------------------------------------------------------------------------------------------------------------
>/dev/null >&
// 实际上,应该等同于这样: 1>/dev/null 2>/dev/null ,
默认情况下就是1,标准输出,所以一般都省略。 而&符号,
后面接的是必须的文件描述符。不能写成2>,
这样就成了标准错误重定向到文件名为1的文件中了,
而不是重定向标准错误到标准输出中。
所以这里就是:标准输出重定向到了/dev/null,而标准错误又重定向到了标准输出,
所以就成了标准输出和标准错误都重定向到了/dev/null >& >/dev/null //此方式为不正确方式
命令从左往右执行,>& 为将标准错误输出重定向到标准输出,而这时标准输出是打印在屏幕上的,还没执行到 >/dev/null
命令行的重定向什么的, 是在执行命令之前就准备好了的. 解释顺序从左至右依次进行, &> ,而1是屏幕, 所以标准错误重定向到屏幕, 再而 >/dev/null ,
即标准输出重定向到 /dev/null, 上述2>& >/dev/null 并不是什么同一时刻要么产生标准输出要么产生标准错误. 而是两个不同的东西.

4.数值计算

#在bash中,在将一个数学运算结果赋给某个变量时,可以用美元符和方括号($[ operation ])将数学表达式围起来
#!/bin/bash

var1=
var2= var3=$[$var1 * $var2]
echo "$var3"

浮点数计算

#variable=$(echo "options; expression" | bc) 

!/bin/bash

var1=$(echo "scale=2; 6.44/3/56"|bc)
#scale 保留小数点位数
echo $var1

5.退出脚本

#Linux提供了一个专门的变量$?来保存上个已执行命令的退出状态码。对于需要进行检查的命令,必须在其运行完毕后立刻查看或使用$?变量。它的值会变成由shell所执行的最后一条命令的退出状态码
状 态         码 描 述
命令成功结束
一般性未知错误
不适合的shell命令
命令不可执行
没找到命令
无效的退出参数
+x 与Linux信号x相关的严重错误
通过Ctrl+C终止的命令
正常范围之外的退出状态码
#!/bin/bash

var=$(echo "scale=3; 1000/3.3" |bc )

echo $var

exit 0

#echo $? 查看状态码
#exit 3 可以指定退出的状态码

二、使用结构化命令

1. if-then

#bash shell的if语句会运行if后面的那个命令。如果该命令的退出状态码是0(该命令成功运行),位于then部分的命令就会被执行。如果该命令的退出状态码是其他值,then部分的命令就不会被执行,bash shell会继续执行脚本中的下一个命令。fi语句用来表示if-then语句到此结束

if command
then
commands
fi
#!/bin/bash

#test multiple commands in the then section
#
#
testuser=zy
#
#then 可以直接跟在if 判断后面 ,也可以另起一行
if grep $testuser /etc/passwd; then
echo "this is my first command"
echo "this is my second command"
echo "$testuser is exist " fi

if-then-else 语句

#!/bin/bash

#test multiple commands in the then section
#
#
testuser=nouser
# if grep $testuser /etc/passwd; then
echo "this is my first command"
echo "this is my second command"
echo "$testuser is exist "
echo
else
echo "the user $testuser does not exist on the system"
echo
#echo 输出一个空行 fi

嵌套 if

#!/bin/bash

#test multiple commands in the then section
#
#
testuser=nouser
# if grep $testuser /etc/passwd; then
echo "this is my first command"
echo "this is my second command"
echo "$testuser is exist "
echo
else
echo "the user $testuser does not exist on the system"
echo
if ls -d /home/$testuser/
then
echo "however ,$testuser has a directory"
fi fi

elif 语句

#!/bin/bash

#test multiple commands in the then section
#
#
testuser=$
# if grep $testuser /etc/passwd; then
echo "this is my first command"
echo "this is my second command"
echo "$testuser is exist "
echo
elif ls -d /home/$testuser/ &>/dev/null;then
echo "the $testuser dose not exists on the system"
echo "however ,$testuser has a directory" else
echo "however ,$testuser dose not exist on the system"
echo "$testuser dose not have a directory" fi
---------------------------------------------------------------------------------------------------- [root@localhost shell_script]# ./test8.sh zy
zy:x:::IT_2:/home/zy_home:/bin/sh
this is my first command
this is my second command
zy is exist [root@localhost shell_script]# ./test8.sh nouser
the nouser dose not exists on the system
however ,nouser has a directory
[root@localhost shell_script]# ./test8.sh aaa
however ,aaa dose not exist on the system
aaa dose not have a directory
[root@localhost shell_script]#
if command1
then
command set
elif command2
then
command set
elif command3
then
command set
elif command4
then
command set
fi

2.test 命令

test命令提供了在if-then语句中测试不同条件的途径。如果test命令中列出的条件成立,test命令就会退出并返回退出状态码0。这样if-then语句就与其他编程语言中的if-then语句以类似的方式工作了。如果条件不成立,test命令就会退出并返回非零的退出状态码,这使得if-then语句不会再被执行
if test condition
then
commands
fi
#如果不写test命令的condition部分,它会以非零的退出状态码退出,并执行else语句块
#!/bin/bash
#
#
#
var=$ if test $var
then
echo " condition " else
echo "no condition" fi
#bash shell提供了另一种条件测试方法,无需在if-then语句中声明test命令。
if [ condition ]
then
commands
fi #方括号定义了测试条件。注意,第一个方括号之后和第二个方括号之前必须加上一个空格,
否则就会报错。
test命令可以判断三类条件:
 数值比较
 字符串比较
 文件比较
比 较             描 述
n1 -eq n2 检查n1是否与n2相等
n1 -ge n2 检查n1是否大于或等于n2
n1 -gt n2 检查n1是否大于n2
n1 -le n2 检查n1是否小于或等于n2
n1 -lt n2 检查n1是否小于n2
n1 -ne n2 检查n1是否不等于n2
#!/bin/bash
#
#Using numeric test evaluations val1=$
val2=$
#
if [ $val1 -gt ];then
echo "$val1 gt 10"
fi if [ $val1 -eq $val2 ];then
echo "$val1 eq $val2" fi
字符串比较测试
比 较 描 述
str1 = str2 检查str1是否和str2相同
str1 != str2 检查str1是否和str2不同
str1 < str2 检查str1是否比str2小
str1 > str2 检查str1是否比str2大
-n str1 检查str1的长度是否非0
-z str1 检查str1的长度是否为0
[root@localhost shell_script]# cat test11.sh
#!/bin/bash #
# var1=$
var2=$ if [ $var1 \> $var2 ];then
#字符串比较大于或者小于需要用 \ 进行转义,否则会当成文件的重定向
echo "$var1 is greater than $var2"
else
echo "$var1 is less than $var2" fi
[root@localhost shell_script]# ./test11.sh aaa cccccc
aaa is less than cccccc
[root@localhost shell_script]#
test命令的文件比较功能
比 较 描 述
-d file 检查file是否存在并是一个目录
-e file 检查file是否存在
-f file 检查file是否存在并是一个文件
-r file 检查file是否存在并可读
-s file 检查file是否存在并非空
-w file 检查file是否存在并可写
-x file 检查file是否存在并可执行
-O file 检查file是否存在并属当前用户所有
-G file 检查file是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧
[root@localhost shell_script]# cat test12.sh
#!/bin/bash #Look before you leap var1=$ if [ -d $var1 ];then
echo "the $var1 directory exist"
cd $var1
ls -a
else
echo "the $var1 directory dose not exists" fi
[root@localhost shell_script]# ./test12.sh /home
the /home directory exist
. .. nouser shell_script tom zhengyue zy_home
[root@localhost shell_script]#

复合条件测试

if-then语句允许你使用布尔逻辑来组合测试。有两种布尔运算符可用:
 [ condition1 ] && [ condition2 ]
 [ condition1 ] || [ condition2 ]
[root@localhost shell_script]# cat test13.sh
#!/bin/bash #
# if [ -d $HOME ] && [ -w $HOME/testing ];then
echo "the file exists and you can write to it"
elif [ -d $HOME ] || [ -w $HONE/testing ];then
echo "the file 2 || 1"
else
echo "I cannot write to the file" fi
[root@localhost shell_script]# ./test13.sh
the file exists and you can write to it
[root@localhost shell_script]# ./test13.sh
the file ||
[root@localhost shell_script]#

if-then 的高级特性

bash shell提供了两项可在if-then语句中使用的高级特性:
 用于数学表达式的双括号
 用于高级字符串处理功能的双方括号
双括号命令符号 (())
符 号 描 述
val++ 后增
val-- 后减
++val 先增
--val 先减
! 逻辑求反
~ 位求反
** 幂运算
<< 左位移
>> 右位移
& 位布尔和
| 位布尔或
&& 逻辑和
|| 逻辑或
[root@localhost shell_script]# cat test14.sh
#!/bin/bash
i=
b=$(( i++ ))
c=$(( ++i )) echo $b
echo $c
[root@localhost shell_script]# ./test14.sh

双方括号命令提供了针对字符串比较的高级特性。双方括号命令的格式如下:
[[ expression ]]

[root@localhost shell_script]# cat test15.sh
#!/bin/bash #
#using pattern match if [[ $USER == r* ]];then
echo "Hello $USER"
else
echo "sorry , I do not know you"
fi
[root@localhost shell_script]# ./test15.sh
Hello root
[root@localhost shell_script]#

case 语句

case 字符串 in
模式)
语句
;;
模式2 | 模式3)
语句
;;
*)
默认执行的 语句
;;
esac
#!/bin/bash
#
case $ in
[-])
echo "$1 IS number"
;;
[A-Z])
echo "$1 is character"
;;
*)
echo "$1 is error character"
;;
esac
[root@localhost shell_script]# cat test16.sh
#!/bin/bash case $ in
[-])
echo "$1 is mumber"
;;
[A-Z])
echo "$1 is character"
;;
*)
echo "$1 is error character"
;;
esac
[root@localhost shell_script]# ./test16.sh
is mumber
[root@localhost shell_script]# ./test16.sh A
A is character
[root@localhost shell_script]# ./test16.sh CC
CC is error character
[root@localhost shell_script]#
只接受start ,stop ,restart ,status

#!/bin/bash
#
case $ in
'start')
echo "start server ..."
;;
'stop')
echo "stop server ..."
;;
'restart')
echo "restart server ..."
;;
'status')
echo "running ..."
;;
*)
echo "`basename $0` {start|stop|restart|status}"
;;
esac
##注意多分支判断每个判断结束都要在后面加上;;

for 命令

for var in list
do
commands
done
root@localhost shell_script]# cat test17.sh
#!/bin/bash
#basic for command for test in A B C D E F ;do
echo "The char : $test" done [root@localhost shell_script]# ./test17.sh
The char : A
The char : B
The char : C
The char : D
The char : E
The char : F
[root@localhost shell_script]#
[root@localhost shell_script]# cat test18.sh
#!/bin/bash #
# list='A v bg ng jn df ttt'
list=$list' cc'
#列表附加内容
for I in $list;do
echo "$I"
done
[root@localhost shell_script]# ./test18.sh
A
v
bg
ng
jn
df
ttt
cc
[root@localhost shell_script]
root@localhost shell_script]# cat test19.sh
#!/bin/bash
#
#
for file in /root/* ;do
if [ -d "$file" ];then
echo "The $file is a directory"
elif [ -f "$file" ];then
echo "the $file is a file"
fi
done
[root@localhost shell_script]# ./test19.sh
the /root/1111.cap is a file
The /root/123 is a directory
the /root/222.cap is a file
the /root/anaconda-ks.cfg is a file
the /root/cc is a file
the /root/Joomla_3.9.3-Stable-Full_Package.zip is a file
the /root/ping.out is a file
the /root/qwe.cap is a file
the /root/tr is a file
the /root/:x is a file
[root@localhost shell_script]#

C 语言的 for命令

for (( variable assignment ; condition ; iteration process ))

[root@localhost shell_script]# cat test20.sh
#!/bin/bash # for (( i=; i<; i++ ));do
echo "The next number is :$i" done
[root@localhost shell_script]# ./test20.sh
The next number is :
The next number is :
The next number is :
The next number is :
[root@localhost shell_script]#

while 命令

while命令的格式是:
while test command
do
other commands
done
#!/bin/bash
#
#
var1= while echo $var1
[ $var1 -ge ];do
echo "This is inside the loop"
var1=$[ $var1 - ]
done

until 命令

until命令和while命令工作的方式完全相反。until命令要求你指定一个通常返回非零退
出状态码的测试命令。只有测试命令的退出状态码不为0,bash shell才会执行循环中列出的命令。
一旦测试命令返回了退出状态码0,循环就结束了。
until test commands
do other commands
done
root@localhost shell_script]# cat test22.sh
#!/bin/bash # var1= until echo $var1
[ $var1 -eq ];do
echo "inside the loop:$var1"
var1=$[ $var1 - ] done
[root@localhost shell_script]# ./test22.sh inside the loop: inside the loop: inside the loop: inside the loop:

嵌套循环

#注意,在使用嵌套循环时,你是在迭代中使用迭代,与命令运行的次数是乘积关系。
[root@localhost shell_script]# cat test23.sh
#!/bin/bash #
for (( a= ;a<=;a++ ));do
echo "starting loop $a"
for ((b=;b<=;b++ ));do
echo "Inside loop:$b"
done
done
[root@localhost shell_script]# ./test23.sh
starting loop
Inside loop:
Inside loop:
Inside loop:
starting loop
Inside loop:
Inside loop:
Inside loop:
starting loop
Inside loop:
Inside loop:
Inside loop:
[root@localhost shell_script]#

break 语句

break n :指定要跳出的循环层级

[root@localhost shell_script]# cat test24.sh
#!/bin/bash
#
#
#breaking out of a while loop var1= while [ $var1 -lt ];do
if [ $var1 -eq ];then
break
fi
echo "Iteration: $var1"
var1=$[ $var1 + ]
done echo "The while loop is complated" [root@localhost shell_script]# ./test24.sh
Iteration:
Iteration:
Iteration:
Iteration:
The while loop is complated
[root@localhost shell_script]#

continue 命令

continue命令可以提前中止某次循环中的命令,但并不会完全终止整个循环。

root@localhost shell_script]# cat test25.sh
#!/bin/bash # for (( i=;i<;i++ ));do
if [ $i -gt ] && [ $i -lt ];then
continue
fi
echo "Iteration : $i"
done
[root@localhost shell_script]# ./test25.sh
Iteration :
Iteration :
Iteration :
Iteration :
Iteration :
Iteration :
Iteration :
[root@localhost shell_script]#
[root@localhost shell_script]# cat test26.sh
#!/bin/bash
# piping a loop to another command
for state in "North Dakota" Connecticut Illinois Alabama Tennessee
do
echo "$state is the next place to go"
done | sort
#输出结果交给sort 进行排序然后进行输出
[root@localhost shell_script]# ./test26.sh
Alabama is the next place to go
Connecticut is the next place to go
Illinois is the next place to go
North Dakota is the next place to go
Tennessee is the next place to go
[root@localhost shell_script]#

实例:

[root@localhost shell_script]# cat test27.sh
#!/bin/bash IFS=:
#指定分隔符为: for folder in $PATH ;do
echo "path : $folder"
#for 循环遍历path 路径
for file in $folder/*
do
#for 循环遍历path 路径下的文件,判断是否是可执行文件
if [ -x $file ];then
echo "-x :$file"
fi
done
done
for 命令允许你遍历一系列的值

while 命令使用普通命令或者测试命令提供了基于条件命令的循环

until 命令也提供了迭代命令的一中方法,但是取非0 的状态进行匹配
#阶乘
[root@localhost shell_script]# cat test29.sh
#!/bin/bash factorial= for (( number = ;number <= $; number++ ));do
factorial=$[ $factorial * $number ]
done echo "The factorial of $1 is $factorial"
[root@localhost shell_script]# ./test29.sh
The factorial of is
[root@localhost shell_script]#

传递9以后的参数

root@localhost shell_script]# cat test30.sh
#!/bin/bash echo $ echo ${}
echo ${}

basename 的作用:返回不包含路径的脚本名

[root@localhost shell_script]# cat addem
#!/bin/bash
#testing a multi-function script name=$(basename $0) if [ $name = "addem" ];then total=$[ $ + $ ] elif [ $name = "multem" ];then total=$[ $ * $ ] fi echo "$total" [root@localhost shell_script]# ./addem [root@localhost shell_script]# mv addem multem
[root@localhost shell_script]# ./multem [root@localhost shell_script]#

参数统计:$#

root@localhost shell_script]# cat test32.sh
#!/bin/bash # $# 能够获取参数的个数
#getting the number of parameters echo there were $# parameters supplied. exit [root@localhost shell_script]# ./test32.sh df h s g h
there were parameters supplied.

关于参数统计的脚本:

 ${$#} 在脚本中会出现异常,需要改成 ${!#}

[root@localhost shell_script]# cat test33.sh
#!/bin/bash
#Testing parameters
# if [ $# -ne 2 ];then
echo
echo Usage: test.sh a b
echo
else
total=$[ $ + $ ]
echo
echo the total is $total
echo
fi
——————————————————————————————————————————————————————————————————————————
[root@localhost shell_script]# ./test33.sh Usage: test.sh a b [root@localhost shell_script]# ./test33.sh the total is

$* 将给定的多个参数定义为一个整体保存,类似"A B C"

$@ 将给定的多个参数定义为同一字符串的多个单独的单词,类似  "A" "B"  "C"

--------------------------------------

shift 移动变量

在使用shift命令时,默认情况下它会将每个参数变量向左移动一个位置。所以,变量$3的值会移到$2中,变量$2的值会移到$1中,而变量$1的值则会被删除(注意,变量$0的值,

也就是程序名,不会改变)

[root@localhost shell_script]# cat test34.sh
#!/bin/bash
#demonstrating the shift command count= while [ -n "$1" ];do
# "$1" 需要加"" 否则无法进行匹配
echo
echo "The parameters# $count: $1"
echo
count=$[ $count + ]
shift
done
[root@localhost shell_script]# ./test34.sh A b The parameters# : A The parameters# : b The parameters# : The parameters# : The parameters# : [root@localhost shell_script]#

处理选项:查找选项

Found the -c option
[root@localhost shell_script]# cat test35.sh
#!/bin/bash echo
while [ -n "$1" ];do case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
*) echo " "$" is not found option" ;;
esac
#shift 将每个变量向左移动一位
shift
done [root@localhost shell_script]# ./test35.sh -z -a -v -b -c -z is not found option
Found the -a option
-v is not found option
Found the -b option
Found the -c option
[root@localhost shell_script]#

分离参数和选项:

[root@localhost shell_script]# cat test36.sh
#!/bin/bash
# extracting options and parameters echo while [ -n "$1" ];do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option" ;;
esac shift
done # count= for param in $@;do
echo "Parameter #$count: $param "
count=$[ $count + ]
done [root@localhost shell_script]#
[root@localhost shell_script]# ./test36.sh -a -c test ss -- Found the -a option
Found the -c option
test is not an option
ss is not an option
Parameter #:
Parameter #:
Parameter #:
[root@localhost shell_script]#

处理带值的选项:

[root@localhost shell_script]#  cat test37.sh
#!/bin/bash
# extracting options and parameters echo while [ -n "$1" ];do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option"
echo
echo "Found the param :$2 "
shift ;;
--) shift
break ;;
*) echo "$1 is not an option" ;;
esac shift
done # count= for param in $@;do
echo "Parameter #$count: $param "
count=$[ $count + ]
done [root@localhost shell_script]# ./test37.sh -a -c test ss -- Found the -a option
Found the -c option Found the param :test
ss is not an option
Parameter #:
Parameter #:
Parameter #:
[root@localhost shell_script]#

getopt 命令:

获得用户输入: read 从键盘获得用户输入

read -t 5 -p "your name: " name

-t  指定超时时间

[root@localhost shell_script]# ./test38.sh
your name: ww
hi ww
your name: ee
ee
[root@localhost shell_script]# cat test38.sh
#!/bin/bash
#testing the read command
# echo -n "your name: "
read name
echo "hi $name" read -p "your name: " name
echo "$name"
[root@localhost shell_script]# ./test38.sh
your name: ff
hi ff
your name: gg
gg
[root@localhost shell_script]#
[root@localhost shell_script]# cat test39.sh
#!/bin/bash
#getting just one character of input
# read -n1 -p "Do you want to contiune [Y/N]? " answer
case $answer in
Y|y) echo
echo "fine, continue on...";;
N|n) echo
echo "OK goodbye"
exit;;
esac
echo "this is the end of the script" [root@localhost shell_script]# ./test39.sh
Do you want to contiune [Y/N]? n
OK goodbye
[root@localhost shell_script]#
#本例中将-n选项和值1一起使用,告诉read命令在接受单个字符后退出。只要按下单个字符回答后,read命令就会接受输入并将它传给变量,无需按回车键。 # -s选项可以避免在read命令中输入的数据出现在显示器上(实际上,数据会被显示,只是read命令会将文本颜色设成跟背景色一样)。
 
#!/bin/bash
# reading data from a file
#
count=
cat test | while read line
do
echo "Line $count: $line"
count=$[ $count + ]
done
echo "Finished processing the file"
#从文件读取内容
root@localhost shell_script]# cat test41.sh
#!/bin/bash
exec >testout
#exec 永久重定向
echo "ff"
echo "aa"
echo "bb" [root@localhost shell_script]# cat testout
ff
aa
bb
[root@localhost shell_script]#

done < ${1} :$1 代表第一个命令行参数

控制脚本

 SIGHUP      挂起进程
SIGINT 终止进程
SIGQUIT 停止进程
SIGKILL 无条件终止进程
SIGTERM 尽可能终止进程
SIGSTOP 无条件停止进程,但不是终止进程
SIGTSTP 停止或暂停进程,但不终止进程
SIGCONT 继续运行停止的进程

trap 捕获信号

root@localhost shell_script]# cat test42.sh
#!/bin/bash
#Testing signal trapping
# trap "echo 'Sorry I have trap Ctrl-C'" SIGINT echo "This is a test script" count=
while [ $count -le ];do
echo "loop #$count"
sleep
count=$[ $count + ]
done echo "END"
[root@localhost shell_script]# ./test42.sh
This is a test script
loop #
loop #
^CSorry I have trap Ctrl-C
loop #
^CSorry I have trap Ctrl-C
loop #
^CSorry I have trap Ctrl-C
loop #
loop #
loop #
loop #
loop #
loop #
END
[root@localhost shell_script]#

后台运行脚本

./test1.sh &   #后台运行脚本,终端关闭后停止
nohup ./test1.sh & #后台运行脚本,终端关闭后任然执行

nice 命令:

nice -n  ./test4.sh > test4.out &

定时任务执行

    .在未来某个时间点执行一次
at
batch at 时间
at > COMMAND
at > crtl +d :提交
指定时间:
绝对时间: HH:MM DD:MM:YY MM/DD/YY
相对时间: now+#
单位: minutes ,hours ,days ,weeks
模糊时间:noon ,midnight ,teatime
命令的执行结果将以邮件的形式发送给安排任务的用户
at -l :显示作业
at -d :删除作业
at -c ;显示执行的内容 .周期性执行
cron :crontab 自身是一个不间断执行的程序 anacron: cron 的补充。能够实现cron 没执行的动作 cron:
系统cron 任务
/etc/crontab
分钟 小时 天 月 周 用户 任务
用户cron 任务
/var/spool/cron/USERNAME 时间统配符:*
*:对应所有有效取值
* * * * * :每分钟执行一次
* * * * :表示每周每月每天每小时的第三分钟执行一次
* * * * :每个星期天的每小时的第三分钟执行一次 * * :每周五12 点13 分执行一次
* :每年7月6号12点13 分执行一次 ,:离散时间点
, * * * * :每小时的第10 分和第40 分执行一次 -:连续时间点
* * - :每周一到周五的2 点 分执行一次 /:对应取值范围内每多久执行一次
*/ * * * * :每三分钟执行一次 * */ * * * :每隔2 小时执行60 次, 因为每分钟为* 每分钟都会执行
*/ * * * :每隔 2小时的第一分钟执行一次 执行结果将以邮件方式发送给管理员 */ * * * * cat /etc/fstab > /dev/null :每3 分钟执行一次,并且将正确输出重定向,错误内容邮箱发送 cron 环境变量在PATH 查找
在脚本中 export 定义环境变量:
service crond status :查看crond 服务运行状态 crontab -l :查看定时任务
crontab -e : 编辑, 注意使用crontab -e 编辑,直接/etc/crontab 不行
crontab -r : 移除所有任务
crontab -u :指定以哪个用户来运行