Adrian World Design
  • Services
    • Website Consulting
    • Startup Consulting
    • Zend Development
  • Portfolio
    • My Framework
  • Knowledge
    • Web Standards
    • PHP bare-bones
    • Zend Framework
    • Zend Framework 2
    • Git - Github
    • Search Engine Optimization
    • Web Hosting
    • Linux
    • Microsoft Windows
    • Web Browsers
    • Mobile Devices
  • About
    • Business
    • Professionally
    • Personally
  • Contact
    • Contact Form
    • Phone
    • Email
    • Messaging

Knowledge Base Overview

Add date to filename in cron

Knowledge ⇒ Linux ⇒ Helpful tips ⇒ Add date to filename in cron
Tweet
Share on Tumblr

Created: Nov 6, 2012, 11:51:47 AM CDTLast updated: Nov 6, 2012, 12:00:30 PM CDT

When you run some scripts in Linux with a cron job you will add this to crontab and may want to redirect the output to log file. Adding a date to this log's filename is a little tricky in crontab. Let me show you how it works.

Command Line (CLI)

Adding a date to a filename for a output redirect on the command line is fairly easy. It goes like this:

echo 'Hello world' > /var/log/somelog`date +%Y%d%m`.log

You simple put the date command with its parameters in grave accents, usually called single back quotes or back-ticks. This is known as command substitution. The problem is this doesn't seem to work in crontab but there is just a little trick you need to know and it will work just fine in a cron job too.

date command in cron

The date command in cron tab works exactly the same but the percent sign (%) we have for the date format is the problem. The percent sign has a special meaning in cron and this is throwing off the whole thing and actually causing the whole cron job to fail. The easy solution is to escape the percent sign with a backslash.

Some backslash to escape the % sign is all that is required.

blog comments powered by Disqus
Prev
Next

Powered by FeedBurner Load our "Knowledge Base" feed in your RSS feeder

Follow us on Twitter
Follow us on Facebook
Follow us on LinkedIn
Follow us on Google+

All rights reserved, Adrian World Design ©2009–2021 Powered by Wejas Framework

Jump to Top