Embed Notice
HTML Code
Corresponding Notice
- Embed this notice@yaboisugoi @DiamondMind @Economic_Hitman @GhostOfMoshe @Godsend @JVKO @Mombi @NonPlayableClown @SilverDeth @TheBoonieGhoul @VaxxSabbath @bonifartius @ceo_of_monoeye_dating @dcc @dorkvalized @fish @fluffy @freemayonnaise @ins0mniak @ins0mniak @mrsaturday @nimt @pista @rebootbrain @samwise @sicp @takao @thebitchisback @tsubo @w0rm @yomiel If you run them in screen/dtach/tmux that is a little easier. But you can also have a bash prompt hook save the history to ~/tmp/bash.$pid.hist every time it prompts you. But that requires doing it ahead of time. This would solve that:
:mycomputer: ps auxw | awk -v "p=$$" 'BEGIN{while(p != 1){no[p]++;f=sprintf("/proc/%d/stat",p);getline < f;close(f);p=$4}} $1==ENVIRON["USER"] && !no[$2] && $11~/bash/{cmd=sprintf("kill -HUP %d && cp %s/.bash_history %s/tmp/bash.%d.hist", $2, ENVIRON["HOME"], ENVIRON["HOME"], $2);print cmd}'
The BEGIN block starts with its own pid (set by -vp=$$), then walks up until it gets to init, marking the pids; this is to avoid killing its own shell. (You could just nohup the awk process but it'll still end up killing the parent shell.) Then it checks if the process is running as the same user, isn't on the blacklist, and is bash or at least the first argument has "bash" as a substring, then it prints what it would do. You could change `print cmd` to `system(cmd)` and it'd just execute it. The idea is we just send SIGHUP to all of the bash processes and then copy their history to ~/tmp.