I've been looking around but haven't found a solution to my particular problem.

I need to create a cron job to backup a file system on daily basis. But the application needs the current date/time to run. Example:

bundle exec thor migrator:export /var/tmp/backups --after "2016-12-22 00:00:00 -0700"

How can I easily run the above command whereby the date will change daily? Time will remain the same.


Assuming the following line is your example.

bundle exec thor migrator:export /var/tmp/backups --after "2016-12-22 00:00:00 -0700"

You could use a command substitution for this job.

bundle exec thor migrator:export /var/tmp/backups --after "$(date --iso) 00:00:00 -0700"

But I rather would recommend to put your command into a separate shell script and run the shell script from cron.

  • 1
    +1 for that last statement. It's also a good idea to have a private daily.sh script that cron runs daily, and similarly for hourly and monthly jobs. The scripts could collect all tasks that you need to perform at those intervals, and it would keep the crontab very simple. – Kusalananda Jan 6 '17 at 12:50
  • @Kusalananda: I see no immediate reason a regular user couldn't call run-parts ~/etc/cron.daily or similar, which will conveniently run all scripts in a directory, a la /etc/cron.daily. No need to bunch different tasks into a single script. – Ulrich Schwarz Jan 6 '17 at 13:36
  • @UlrichSchwarz run-parts? Of course, if you're on a Linux box. I'm not. – Kusalananda Jan 6 '17 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.