Explorar o código

crontab: add tail runtime log and change to checkbox

rufengsuixing %!s(int64=5) %!d(string=hai) anos
pai
achega
7974370be8
Modificáronse 2 ficheiros con 35 adicións e 40 borrados
  1. 10 8
      luasrc/model/cbi/AdGuardHome/base.lua
  2. 25 32
      root/etc/init.d/AdGuardHome

+ 10 - 8
luasrc/model/cbi/AdGuardHome/base.lua

@@ -244,14 +244,16 @@ else
 	return value
 end
 end
-----autoupdate
-o = s:option(Flag, "autoupdate", translate("Auto update core with crontab"))
-o.default = 0
-o.optional = true
-----cutquerylog
-o = s:option(Flag, "cutquerylog", translate("Auto tail querylog with crontab"))
-o.default = 0
-o.optional = true
+
+----Crontab
+o = s:option(MultiValue, "crontab", translate("Crontab task"))
+o:value("autoupdate","Auto update core")
+o:value("cutquerylog","Auto tail querylog")
+o:value("cutruntimelog","Auto tail runtime log")
+o.widget = "checkbox"
+o.default = nil
+o.optional=true
+
 ----downloadpath
 o = s:option(TextValue, "downloadlinks",translate("Download links for update"))
 o.optional = false

+ 25 - 32
root/etc/init.d/AdGuardHome

@@ -530,47 +530,40 @@ boot() {
 	fi
 }
 do_crontab(){
-crontab_updatecore
-crontab_cutquerylog
-}
-crontab_updatecore(){
-	config_get autoupdate $CONFIGURATION autoupdate "0"
-	local line=$(grep "/usr/share/AdGuardHome/update_core.sh" $CRON_FILE)
-	if [ -n "$line" -a "${line:0:1}" != "#" ]; then
-		if [ $autoupdate -eq 1 ]; then
-			return
-		else
-			sed -i '\,/usr/share/AdGuardHome/update_core.sh,'d $CRON_FILE
-			echo "#$line" >> $CRON_FILE
-			/etc/init.d/cron restart
-		fi
-	else
-		if [ $autoupdate -eq 1 ]; then
-			[ -z "$line" ] && line="#30 3 * * * /usr/share/AdGuardHome/update_core.sh 2>&1"
-			sed -i '\,/usr/share/AdGuardHome/update_core.sh,'d $CRON_FILE
-			echo "${line:1}" >> $CRON_FILE
-			/etc/init.d/cron restart
-		else
-			return
-		fi
+	config_get crontab $CONFIGURATION crontab ""
+	local findstr default enable
+	findstr="/usr/share/AdGuardHome/update_core.sh"
+	default="#30 3 * * * /usr/share/AdGuardHome/update_core.sh 2>&1"
+	[ "${crontab//autoupdate/}" == "$crontab" ] && enable=0 || enable=1
+	crontab_editor
+	
+	findstr="tail -n [0-9]* .*/data/querylog.json > .*/data/querylog.json"
+	default="#0 * * * * tail -n 2000 $workdir/data/querylog.json > $workdir/data/querylog.json"
+	[ "${crontab//cutquerylog/}" == "$crontab" ] && enable=0 || enable=1
+	crontab_editor
+	
+	config_get logfile $CONFIGURATION logfile ""
+	if [ "$logfile" != "syslog" -a "$logfile" != "" ]; then
+		findstr="tail -n [0-9]* $logfile > $logfile"
+		default="#30 3 * * * tail -n 2000 $logfile > $logfile"
+		[ "${crontab//cutruntimelog/}" == "$crontab" ] && enable=0 || enable=1
+		crontab_editor
 	fi
 }
-crontab_cutquerylog(){
-	config_get cutquerylog $CONFIGURATION cutquerylog "0"
-	config_get workdir $CONFIGURATION workdir "/usr/bin/AdGuardHome"
-	local line=$(grep "tail -n [0-9]* $workdir/data/querylog.json > $workdir/data/querylog.json" $CRON_FILE)
+crontab_editor(){
+	local line="$(grep "$findstr" $CRON_FILE)"
 	if [ -n "$line" -a "${line:0:1}" != "#" ]; then
-		if [ $cutquerylog -eq 1 ]; then
+		if [ $enable -eq 1 ]; then
 			return
 		else
-			sed -i "\,tail -n [0-9]* .*/data/querylog.json > .*/data/querylog.json,d" $CRON_FILE
+			sed -i "\,$findstr,d" $CRON_FILE
 			echo "#$line" >> $CRON_FILE
 			/etc/init.d/cron restart
 		fi
 	else
-		if [ $cutquerylog -eq 1 ]; then
-			[ -z "$line" ] && line="#0 * * * * tail -n 2000 $workdir/data/querylog.json > $workdir/data/querylog.json"
-			sed -i "\,tail -n [0-9]* .*/data/querylog.json > .*/data/querylog.json,d" $CRON_FILE
+		if [ $enable -eq 1 ]; then
+			[ -z "$line" ] && line="$default"
+			sed -i "\,$findstr,d" $CRON_FILE
 			echo "${line:1}" >> $CRON_FILE
 			/etc/init.d/cron restart
 		else