Command-History
是否可以查看另一個 shell 的歷史記錄?
我正在一個
bash
視窗中執行一些我不想中斷甚至暫時暫停的東西。是否可以查看該特定視窗會話的命令歷史記錄?我有多個視窗打開,所以查看.bash_history
不會有太大幫助。
不,
bash
不支持。歷史記錄保存在記憶體中,在使用或保存到.bash_history
同一會話之前,其他程序無法使用。但是,一旦將其寫入文件系統,命令源自哪個會話的資訊就會失去。history -a``history -w
您可以獲得的最接近的方法是使用一些行
.bashrc
來讓bash
執行後直接附加每個命令:https
://unix.stackexchange.com/a/1292/147970 然後您可以在.bash_history
.要訪問特定會話的歷史記錄,您需要使案例如中斷該會話中的前台程序
Ctrl+Z
。
以下是如何使用
gdb
(您需要使用管理權限執行它),通過https://stackoverflow.com/questions/7272558/can-we-define-a-new-data-type-in-a -gdb-會話:準備:
echo 'typedef void * histdata_t; typedef struct _hist_entry { char *line; char *timestamp; histdata_t data; } HIST_ENTRY; typedef struct _hist_state { HIST_ENTRY **entries; int offset; int length; int size; int flags; } HISTORY_STATE; HIST_ENTRY _sampleentry; HISTORY_STATE _samplestate; ' | tee sample.c # get sample.o gcc -g -c sample.c # get bash pid, maybe via `pgrep bash`, or `pidof bash`, etc # say in this example, it is 16573
執行測試命令:
$ sudo gdb -p 16573 -ex "set confirm off" -ex "add-symbol-file sample.o 0" -ex 'printf "ptype HIST_ENTRY\n"' -ex "ptype HIST_ENTRY" -ex 'printf "p *(HISTORY_STATE*)history_get_history_state()\n"' -ex 'p *(HISTORY_STATE*)history_get_history_state()' -ex 'set $myoffs = (*(HISTORY_STATE*)history_get_history_state())->offset' -ex 'printf "myoffs %d\n", $myoffs' -ex 'printf "p *(HIST_ENTRY *)history_get($myoffs)\n"' -ex 'p *(HIST_ENTRY *)history_get($myoffs)' .... 0x00007fb053abb0e9 in __pselect (nfds=1, readfds=0x7ffe81a009b0, writefds=0x0, exceptfds=0x0, timeout=<optimized out>, sigmask=0x7ffe81a00930) at ../sysdeps/unix/sysv/linux/pselect.c:69 69 ../sysdeps/unix/sysv/linux/pselect.c: No such file or directory. add symbol table from file "sample.o" at .text_addr = 0x0 Reading symbols from sample.o...done. ptype HIST_ENTRY type = struct _hist_entry { char *line; char *timestamp; histdata_t data; } p *(HISTORY_STATE*)history_get_history_state() $1 = {entries = 0x55ed117f4ab0, offset = 155, length = 155, size = 502, flags = 1} myoffs 155 p *(HIST_ENTRY *)history_get($myoffs) $2 = {line = 0x55ed119684d0 "kill -STOP $$", timestamp = 0x55ed119709a0 "#1545016332", data = 0x0}
準備 gdb“擷取最後的歷史條目”命令:
echo ' set verbose off set complaints 0 set trace-commands off add-symbol-file sample.o 0 set $myoffs = ((HISTORY_STATE*)history_get_history_state())->offset set $line = ((HIST_ENTRY *)history_get($myoffs))->line printf "%s\n", $line ' | tee gdbscript
執行 gdb“擷取最後一個歷史條目”命令:
sudo gdb -p 16573 -batch -x gdbscript 2>/dev/null | tail -1 kill -STOP $$