shell历史命令

1、每分钟备份历史命令

制定计划任务:每分钟执行备份历史命令的脚本

注意:要用python写计划任务脚本,因为用shell脚本写的计划任务总是不执行

先写脚本:

[root@master ~]# cat /backup/history.py 
#!/usr/bin/python
# -*- coding:UTF-8 -*-
ms=open("/root/.bash_history") 
with open('/backup/history.txt','w+') as mon: 
    mon.writelines(ms)
ms.close()


启动计划任务:

[root@www mnt]#crontab -e
*/1 * * * * python /backup/history.py

查看效果:

[root@master ~]# cat /backup/history.txt 



2、修改历史命令保存个数

shell历史命令默认保存1000条,修改成10000条

sed -i 's/HISTSIZE=1000/HISTSIZE=10000/g' /etc/profile

source /etc/profile


3、时刻同步历史命令到bash_history,

history 写到计划任务里面不管用,试过,不管用shell还是python都不管用,只能手动执行要保存的命令,没有捷径

history -w ~/.bash_history







原文地址:https://www.cnblogs.com/effortsing/p/10296234.html