King Zone - crontab
type: channel
link: https://www.kingtam.win/tag/crontab/
items: Automating Blog Updates Across Multiple Servers with cron Jobs and Shell Scripts
items: 啟用Windows 10/11 內置 WSL2的Crontab服務

Automating Blog Updates Across Multiple Servers with cron Jobs and Shell Scripts
type: item
date: 2023-05-19T12:01:00+08:00
description: Automating Blog Updates Across Multiple Servers with cron Jobs and Shell ScriptsLogo.jpgAbout Script With these scripts, update blog servers data just once each time.Update the first server, and the updates will propagate to the other servers automatically. And with the backup cron job, also, the data is always safe and secure.Scenario There have 3 servers for blog: blog 01, blog 02, and blog 03. Whenever update the first server (blog 01), it triggers the update of the second server (blog 02), which in turn triggers the update of the third server (blog 03). Additionally, a backup of the latest zip file is stored on a storage server through a cron job.Architecture 2023-05-19_120653.pngInformation on the serversInformation on the servers NumberServer NameIP Address01blog 0110.1.1.102blog 0210.2.2.203blog 0310.3.3.304storage server10.4.4.4Requirements Each server must be able to communicate with the others via SSH without a password.The blog services on each server must be deployed through Docker and Docker Compose.A user account (in this scenario, named "user") must have privileges to run Docker.1st Server (Blog01) In the first server, blog 01. Set up a cron job to run a shell script that checks if a directory has been modified in the last 5 minutes.crontab -e0 1 * bash /apps/bak_dnmp.sh > /apps/archive/$(date +%Y-%m-%d).logIf it has been modified, the script performs a backup of a specified directory, copies the backup to a remote server, and runs an update script on the remote server. If the directory has not been modified, the script does nothing.The shell script for blog 01:#!/bin/bash # Set the directory to check DIR="/apps/dnmp/www/" if find "$DIR" -type f -mmin -5 | grep -q "." then echo "Directory has been modified in the last 5 minutes" # Variables BACKUP_FILE="/apps/dnmp_backup_$(date +%Y-%m-%d).tar.gz" DNMP_PATH="/apps/dnmp" ARCHIVE_DIR="/apps/archive/$(date +%Y-%m-%d)" # Stop docker-compose docker-compose -f $DNMP_PATH/docker-compose.yml stop # Backup DNMP…

啟用Windows 10/11 內置 WSL2的Crontab服務
type: item
date: 2022-04-22T08:47:56+08:00
description: 啟用Windows 10/11 內置 WSL2的Crontab服務Logo.pnglogo source: PNGWing前言: Linux的Crontab排程，無論是備份還是執行任務都非常方便。但Windows 內置的 WSL2 (Windows Subsystem for Linux) Crontab服務，預設是沒有啟用，所有的排程也不會被執行，只能每次進入到WSL2內手動啟用Crontab服務。操作步驟: 進入到WSL內，設置帳號sudo免密碼。這樣是為了給Windows呼叫Crontab服務時不用輸入密碼。sudo tee /etc/sudoers.d/user <<< 'user ALL=(ALL) NOPASSWD: ALL'在Windows 的執行 (run)，開啟自啟動目錄。2022-04-22_080954.pngSHELL:STARTUP右鍵 -> 新增(W) -> 捷徑(S)對應圖示，目標(T) 及 開始位置(S) 貼上以下內容。2022-04-22_081024.png%WINDIR%\System32\wsl.exe sudo /etc/init.d/cron start%%WINDIR%\System32最後進入到WSL2，新增排程指命。2022-04-22_081054.pngcrontab -e0 0 * * 7 /home/user/archiveHW.sh @reboot /home/user/mount.sh 0 4 * * * /mnt/nas04/Tools/user/Bak_Rsync.sh結語: 這樣就可以在Windows的系統內自動執行Linux的排程及腳本。