how to make scheduler backup command history every restart ?
i try with crontab -e
@reboot history > backup.txt

and I reboot my pc there is a file backup.txt in directory but the file is empty, there are no results of the command history

3 Answers 3


First thanks for answer
Before see answer i found the solution

crontab -e
@reboot cp /home/username/.bash_history /home/username/History_Command.txt

And it works

  • 1
    I advise perform this checking before replace the history file to avoid backup the truncated history file if something went wrong. (adjust 10000 to your current total lines, and also <username> and pls_check_bash_history path): SHELL=/bin/bash @reboot if [[ $(wc -l </home/<username>/.bash_history) -ge 10000 ]]; then cp /home/<username>/.bash_history /home/<username>/.bash_history_reboot_bk; else echo $(wc -l </home/<username>/.bash_history) > /home/<username>/Downloads/pls_check_bash_history; fi
    – 林果皞
    Nov 16, 2017 at 10:37
  • Worth adding --backup option to the previous comment script to avoid truncated history issue.
    – LMC
    May 12, 2021 at 17:56

Since the history is just a file, you could simply copy it:

@reboot cp $HOME/.bash_history $HOME/backup.txt 2>&1

Bash's history doesn't work that way. Read this link for more information.

A direct quote from the link above:

Bash maintains the list of commands internally in memory while it's running.

Since Bash is not running after a reboot, you get no output.

Furthermore, cron does not use the same PATH as your Bash shell does. More on that here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .