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