OS solaris.
I am trying to log the system info from uptime command. For this I schedule a job in crontab which runs every hour. uptime comes with the output of time but not datetime, so I combine date command with uptime.
I want to have the datetime format yyyymmddHHMM for this I format the date output with format (date +"%Y%m%d%H%M").
It works perfectly if it is not on crontab. For crontab % causes errors, so I need to escape % with **** to make it working. So here is my cron job line:
0 * * * * bash -c "date +'\%Y\%m\%d\%H\%M' | tr '\n' ' ' && uptime" >> /tmp/uptime.log
So I expect out to be like that:
201703161309 1:09pm up 52 day(s), 14:45, 2 users, load average: 0.99, 1.41, 1.45
But what I get is:
\2017\03\16\13\09 1:09pm up 52 day(s), 14:45, 2 users, load average: 0.99, 1.41, 1.45
So how to make it work properly.
Thanks in advance.
As You are using bash there is another way to escape the %
: $'\045'
. That will look strange but it should work (I have no solaris to test it on)
0 * * * * bash -c "date +$'\045'Y$'\045'm$'\045'd$'\045'H$'\045'M | tr '\n' ' ' && uptime" >> /tmp/uptime.log