Append current date to the filename via Cron? - cron

I've created a Cron task at my webhost to daily backup my database and I would like it to append the current date to the filename.
My Cron job looks like this
mysqldump -u username -pPassword db_name > www/db_backup/db_backup+date%d%m%y.sql
But the file I get is this: db_backup+date no file extension or date.
I've also tried this command
mysqldump -u username -pPassword db_name > www/db_backup/db_backup_'date +%d%m%y'.sql
but that doesn't even give an file output.
What is the right syntax for getting the date appended to my file??

* * * * * echo "hello" > /tmp/helloFile_$(date +\%Y\%m\%d\%H\%M\%S).txt
You just need to escape the percent signs.
Other date formats:
http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/

You should use `` instead of '' around the command you want to execute to generate the current date for your filename.

You must escape the format and use evaluation
mysqldump -u username -pPassword db_name > www/db_backup/db_backup_`date +\%d\%m\%y`.sql

I need to create a new log file every time command is executed. So every day I should have a log like this /home/me/Logs/power_20151230.log
The crontab line that I use is this:
00 8 * * * /home/me/power.py ON >> /home/me/Logs/power\_`date +20\%y\%m\%d`
Note the underscore character must be escaped too.

Related

How can I move a file and append a timestamp to its name via cron job?

I have a file CBD_DATA.zip present in a directory: /home/cbd_dev/CBD_DATA.zip. I want to move it to the directory /home/sundaram_srivastava/archives/ as CBD_DATA_{DateTimeStamp}.zip.
I have tried using a cron job:
* * * * * mv /home/cbd_dev/CBD_DATA.zip /home/sundaram_srivastava/archives > /home/sundaram_srivastava/archives/CBD_DATA_`date +\%d\%m\%y`.zip
The problem with the cron job above is that it moves the file as CBD_DATA.zip into another directory with the same name and then it creates another file CBD_DATA_110620.
Now, the file CBD_DATA_110620 is 0 KB. So, in the destination directory, I have two files, CBD_DATA.zip and CBD_DATA_110620, but I want just one and it should not be empty.
What should I change in my cron code?
First, I'd try and figure out the command on its own, without a cron job. What you're doing is something like this (with shortened directory paths for readability):
mv /foo/CBD_DATA.zip /foo/archives > /foo/archives/CBD_DATA_`date +%d%m%y`.zip
This moves the file and then creates a new empty file; there is no output from the mv command, and the redirection has nothing to redirect, so the file with the datestamp is empty.
The second argument for the mv command is the new location itself; if it is a directory, the filename stays the same, but if it is not a directory, it is interpreted as the new name. You don't need any redirection.
mv /foo/CBD_DATA.zip "/foo/archives/CBD_DATA_$(date '+%d%m%y').zip"
I have replaced the deprecated backticks in the command substitution with $(...) and quoted the expansion. Side note: if you can choose the datestamp format, I'd strongly recommend using +%F (or %Y%m%d) instead so it sorts chronologically.
With your paths and escaped for a cron job (do you really want to run this every minute?):
* * * * * mv /home/cbd_dev/CBD_DATA.zip "/home/sundaram_srivastava/archives/CBD_DATA_$(date +\%d\%m\%y).zip"

how to add a timestamp along with the error log for a script from crontab

I have a crontab running like:
*/15 * * * 4,5 /apps/ins/sid/compare_stats 2>> /apps/ins/sid/compare_stats.err
Everything working as expected. the only thing is I want my error logs to generate in the compare_stats.err file like this:
Jul 3 14:45:04 <error text>
which means I just want to add a date along with this. Is there any way to do it by modifying the crontab entry ( without really making any change in my script) ?
Thanks in advance.
Use the ts command which is part of the moreutils package. E.g.:
*/15 * * * 4,5 /apps/ins/sid/compare_stats | ts '[%Y-%m-%d %H:%M:%S]' 2>> /apps/ins/sid/compare_stats.err
This will prepend the timestamp to every line of the output and save it into your log.

Date format shows exception in shell script

The shell script:
echo '/bin/date -d "1 days ago" +"%x"'
When I run the shell script directly from terminal the output is:
06/07/2013\n
But while I run script in the crontab, the output is:
06/07/13\n
I hope the output should be the same with the upper one. If you know the reason, can you share with me.
The %x flag means "locale's date representation". chrony has different locale settings compared to your user, to verify this try to execute the locale command under the cron user (ie adding * * * * * root locale > /root/cron-locale to /etc/crontab) ; you will see it is:
LANG=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
while running locale for your user account would probably return something like:
LANG=en_US.utf8
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=
To have a consistent date representation, use: %Y , which means 4-chars year

Multiple Query String Items in crontab Job

I have the following cron job command:
* * * * * /usr/bin/lynx -term=vt100 http://abc.com/dir1/di2/script.php?action=add&config=xyz >/dev/null 2>&1
My PHP script does not recognize _GET['config'] and I get a "Cron Daemon" email message which seems to alert me that the crontab instruction is not correct.
If I take out the 2nd _GET var I do not get the "Cron Daemon" email.
Any thoughts or suggestions on how to define multiple query string items in a crontab job?
BTW, I tried the URL Encode char for the ampersand and that did not work either.
Try putting your url in quotes :
* * * * * /usr/bin/lynx -term=vt100 "http://abc.com/dir1/di2/script.php?action=add&config=xyz" >/dev/null 2>&1
For the little explanation, & is a special character which put the process in the background, so you have to put the url in quotes, otherwise cron try to put the first part in the background and execute the second part.

Cron error with using backquotes

The following works fine from command line
/usr/bin/mysqldump -uUser -pPass Db_name > /var/www/db_backup/db.`date +%Y%m%d%H%M`.sql
but when I try to do that in cron, I get the error:
bad ` sign
errors in crontab file, can't install
I saw someone else on the net solve the same problem by escaping the percent signs, but that didn't help and I tried it with just date inside backquotes with no format specifiers and still got the errors.
I've also seen date's argument enclosed in single or double quotes, but that doesn't help either.
Granted I could just throw it into a script and execute that I suppose - but what fun is that?
Any ideas? I'm using RHEL 5.
Try it with $() instead of backticks. And you probably do need to escape the percent signs since cron converts them to newlines otherwise.
* 0 * * * /usr/bin/mysqldump -uUser -pPass Db_name > /var/www/db_backup/db.$(date +\%Y\%m\%d\%H\%M).sql
Also, you should store the password in an option file with secure permissions (eg. 600 or 640) instead of passing it on the command line.
Put your one line script (as shown) into a proper script file and invoke that from cron:
$ cat /usr/local/bin/db-backup
#!/bin/sh
/usr/bin/mysqldump -uUser -pPass Db_name > \
/var/www/db_backup/db.`date +%Y%m%d%H%M`.sql
$ # use RHEL commands to add db-backup to your crontab

Resources