Linux学习笔记(第五课)

复习shell+流程控制语句+计划任务

实用的干货,要多练习

 

4.3 流程控制语句
if.......then.........
[root@localhost mnt]# vim mkcdrom.sh
DIR="/media/cdrom"
if [ ! -d $DIR ]
then
mkdir -p $DIR
fi
[root@localhost mnt]# bash mkcdrom.sh
[root@localhost mnt]# ls -ld /media/cdrom/
drwxr-xr-x. 2 root root 6 Apr 13 16:02 /media/cdrom/
[root@localhost mnt]# date
Tue Apr 13 16:03:06 EDT 2021
[root@localhost mnt]#


[root@localhost mnt]# vim chkhost.sh
#!/bin/bash
ping -c 3 -i 0.2 -W 3 $1 &>/mnt/bing
if [ $? -eq 0 ]
then
echo "host $1 is on-line"
else
echo "host $1 is off-line"
fi
[root@localhost mnt]# bash chkhost.sh 127.0.0.1
host 127.0.0.1 is on-line
[root@localhost mnt]# bash chkhost.sh 192.168.242.99
host 192.168.242.99 is on-line
[root@localhost mnt]# bash chkhost.sh 192.168.242.98
host 192.168.242.98 is off-line
[root@localhost mnt]# cat bing
PING 192.168.242.98 (192.168.242.98) 56(84) bytes of data.
From 192.168.242.99 icmp_seq=1 Destination Host Unreachable
From 192.168.242.99 icmp_seq=2 Destination Host Unreachable
From 192.168.242.99 icmp_seq=3 Destination Host Unreachable

--- 192.168.242.98 ping statistics ---
3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 413ms
pipe 3
[root@localhost mnt]#

 

for
---------
[root@localhost mnt]# vim user.txt
zhangsan
lisi
wanger
mazi
chenwu
zhaoliu
[root@localhost mnt]# vim adduser.sh
#!bin/bash
read -p "enter the user pass:" PASSWD
for UNAME in ` cat user.text`
do
id $UNAME &> /dev/null
if [ $? -eq 0 ]
then
echo "$UNAME , Already exists"
else
useradd $UNAME &> /dev/null
echo "$UNAME &> /dev/null"
echo "$PASSWD" | passwd --stdin $UNAME &> /dev/null
echo "$UNAME , Create success"
fi
done
[root@localhost mnt]# bash adduser.sh
enter the user pass:123456
zhangsan &> /dev/null
zhangsan , Create success
lisi &> /dev/null
lisi , Create success
wanger &> /dev/null
wanger , Create success
mazi &> /dev/null
mazi , Create success
chenwu &> /dev/null
chenwu , Create success
zhaoliu &> /dev/null
zhaoliu , Create success
[root@localhost mnt]# vim adduser.sh
[root@localhost mnt]# tail -6 /etc/passwd
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
wanger:x:1003:1003::/home/wanger:/bin/bash
mazi:x:1004:1004::/home/mazi:/bin/bash
chenwu:x:1005:1005::/home/chenwu:/bin/bash
zhaoliu:x:1006:1006::/home/zhaoliu:/bin/bash

[root@localhost mnt]# vim ipaddrs.txt
192.168.242.99
192.168.242.98
192.168.242.97
127.0.0.1
[root@localhost mnt]# vim checkhost.sh
#!bin/bash
HLIST=$(cat /mnt/ipaddrs.txt)
for IP in $HLIST
do
ping -c 3 -i 0.2 -W 3 $IP &>/mnt/bing
if [ $? -eq 0 ]
then
echo "host $1 is on-line"
else
echo "host $1 is off-line"
fi
done
[root@localhost mnt]# bash checkhost.sh
host is on-line
host is off-line
host is off-line
host is on-line
[root@localhost mnt]#


which
case
---------------------------------------
计划任务服务程序

一次性计划任务
-------------------
[root@localhost mnt]# at 23:30
warning: commands will be executed using /bin/sh
at> systemctl restart httpd
at> <EOT>此处请同时按下<Ctrl>+<d>键来结束编写计划任务
job 1 at Tue Apr 13 23:30:00 2021
[root@localhost mnt]# at -l
1 Tue Apr 13 23:30:00 2021 a root
[root@localhost mnt]# at 23:50
warning: commands will be executed using /bin/sh
at> systemctl restart httpd
at> <EOT>此处请同时按下<Ctrl>+<d>键来结束编写计划任务
job 2 at Tue Apr 13 23:50:00 2021
[root@localhost mnt]# at -l
1 Tue Apr 13 23:30:00 2021 a root
2 Tue Apr 13 23:50:00 2021 a root

上面设置了两条一样的计划任务,可以使用atrm命令轻松删除其中一个
[root@localhost mnt]# atrm 2
[root@localhost mnt]# at -l
1 Tue Apr 13 23:30:00 2021 a root
[root@localhost mnt]#

 

 


长期性计划任务
--------------------------
root权限下

[root@bing ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

# ll /etc/crontab
-rw-r--r--. 1 root root 451 Dec 28 2013 /etc/crontab

每隔一天,早上01:40分备份/etc/fstab文件到/backup/下

[root@server0 ~]# touch backupfstab.sh
[root@server0 ~]# chmod +x backupfstab.sh

[root@server0 ~]# cat backupfstab.sh
#!/bin/sh
##############################
#author:
#date:
#version:
#desc:
#file : /root/bak/check.sh
###############################
cp /etc/fstab /backup/fstab.$(date +%Y%m%d%H%M%S)


让它每隔1天1:40执行,在/etc/crontab最后加上如下行:
40 1 */2 * * root /root/backupfstab.sh

[root@bing backup]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,s
at
# | | | | |
# * * * * * user-name command to be executed
*/1 * * * * root /backup/backupfstab.sh ---每分钟运行一次备份
*/2 * * * * root /backup/backupfstab.sh ---每隔一分钟运行一次备份
40 1 */2 * * root /root/backupfstab.sh ---每隔1天1:40运行一次备份


时间定义可以很灵活,参考
# man 5 crontab

-----------------------
普通用户 (注意文件夹权限问题)
[student@server0 ~]$ crontab -e
格式跟/etc/crontab一样,只是少了用户列
例如:student用户每分钟去更新家目录下的update.txt的时间戳
* * * * * touch /home/student/update.txt


禁止普通用户使用计划任务:
只需要把用户名写入/etc/cron.deny文件就可以了,每个用户名一行
$ crontab -e
You (student) are not allowed to use this program (crontab)
See crontab(1) for more information

 

普通用户的计划任务存放路径:他的计划任务以用户名命名的
[root@server0 ~]# ll /var/spool/cron/
total 4
-rw-------. 1 student student 41 Jul 5 21:41 student

去除普通用户的计划任务只要删除该文件就可以了
[root@server0 ~]# rm /var/spool/cron/student
rm: remove regular file ‘/var/spool/cron/student’? y

-----------------------------------------------------------------
查看命令详细路径:
[root@localhost mnt]# whereis reboot
reboot: /usr/sbin/reboot /usr/share/man/man2/reboot.2.gz /usr/share/man/man8/reboot.8.gz

让它在17点59执行重启,在/etc/crontab最后加上如下行
59 17 * * * root /usr/sbin/reboot
--------------------------------------------------------------------

posted @ 2021-04-13 22:03  bing2215  阅读(57)  评论(0编辑  收藏  举报