|
@@ -0,0 +1,170 @@
|
|
|
+## 如何使用定时脚本
|
|
|
+1、将以下内容存储为 loop.sh, 然后移动到任意目录下
|
|
|
+```bash
|
|
|
+#!/bin/sh
|
|
|
+
|
|
|
+sleep_time=900; # 间隔时间
|
|
|
+file_max_size=100; # 日志文件的最大体积
|
|
|
+plist_path="/Library/LaunchDaemons/com.remove.defender.plist"; # plist文件路径
|
|
|
+
|
|
|
+Launch_daemons="
|
|
|
+<?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
|
|
+<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
|
|
|
+<plist version=\"1.0\">
|
|
|
+ <dict>
|
|
|
+ <key>Label</key>
|
|
|
+ <string>com.remove.defender</string>
|
|
|
+ <key>removeDefender</key>
|
|
|
+ <string>com.remove.defender</string>
|
|
|
+ <key>ProgramArguments</key>
|
|
|
+ <array>
|
|
|
+ <string>${PWD}/run.sh</string>
|
|
|
+ </array>
|
|
|
+
|
|
|
+ <key>LimitLoadToSessionType</key>
|
|
|
+ <array>
|
|
|
+ <string>Aqua</string>
|
|
|
+ <string>Background</string>
|
|
|
+ <string>LoginWindow</string>
|
|
|
+ <string>StandardIO</string>
|
|
|
+ <string>System</string>
|
|
|
+ </array>
|
|
|
+
|
|
|
+ <key>StartInterval</key>
|
|
|
+ <integer>${sleep_time}</integer>
|
|
|
+
|
|
|
+ <key>StandardOutPath</key>
|
|
|
+ <string>${PWD}/run.log</string>
|
|
|
+
|
|
|
+ <key>StandardErrorPath</key>
|
|
|
+ <string>${PWD}/run.err</string>
|
|
|
+ </dict>
|
|
|
+</plist>
|
|
|
+"
|
|
|
+
|
|
|
+run="
|
|
|
+#!/bin/sh\n
|
|
|
+sudo '/Library/Application Support/Microsoft/Defender/uninstall/uninstall'\n
|
|
|
+time=\$(TZ=UTC-8 date +%Y-%m-%d\" \"%H:%M:%S)\n\n
|
|
|
+# 清理超过一定体积的日志文件\n
|
|
|
+clearLog() {\n
|
|
|
+ outcome= du \$1 | awk '{ if(\$1 > ${file_max_size}){printf\"1\"} else {printf\"2\"}}'\n
|
|
|
+ if [[ \$outcome = '1' ]];\n
|
|
|
+ then\n
|
|
|
+ echo '' > \$1\n
|
|
|
+ fi\n
|
|
|
+}\n
|
|
|
+clearLog ${PWD}/run.log\n
|
|
|
+clearLog ${PWD}/run.err\n\n
|
|
|
+echo \${time} 进行一次删除操作 >> ${PWD}/run.log
|
|
|
+"
|
|
|
+
|
|
|
+init() {
|
|
|
+ # -f 参数判断 $file 是否存在
|
|
|
+ if [ -f "$plist_path" ]; then
|
|
|
+ unload;
|
|
|
+ sudo rm $plist_path;
|
|
|
+ fi
|
|
|
+ # 创建自动化脚本
|
|
|
+ sudo echo $Launch_daemons > $plist_path;
|
|
|
+ echo $run > ${PWD}/run.sh;
|
|
|
+ chmod +x run.sh;
|
|
|
+ echo '' > ${PWD}/run.err;
|
|
|
+ echo '' > ${PWD}/run.log;
|
|
|
+ load;
|
|
|
+}
|
|
|
+
|
|
|
+load() {
|
|
|
+ sudo launchctl load -w $plist_path
|
|
|
+}
|
|
|
+
|
|
|
+reload() {
|
|
|
+ unload;
|
|
|
+ load;
|
|
|
+}
|
|
|
+
|
|
|
+unload() {
|
|
|
+ sudo launchctl unload $plist_path
|
|
|
+}
|
|
|
+
|
|
|
+case $1 in
|
|
|
+ 'init') # 初始化:写入脚本
|
|
|
+ init;;
|
|
|
+ 'load') # 加载定时任务
|
|
|
+ load;;
|
|
|
+ 'reload') # 重新加载定时任务
|
|
|
+ reload;;
|
|
|
+ 'unload') # 卸载定时任务
|
|
|
+ unload;;
|
|
|
+esac
|
|
|
+```
|
|
|
+
|
|
|
+2、执行脚本:
|
|
|
+```bash
|
|
|
+sudo bash loop.sh init
|
|
|
+```
|
|
|
+3、如果有调整的话,再次执行 `sudo bash loop.sh init`即可
|
|
|
+
|
|
|
+### 命令
|
|
|
+```bash
|
|
|
+启动:
|
|
|
+launchctl load -w /Library/LaunchDaemons/com.remove.defender.plist
|
|
|
+
|
|
|
+终止:
|
|
|
+launchctl unload /Library/LaunchDaemons/com.remove.defender.plist
|
|
|
+```
|
|
|
+
|
|
|
+### LaunchDaemons
|
|
|
+path: /Library/LaunchDaemons/com.remove.defender.plist
|
|
|
+
|
|
|
+```xml
|
|
|
+<?xml version="1.0" encoding="UTF-8"?>
|
|
|
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
+<plist version="1.0">
|
|
|
+ <dict>
|
|
|
+ <key>Label</key>
|
|
|
+ <string>com.remove.defender</string>
|
|
|
+ <key>removeDefender</key>
|
|
|
+ <string>com.remove.defender</string>
|
|
|
+ <key>ProgramArguments</key>
|
|
|
+ <array>
|
|
|
+ <string>/Users/sysadmin/run.sh</string>
|
|
|
+ </array>
|
|
|
+
|
|
|
+ <key>LimitLoadToSessionType</key>
|
|
|
+ <array>
|
|
|
+ <string>Aqua</string>
|
|
|
+ <string>Background</string>
|
|
|
+ <string>LoginWindow</string>
|
|
|
+ <string>StandardIO</string>
|
|
|
+ <string>System</string>
|
|
|
+ </array>
|
|
|
+
|
|
|
+ <key>StartInterval</key>
|
|
|
+ <integer>900</integer>
|
|
|
+
|
|
|
+ <key>StandardOutPath</key>
|
|
|
+ <string>/Users/sysadmin/run.log</string>
|
|
|
+ <key>StandardErrorPath</key>
|
|
|
+ <string>/Users/sysadmin/run.err</string>
|
|
|
+ </dict>
|
|
|
+</plist>
|
|
|
+
|
|
|
+```
|
|
|
+### run.sh
|
|
|
+```bash
|
|
|
+#!/bin/sh
|
|
|
+sudo '/Library/Application Support/Microsoft/Defender/uninstall/uninstall'
|
|
|
+time time=$(date "+%Y%m%d-%H%M%S")
|
|
|
+echo ${time} >> ~/run.log
|
|
|
+```
|
|
|
+
|
|
|
+GUI工具:
|
|
|
+
|
|
|
+下载地址:https://www.soma-zone.com/download/files/LaunchControl-2.0.1.tar.xz
|
|
|
+
|
|
|
+挂载成功:
|
|
|
+![[Pasted image 20221104200330.png]]
|
|
|
+
|
|
|
+卸载成功:
|
|
|
+![[Pasted image 20221104200440.png]]
|