I am running Ubuntu 21.10 and trying to setup a crontab task.
I want the date and time to be printed to the console before the task starts all of which get's logged to a log file.
This is what I have currently.
{ printf "\%(\%Y-\%m-\%d \%H:\%M:\%S)T Started Task\n"; <run task> } >> /path/to/log/file
When I type printf "%(%Y-%m-%d %H:%M:%S)T Started Task\n"
I get the expected output:
2021-10-20 15:22:03 Started Task
However, in the crontab I know you have to escape out the "%"
with \
. Except even after escaping out all the %
I end up with a error :
/bin/sh: 1: printf: %(: invalid directive
No matter how a try and format the crontab I still end up with the same error.
If anyone know what I have done wrong please help me, thank you.
My specific cronjob is as follows:
0 * * * * { printf "\%(\%Y-\%m-\%d \%H:\%M:\%S)T Updating Certs\n"; sudo certbot renew --post-hook "systemctl reload nginx.service postfix.service dovecot.service"; } >> /home/ubuntu/logs/certbot.log
CodePudding user response:
You have a few errors:
- with
/bin/sh
, one-liners with curly brackets may need a semi-colon at the end of the last instruction. - with
/bin/sh
,printf
may not support the%()T
feature.
Here's how you could do it with /bin/sh
:
0 * * * * { date ' \%Y-\%m-\%d \%H:\%M:\%S Started Task'; <run task>; } >> /path/to/log/file
Or just change the SHELL like in Cyrus reply
CodePudding user response:
Your cron uses sh
as shell. Your code (printf
with feature T
) expects bash
(version >= 4.2). I suggest to add this in a separate line before your cronjob to force bash
as shell.
SHELL=/bin/bash