In crontab, I scheduled a daily backup script. Now when the cron executes the script the status are logged to a log file as shown below.

0 0 * * * /home/backup.sh > /home/groupz/db-backup/fbackup.log 2>&1  

Now, when the cron executes a script the contents of the log get renewed everytime. So, I want the contents to be added to the same file with time stamp of the executed time and below the contents of each time along with the existing contents. How can I do this.

  • 1
    Don't forget to escape the "%" character in your crontab! +%d-%m-%y/%H:%M:%S Would become : +\%d-\%m-\%y/\%H:\%M:\%S – Vylmarion Jun 12 '19 at 18:34

why not just

0 * * * * (/bin/date && /home/backup.sh) >> /var/log/backup.log 2>&1


How to get timestamp in a file

To add a time stamp in a file you can use date see man date for more details. For example if you use in terminal you will have output like,

$ date +%d-%m-%y/%H:%M:%S

The output is in the format dd-mm-yy/hour:min:sec

If you wish to put the time stamp in a file, use

date +%d-%m-%y/%H:%M:%S > filename


If you use date +%d-%m-%y/%H:%M:%S > filename then the date will be stored into the file but it it will be overwritten every time you use the command. To append it in an existing file use,

date +%d-%m-%y/%H:%M:%S >> filename

It will add the last execution output at the end of your existing file.

What you do in your case

You can add the following line at the end of your /home/backup.sh,

date +%d-%m-%y/%H:%M:%S

And use the following in crontab,

0 0 * * * /home/backup.sh >> /home/groupz/db-backup/fbackup.log 2>&1

I think the above modification should do what you want.

  • This should be the accepted answer – Dominik Aug 21 '16 at 23:42

(On Debian Jessie) Use the ts command which is part of the moreutils package. E.g.:

0 0 * * * /home/backup.sh | ts '[%Y-%m-%d %H:%M:%S]' > /home/groupz/db-backup/fbackup.log 2>&1  

This will prepend the timestamp to every line of the output and save it into your log.

  • 1
    Didn't work for me, I'm using Ubuntu 16.04. I installed moreutils – Hamman Samuel Apr 22 '17 at 19:32

Since you are running a shell script, why not add a line like,

some ./script    
echo `date -u `
some ./other/script

in your script ie /home/backup.sh


0 0 * * * /home/backup.sh >> /home/groupz/db-backup/fbackup.log 2>&1

will append a line before/after fbackup.log like Thu Jun 14 11:10:22 UTC 2018

  • Isn't echo `date -u` the same as data -u? – Stephen Rauch Jun 14 '18 at 11:39
  • The echo adds a line break AIUI. – pbhj Jun 14 '18 at 13:20

You can append something to a file using two of these things '>'.

$ echo "test" >> test.txt
$ echo "test" >> test.txt

$ cat test.txt
  • 2
    What about timestamp – user3004356 Dec 16 '13 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.