当前位置: 动力学知识库 > 问答 > 编程问答 >

How to run crontab at last second of the month?

问题描述:

I need crontab to run a script that transfers info from a table in mysql to another table at the last second the month. (The last second of the last day of the month).

What would the crontab execution be for this?

Thank you.

网友答案:

Not sure this can be done in a generic way, because the last day of the month varies and as far as I can see, the crontab syntax does not offer anything for that use case.

You may have to find out the correct date for yourself, and add individual jobs (with full dates and times, i.e. 12 per year) to the crontab.

But what do you need this for in the first place? It sounds a bit like a "smell" because you can't really rely on any job being finished before the end of the month if you start it in the last second. Would executing it at 0:00 every first day of the month not be much easier?

Stolen from this answer on Serverfault:

0 0 1 * * /usr/bin/foo

网友答案:

This will get you exactly what you are looking for (down to the minute - not the second). Just adjust the crontab time I mention to be 59 23 ( http://sudobash.net/?p=424 )

Cron:

2 0 * * * /usr/bin/last-day-of-the-month.sh

Script:

#!/usr/bin/bash
# last-day-of-the-month.sh
# By: Scott Rowley
# http://www.sudobash.net/
#########################################

TODAY=`/usr/local/bin/date +%d`
TOMORROW=`/usr/local/bin/date +%d -d "1 day"`

# See if tomorrow's day is less than today's
if [ $TOMORROW -lt $TODAY ]
 then
  echo "Today is the last day of the month, running code..."
  /run/your/code
fi

exit 
分享给朋友:
您可能感兴趣的文章:
随机阅读: