forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.func
More file actions
149 lines (118 loc) · 3.49 KB
/
cron.func
File metadata and controls
149 lines (118 loc) · 3.49 KB
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
get_next_cron_string() {
# Parsing config
curr_str=$(grep "JOB=" $V_USERS/$user/cron.conf|cut -f 2 -d \'|\
sort -n|tail -n1)
# Print result
echo "$((curr_str +1))"
}
is_cron_job_free() {
# Checking record id
check_job=$(grep "JOB='$job'" $V_USERS/$user/cron.conf)
if [ ! -z "$check_job" ]; then
echo "Error: job id exist"
log_event 'debug' "$E_EXISTS $V_EVENT"
exit $E_EXISTS
fi
}
sort_cron_jobs() {
# Defining conf
conf="$V_USERS/$user/cron.conf"
cat $conf |sort -n -k 2 -t \' >$conf.tmp
mv -f $conf.tmp $conf
}
sync_cron_jobs() {
conf="/var/spool/cron/$user"
# Deleting old data
rm -f $conf
# Checking reporting system
rep=$(grep 'REPORTS=' $V_USERS/$user/user.conf | cut -f 2 -d \')
if [ "$rep" = 'yes' ]; then
email=$(grep 'CONTACT=' $V_USERS/$user/user.conf | cut -f 2 -d \')
echo "MAILTO=$email" >$conf
fi
# Reading user cron.conf
while read line ; do
# Defining new delimeter
IFS=$'\n'
# Parsing key=value
for key in $(echo $line|sed -e "s/' /'\n/g"); do
eval ${key%%=*}="${key#*=}"
done
if [ "$SUSPEND" = 'no' ] ; then
# Adding line to system cron
echo "$MIN $HOUR $DAY $MONTH $WDAY $CMD" |\
sed -e "s/%quote%/'/g" -e "s/%dots%/:/g" >> $conf
fi
done <$V_USERS/$user/cron.conf
}
is_job_valid() {
result=$(grep "JOB='$job'" $V_USERS/$user/cron.conf)
if [ -z "$result" ]; then
echo "Error: job id not exists"
log_event 'debug' "$E_NOTEXIST $V_EVENT"
exit $E_NOTEXIST
fi
}
del_cron_job() {
str=$(grep -n "JOB='$job'" $V_USERS/$user/cron.conf|cut -f 1 -d :)
sed -i "$str d" $V_USERS/$user/cron.conf
}
is_job_suspended() {
# Parsing jobs
str=$(grep "JOB='$job'" $V_USERS/$user/cron.conf|grep "SUSPEND='yes'" )
# Checkng key
if [ ! -z "$str" ]; then
echo "Error: job suspended"
log_event 'debug' "$E_SUSPENDED $V_EVENT"
exit $E_SUSPENDED
fi
}
is_job_unsuspended() {
# Parsing jobs
str=$(grep "JOB='$job'" $V_USERS/$user/cron.conf|grep "SUSPEND='no'" )
# Checkng key
if [ ! -z "$str" ]; then
echo "Error: job unsuspended"
log_event 'debug' "$E_UNSUSPENDED $V_EVENT"
exit $E_UNSUSPENDED
fi
}
update_cron_job_value() {
key="$1"
value="$2"
# Defining conf
conf="$V_USERS/$user/cron.conf"
# Parsing conf
job_str=$(grep -n "JOB='$job'" $conf)
str_number=$(echo $job_str | cut -f 1 -d ':')
str=$(echo $job_str | cut -f 2 -d ':')
# Parsing key=value
IFS=$'\n'
for keys in $(echo $str|sed -e "s/' /'\n/g"); do
eval ${keys%%=*}=${keys#*=}
done
# Defining clean key
c_key=$(echo "${key//$/}")
eval old="${key}"
# Escaping slashes
old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
new=$(echo "$value" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
# Updating conf
sed -i "$str_number s/$c_key='${old//\*/\\*}'/$c_key='${new//\*/\\*}'/g" \
$conf
}
cron_clear_search() {
# Defining delimeter
IFS=$'\n'
# Reading file line by line
for line in $(grep $search_string $conf); do
# Defining new delimeter
IFS=$'\n'
# Parsing key=value
for key in $(echo $line|sed -e "s/' /'\n/g"); do
eval ${key%%=*}=${key#*=}
done
# Print result line
eval echo "$field"
done
}