จะลบโฟลเดอร์ /tmp โดยอัตโนมัติใน Linux ได้อย่างไร? สคริปต์ทุบตีล้างข้อมูลบันทึกดิสก์อัตโนมัติ
เผยแพร่แล้ว: 2014-03-13
นี่คือสคริปต์ง่ายๆ ที่จะทำการล้างข้อมูลบันทึกบนดิสก์โดยอัตโนมัติสำหรับสภาพแวดล้อม Linux ใดๆ คุณเพียงแค่ต้องระบุ CRUNCHIFY_TMP_DIRS ที่ถูกต้อง เมื่อดิสก์ไม่มีเนื้อที่ว่างเหลืออยู่ ปัญหาต่างๆ อาจเกิดขึ้นได้
แต่ก่อนหน้านั้น เรามาทำความเข้าใจพื้นฐานของคำสั่งสำคัญบางคำสั่งกันก่อน
ขั้นตอนที่ 1)
ตรวจสอบคำสั่ง df -H
ใช้คำสั่ง df
เพื่อแสดงข้อมูลเกี่ยวกับพื้นที่ทั้งหมดและพื้นที่ว่างบนระบบไฟล์
พารามิเตอร์ FileSystem ระบุชื่อของอุปกรณ์ที่ระบบไฟล์ตั้งอยู่ ไดเร็กทอรีที่ระบบไฟล์ถูกเมาต์ หรือชื่อพาธสัมพัทธ์ของระบบไฟล์
หากคุณไม่ได้ระบุพารามิเตอร์ FileSystem df command
จะแสดงข้อมูลสำหรับระบบไฟล์ที่เมาท์ทั้งหมดในปัจจุบัน หากระบุไฟล์หรือไดเร็กทอรีไว้ คำสั่ง df จะแสดงข้อมูลสำหรับระบบไฟล์ที่ไฟล์นั้นอยู่
เอาท์พุท:
1 2 3 |
Filesystem Size Used Avail Capacity iused ifree % iused Mounted on / dev / disk0s2 499G 114G 385G 23 % 27868719 94059510 23 % / devfs 189k 189k 0B 100 % 640 0 100 % / dev |
ขั้นตอนที่ 2)
ถัดไปกรองระบบไฟล์และหาเปอร์เซ็นต์ของพื้นที่
1 |
df - H | grep - vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' |
เอาท์พุท:
1 2 |
23 % / dev / disk0s2 100 % devfs |
ดังนั้น ในบางครั้งโดยทางโปรแกรม คุณอาจต้องการล้างไฟล์จากโฟลเดอร์เฉพาะ ในกรณีที่พื้นที่ของคุณหมด ในกรณีนั้น คุณเพียงแค่ต้องรันสคริปต์ด้านล่าง และมันจะจัดการล้างไฟล์ที่ไม่ได้ใช้ทั้งหมดตามเกณฑ์การกรองที่กล่าวถึงในสคริปต์ นอกจากนี้ยังส่งอีเมลไปยังผู้ใช้ที่ระบุในสคริปต์
ต้องอ่านอีก:
- วิธีเรียกใช้คำสั่ง Windows/Mac ใน JAVA และส่งคืนผลลัพธ์ข้อความ
ทำสคริปต์ Linux DiskCleanup ให้สมบูรณ์:
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 |
#!/bin/bash # Diskclean-Linux.sh - Remove unused files from /tmp directories # @author: Crunchify.com # ------------- Here are Default Configuration -------------------- # CRUNCHIFY_TMP_DIRS - List of directories to search # DEFAULT_FILE_AGE - # days ago (rounded up) that file was last accessed # DEFAULT_LINK_AGE - # days ago (rounded up) that symlink was last accessed # DEFAULT_SOCK_AGE - # days ago (rounded up) that socket was last accessed CRUNCHIFY_TMP_DIRS = "/tmp /var/tmp /usr/src/tmp /mnt/tmp" DEFAULT_FILE_AGE =+ 2 DEFAULT_LINK_AGE =+ 2 DEFAULT_SOCK_AGE =+ 2 # Make EMPTYFILES true to delete zero-length files EMPTYFILES = false #EMPTYFILES=true cd / tmp / log "cleantmp.sh[$] - Begin cleaning tmp directories" echo "" echo "delete any tmp files that are more than 2 days old" / usr / bin / find $ CRUNCHIFY_TMP_DIRS \ - depth \ - type f - a - ctime $ DEFAULT_FILE_AGE \ - print - delete echo "" echo "delete any old tmp symlinks" / usr / bin / find $ CRUNCHIFY_TMP_DIRS \ - depth \ - type l - a - ctime $ DEFAULT_LINK_AGE \ - print - delete echo "" if / usr / bin / $ EMPTYFILES ; then echo "delete any empty files" / usr / bin / find $ CRUNCHIFY_TMP_DIRS \ - depth \ - type f - a - empty \ - print - delete fi echo "Delete any old Unix sockets" / usr / bin / find $ CRUNCHIFY_TMP_DIRS \ - depth \ - type s - a - ctime $ DEFAULT_SOCK_AGE - a - size 0 \ - print - delete echo "" echo "delete any empty directories (other than lost+found)" / usr / bin / find $ CRUNCHIFY_TMP_DIRS \ - depth - mindepth 1 \ - type d - a - empty - a ! - name 'lost+found' \ - print - delete echo "" / usr / bin / logger "cleantmp.sh[$] - Done cleaning tmp directories" # send out an email about diskcleanup action mail - s "Disk cleanup has been performed successfully." you @ email .com echo "" echo "Diskcleanup Script Successfully Executed" exit 0 |
หวังว่านี่จะช่วยได้ หากคุณมีคำถามใด ๆ เกี่ยวกับสคริปต์ด้านบนโปรดแจ้งให้เราทราบ ข้อเสนอแนะใด ๆ ยินดีต้อนรับมากที่สุด สคริปต์ทำงานภายใต้ Linux และ Mac OS X

ต้องการเรียกใช้สคริปต์ด้านบน every 3 days
หรือไม่ เพียงใช้กำหนดการ cron ด้านล่าง
บทแนะนำโดยละเอียดเกี่ยวกับ Setting up CronJobs
กำลังดำเนินการอยู่ โปรดคอยติดตาม
1 |
0 0 */ 3 * * / opt / crunchify / crunchify_script . sh |