[教學] How To Run Cron Job Script On The Last Day Of a Month

本帖最後由 toylet 於 2015-8-6 23:49 編輯

有趣的方法!

Full article: http://www.cyberciti.biz/faq/uni ... ast-day-of-a-month/

Say hello to TZ variable

TZ is time zone environment variable on Linux or Unix-like systems. The TZ environment variable tells functions such as the ctime(3) family and programs like date what the time zone and daylight saving rule is. For example, Greenwich Mean Time can be defined as follows:

TZ='GMT'

You can set TZ as follows to get tomorrow from the current date (+%d):
TZ=GMT-24 date +%d
  1. #!/bin/bash
  2. # Purpose: Tell if it is last day of a month
  3. # Author: Vivek Gite <www.cyberciti.biz> under GPL v2.x+
  4. # ---------------------------------

  5. # Find tomorrow day in IST time zone
  6. day=$(TZ=IST-24 date +%d)

  7. # Compare it
  8. # If both are 1 means today is the last day of a month
  9. if test $day -eq 1; then
  10.    echo "Last Day of a Month"
  11. else
  12.    echo "Noop"
  13. fi
複製代碼