-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.sh
187 lines (184 loc) · 5.98 KB
/
cron.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#! /bin/bash
source $(cd $(dirname $0);pwd)/core.sh
show_menu() {
choice=$2
if [[ ! -n $2 ]]; then
echo "> 定时任务管理"
echo "------------------------"
echo
crontab -l
echo
echo "1. 自定义任务"
echo "2. 每日任务"
echo "3. 删除任务"
echo "------------------------"
echo "0. 返回主菜单"
echo "------------------------"
echo
if [[ -n $1 ]]; then
echo -e "${red}$1${plain}"
echo
fi
read -p "请输入选择: " choice
fi
case $choice in
1)
clear
sub_title="自定义任务\n------------------------"
echo -e $sub_title
while read -p "需要执行的命令: " newquest
do
goback $newquest "clear;show_menu" "show_menu \"\" 1"
if [[ ! -n $newquest ]]; then
warning "请输入需要执行的命令" "$sub_title"
continue
fi
break
done
sub_title="$sub_title\n需要执行的命令: $newquest"
clear && echo -e $sub_title
while read -p "执行时间点(分钟, 0-59): " minute
do
goback $minute "clear;show_menu" "show_menu \"\" 1"
if [[ ! -n $(echo "$minute" | gawk '/^(\*)(\/[1-9]([0-9]{1,2})?)?$|^([0-9]|[1-5][0-9])([\,]([0-9]|[1-5][0-9]))?$/{print $0}') && -n $minute ]]; then
warning "执行时间点(分钟)格式错误" "$sub_title"
continue
fi
break
done
sub_title="$sub_title\n执行时间点(分钟, 0-59): $minute"
clear && echo -e $sub_title
while read -p "执行时间点(小时, 0-23): " hour
do
goback $hour "clear;show_menu" "show_menu \"\" 1"
if [[ ! -n $(echo "$hour" | gawk '/^(\*)(\/[1-9]([0-9]{1,2})?)?$|^([0-9]|[1][0-9]|[2][0-3])([\-\,]{1}([0-9]|[1][0-9]|[2][0-3]))?(\/[1-9]([0-9]{1,2})?)?$/{print $0}') && -n $hour ]]; then
warning "执行时间点(小时)格式错误" "$sub_title"
continue
fi
break
done
sub_title="$sub_title\n执行时间点(小时, 0-23): $hour"
clear && echo -e $sub_title
while read -p "执行时间点(日, 1-31): " day
do
goback $day "clear;show_menu" "show_menu \"\" 1"
if [[ ! -n $(echo "$day" | gawk '/^(\*)(\/[1-9]([0-9]{1,2})?)?$|^([1-9]|[1-2][0-9]|3[0-1])([\-\,]{1}([1-9]|[1-2][0-9]|3[0-1]))?(\/[1-9]([0-9]{1,2})?)?$/{print $0}') && -n $day ]]; then
warning "执行时间点(日)格式错误" "$sub_title"
continue
fi
break
done
sub_title="$sub_title\n执行时间点(日, 1-31): $day"
clear && echo -e $sub_title
while read -p "执行时间点(月, 1-12): " month
do
goback $month "clear;show_menu" "show_menu \"\" 1"
if [[ ! -n $(echo "$month" | gawk '/^(\*)(\/[1-9]([0-9]{1,2})?)?$|^([1-9]|[1][0-2])([\-\,]{1}([1-9]|[1][0-2]))?(\/[1-9]([0-9]{1,2})?)?$/{print $0}') && -n $month ]]; then
warning "执行时间点(月)格式错误" "$sub_title"
continue
fi
break
done
sub_title="$sub_title\n执行时间点(月, 1-12): $month"
clear && echo -e $sub_title
while read -p "执行时间点(周, 0-6): " week
do
goback $week "clear;show_menu" "show_menu \"\" 1"
if [[ ! -n $(echo "$week" | gawk '/^(\*)(\/[1-9]([0-9]{1,2})?)?$|^([0-6])([\-\,]{1}([0-6]))?(\/[1-9]([0-9]{1,2})?)?$/{print $0}') && -n $week ]]; then
warning "执行时间点(周)格式错误" "$sub_title"
continue
fi
break
done
sub_title="$sub_title\n执行时间点(周, 0-6): $week"
clear && echo -e $sub_title
echo
if [[ ! -n $minute ]]; then minute="*"; fi
if [[ ! -n $hour ]]; then hour="*"; fi
if [[ ! -n $day ]]; then day="*"; fi
if [[ ! -n $month ]]; then month="*"; fi
if [[ ! -n $week ]]; then week="*"; fi
(crontab -l ; echo "$minute $hour $day $month $week $newquest") | crontab - > /dev/null 2>&1
echo -e "- ${yellow}自定义任务已添加${plain}"
echo
read -n1 -p "按任意键继续" key
clear
show_menu
;;
2)
clear
sub_title="每日任务\n------------------------"
echo -e $sub_title
while read -p "需要执行的命令: " newquest
do
goback $newquest "clear;show_menu" "show_menu \"\" 2"
if [[ ! -n $newquest ]]; then
warning "请输入需要执行的命令" "$sub_title"
continue
fi
break
done
sub_title="$sub_title\n需要执行的命令: $newquest"
clear && echo -e $sub_title
while read -p "执行时间点(格式 2:30): " time
do
goback $time "clear;show_menu" "show_menu \"\" 2"
if [[ ! -n $(echo "$time" | gawk '/^([0-9]|[1][0-9]|[2][0-3])(\:([0-5][0-9]))?$/{print $0}') && -n $time ]]; then
warning "执行时间点格式错误" "$sub_title"
continue
fi
break
done
sub_title="$sub_title\n执行时间点(格式 2:30): $time"
clear && echo -e $sub_title
if [[ ! -n $time ]]; then
(crontab -l ; echo "$(expr $RANDOM % 59 + 1) $(expr $RANDOM % 23 + 1) * * * $newquest") | crontab - > /dev/null 2>&1
elif [[ ! -n $(echo "$time" | awk -F ":" '{print $2}') ]]; then
(crontab -l ; echo "0 $time * * * $newquest") | crontab - > /dev/null 2>&1
else
minute=`echo "$time" | awk -F ":" '{print $2}'`
hour=`echo "$time" | awk -F ":" '{print $1}'`
(crontab -l ; echo "$(expr $minute + 0) $hour * * * $newquest") | crontab - > /dev/null 2>&1
fi
echo -e "- ${yellow}每日任务已添加${plain}"
echo
read -n1 -p "按任意键继续" key
clear
show_menu
;;
3)
clear
echo "删除任务"
echo "------------------------"
echo
crontab -l
echo
if [[ -n $3 ]]; then
echo -e "${red}$3${plain}"
echo
fi
while read -p "输入任务关键字: " kquest
do
goback $kquest "clear;show_menu"
if [[ ! -n $kquest ]]; then
show_menu "" 3 "请输入任务关键字"
continue
fi
break
done
echo
crontab -l | grep -v "$kquest" | crontab -
clear
show_menu
;;
0)
run_script start.sh
;;
*)
clear
show_menu "请输入正确的数字"
;;
esac
}
clear
show_menu