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
来源: http://www.bubuko.com/infodetail-2928261.html