4

I have a cronjob that will be launched at the end of each month to generate a monthly report.

Here I found how to launch the job at the end of each month: Cron job to run on the last day of the month

Now, I just want the date of the last day of the month as an argument for the script.

For example:

compute_monthly_rate -e **2016.01.31**

How can I pass this date as an argument?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Nokali
  • 61
  • 1
  • 6

2 Answers2

9

Most implementations of cron pass a command string to /bin/sh so depending on what it is on your system and what implementation of date you have you may have luck with this:

compute_monthly_rate -e $(date +%Y.%m.%d)

Try it in a terminal first:

$ date +%Y.%m.%d
2016.02.02
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
1

Just extending above answer,

In most of the cases, the Cron jobs takes relative dates like re-index last 15 days stats / logs, etc.

the date command can be used as,

date -d '-15days' +'%Y-%m-%d'

-d, --date option specifies the date

+'%Y-%m-%d' is the date format.

The date command accepts multiple format specifier check the date --help for full command reference.

Premkumar chalmeti
  • 800
  • 1
  • 8
  • 23