0

The bot on this forum considers my thread to be a duplicate. However, the solution that is proposed does not answer the main question: how, USING A VARIABLE to output the date or the output of any other command from the shell. I can output the date without using a variable easily. But how do this with use a variable?

It is not possible to enter the current date in a variable in the crontab and display it in the name of the log file.

Wrote this:

SHELL=/usr/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/sy
HOME=/home/user/
NOW=`date '+%Y-%m-%d'`

/1 * * * cd /work/current && nice -n 15 php app/console debug:container >> /www/log/cron/test${NOW}.log

I tried this too:

*/1 * * * * cd /work/current && nice -n 15 php app/console debug >> /www/log/cron/_test_${$NOW}.log

And I tried so:

*/1 * * * * cd /work/current && nice -n 15 php app/console debug >> /www/log/cron/_test_"${NOW}".log

But it still doesn't work.

Or it outputs like this:

__test_`date '\''+\%Y-\%m-\%d'\''`.log

or cron displays an error in the log:

ambiguous redirect
  • IIRC cron doesn't like %, they need to be escaped. – rathier Dec 12 '23 at 14:18
  • Have you considered writing a script to perform the append to the logfile instead of asking cron to do it? You'll have the full power of the shell instead of the more limited crontab syntax. The script could even invoke the cd and php commands too. A script is easier to store and track in a source code control system than most crontab files. – Sotto Voce Dec 12 '23 at 14:29
  • Yeah, I've been thinking about that. And I realize it would be easier in some ways. But I would like to implement it using cron. Is that possible? – sivsoft Dec 12 '23 at 21:20
  • If I escaped %, will I get the variable output correctly? – sivsoft Dec 12 '23 at 21:22
  • I'm not a bot. I just think that your question is a duplicate of the other one I've linked. Fundamentally the problem is the line NOW=`date '+%Y-%m-%d'` – Chris Davies Dec 12 '23 at 22:43
  • Excuse me then ). This construct works if you enter it like this: /1 * * * * * cd /work/current && nice -n 15 php app/console debug:container >> /www/log/cron/testdate '+\%Y-\%Y-\%m-\%d'.log. But I need it to be in a variable. Like this: NOW=date '+%Y-%m-%d' it doesn't work. Nor does it work in the following way: NOW=$(date '+%Y-%m-%d), and in the following way: NOW=date '+%Y-%m-%d', and in the following way: NOW=date '+\%Y-\%m-\%d'. So the question is more about how exactly to enter this construct into a variable and how to pass this variable to cron. – sivsoft Dec 13 '23 at 08:55
  • But, I fix the problem. Thanx for link. Sorry, there was an option with a variable in one of the posts. I just didn't see it right away. – sivsoft Dec 13 '23 at 17:40

0 Answers0