I want to get job status output with latest executed time to the text file , however when I run the job each time ( schedule using cronjob ) It’s appending old, executed result also in the text file as well, but I want to get text output with latest executed result only . How to overcome this issue .
Example :
#!/bin/bash
ydate="$(date "%H-%M-%S")"
echo "${ydate} Test1 Done " >>/app/my_status.log 2>>/app/my_status.log.err
echo "${ydate} Test2 Done " >>/app/my_status.log 2>>/app/my_status.log.err
echo "${ydate} Test3 Done " >>/app/my_status.log 2>>/app/my_status.log.err
echo "${ydate} Test4 Error " >>/app/my_status.log 2>>/app/my_status.log.err
echo "${ydate} Test5 Not_Running " >>/app/my_status.log 2>>/app/my_status.log.err
when I execute script each 5 minutes, I get result like below
14-41-14 Test1 Done
14-41-14 Test2 Done
14-41-14 Test3 Done
14-41-14 Test4 Error
14-41-14 Test5 Not_Running
14-46-15 Test1 Done
14-46-15 Test2 Done
14-46-15 Test3 Done
14-46-15 Test4 Error
14-46-15 Test5 Not_Running
14-51-15 Test1 Done
14-51-15 Test2 Done
14-51-15 Test3 Done
14-51-15 Test4 Error
14-51-15 Test5 Not_Running`
But I want to get only 5 lines with latest time like below after last execution result
14-51-15 Test1 Done
14-51-15 Test2 Done
14-51-15 Test3 Done
14-51-15 Test4 Error
14-51-15 Test5 Not_Running`
echo out the result without appending
CodePudding user response:
You want to truncate then. Use a grouping.
#!/bin/bash
ydate=$(date "%H-%M-%S")
{
echo "${ydate} Test1 Done"
echo "${ydate} Test2 Done"
echo "${ydate} Test3 Done"
echo "${ydate} Test4 Error"
echo "${ydate} Test5 Not_Running"
} >/app/my_status.log 2>/app/my_status.log.err
You can also use exec
to open a file and use a file descriptor for the whole session. Just look around for examples.