Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable date not working in linux cron

Tags:

linux

cron

I am having an issue that I can not find any information for while doing an extensive google search.

I have a linux cron, running via crontab, that works great until I try to add a variable date to the title of the file. BUT.. When I run the same command outside the cron, just from the command line, it works fine.. Also, the cron does work if I take out the date part.

Command line code that works:

sudo mysqldump -h mysql.url.com  -u user -pPassword intravet sites | gzip > /mnt/disk2/database_`date '+%m-%d-%Y'`.sql.gz

Cron that works:

15 2 * * * root mysqldump -h mysql.url.com -u user -pPassword intravet sites | gzip > /mnt/disk2/database.sql.gz

Cron that DOESN'T work:

15 2 * * * root mysqldump -h mysql.url.com -u user -pPassword intravet sites | gzip > /mnt/disk2/database_`date '+%m-%d-%Y'`.sql.gz

I am not understanding why I can not use the date function while inside a cron? Everything I find says I can, but in practice, I can not.

Server details: Ubuntu 12.04.5

Thank you for any insight.

like image 594
Jason Avatar asked Apr 17 '15 15:04

Jason


People also ask

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week . 0 1 * * * - this means the cron will run always at 1 o'clock. * 1 * * * - this means the cron will run each minute when the hour is 1.

How do I set the date in crontab?

Crontab date/time fieldsRanges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an "hours" entry specifies execution at hours 8, 9, 10 and 11. Lists are allowed.

Can cron read environment variables?

When cron executes a job, it doesn't load the environmental variables from files like ~/. bashrc, ~/. bash_profile, /etc/profile, and others. This is because cron runs jobs from a non-interactive, non-login shell.

Why crontab scripts are not working?

One of the most frequent causes for the crontab job not being correctly executed is that a cronjob does not run under the user's shell environment. Another reason can be – not specifying the absolute path of the commands used in the script.


2 Answers

You just need to use escaping % sign

* * * * * touch /tmp/foo_`date '+\%m-\%d-\%Y'`.txt

Result:

[root@linux tmp]# ls -l /tmp/foo_*
-rw-r--r-- 1 root root 0 Apr 18 02:17 /tmp/foo_04-18-2015.txt
like image 130
Satish Avatar answered Sep 19 '22 06:09

Satish


Try replacing the backticks with $() and escaping your %s, such as:

15 2 * * * root mysqldump -h mysql.url.com -u user -pPassword intravet sites | gzip > /mnt/disk2/database_$(date '+\%m-\%d-\%Y').sql.gz

I only mention removing the backticks because you will end up having all kinds of escaping problems later in your coding endeavours. Stick with using $() for command substitution.

like image 20
deutl Avatar answered Sep 22 '22 06:09

deutl