Reese Knowledgebase

Date Variable

View Kristian Reese's profile on LinkedIn

If you like this article, please +1 or Recommend via FB with the provided buttons above:

Article ID: 146
by: Reese K.
Posted: 16 Jan, 2014
Last updated: 16 Jan, 2014
Views: 761

When logging time sensitive information, I like to create a NOW variable with the following data format:

[root@atl4vps01 ~]# date +"%m-%d-%Y_@_%T"

Include the following line in any of your scripts, and 'echo $NOW' whenever needed

NOW=$(date +"%m-%d-%Y_@_%T")

A word of caution:

In the case of formatting the date directly in a crontab, it's VERY important the % is escaped, otherwise, cron converts them into newline, and everything past the non-escaped % is passed to your command and will cause the rest of crontab to be misinterpreted, resulting in failed execution.

For example, within crontab, you have something like this, use the escape:

0 0 * * * /usr/sbin/vzabackup -i --rm-old --backup-folder-path /vz/backup/atl4vps04/vzlocal/ -D "local container backup" --chain-length 5 localhost -e 16343 20474 21394 >/vzlocal/backup.`date +"\%m-\%d-\%Y_@_\%T"`.log 2>&1

If NOW is defined as a variable within the script that's being called from cron, then this isn't necessary.

If you find yourself needing to troubleshoot your cronjob as to why a script won't execute when it runs fine from the command line, you can always capture the output into a log file by appending the following within crontab:

* * * * * /root/scripts/ssh-blackhole.bsh &>/tmp/mycommand.log

I've included an external resource link below that I found useful before when troubleshooting crontab issues.

This article was:   Helpful | Not Helpful
External links

Prev   Next
conditional statements without using $? exit status internal...     Monitor Log for pattern match and trigger action