Zur weiteren Vertiefung des Themas script sind hier noch ein paar (hoffentlich) anregende Links:
9.2.3. Recording the shell activities cleanly
The simple use of script(1) (see Section 1.4.9, “Recording the shell activities”) to record shell activity produces a file with control characters. This can be avoided by using col(1) as the following.
Script started, file is typescript
Do whatever … and press Ctrl-D to exit script.
| $ col -bx <typescript >cleanedfile
|
If you don't have script (for example, during the boot process in the initramfs), you can use following instead.
| $ sh -i 2>&1 | tee typescript
|
[Tip] Tip
Some x-terminal-emulator such as gnome-terminal can record. You may wish to extend line buffer for scrollback.
[Tip] Tip
You may use screen(1) with "^A H" (see Section 9.1.2, “Key bindings for the screen command”) to perform recording of console.
[Tip] Tip
You may use emacs(1) with "M-x shell", "M-x eshell", or "M-x term" to perform recording of console. You may later use "C-x C-w" to write the buffer to a file.
Recording shell activities in debian
System administration involves much more elaborate tasks in a Unix environment than in an ordinary personal computer environment. Make sure to know the most basic means of configuration in case you need to recover from system trouble. X11-based GUI configuration tools look nice and convenient but are often unsuitable in these emergency situations.
In this context, recording shell activities is a good practice, especially as root.
Emacs: Use M-x shell to start recording into a buffer, and use C-x C-w to write the buffer to a file.
Shell: Use the screen command with "^A H" or use the script command.
Script started, file is typescript
... do whatever ...
Ctrl-D
| $ col -bx <typescript >savefile
|
The following can be used instead of script:
| $ bash -i 2>&1 | tee typescript
|
Recording X activities
If you need to record the graphic image of an X application, including an xterm display, use gimp (GUI). It can capture each window or the whole screen. Alternatives are xwd (xbase-clients), import (imagemagick), and scrot (scrot).
How to automatically record all your terminal sessions with script utility
up vote 3 down vote favorite
1
What I want to achieve is be able to record my terminal sessions to file automatically whenever I use Yakuake/Konsole.
It's easy to achieve if at the start of my session I do:
script -f /home/$USER/bin/shell_logs/$(date +"%d-%b-%y_%H-%M-%S")_shell.log
But I want to run the above automatically whenever I start Yakuake or open a new tab.
Using .bashrc does not work because it creates endless loop as 'script' opens a new session, which in turn reads .bashrc and starts another 'script' and so on.
So presumably I need to script Yakuake/Konsole somehow to run 'script' once as a new tab gets opened. The question is how?
bash terminal scripting konsole
share|improve this question
edited Nov 30 '11 at 7:45
asked Nov 29 '11 at 12:41
knef
314
try the problematic solution with the loop problem, but prepend the exec at the start of the line. it should start the script -f in the same shell PID. – Hanan N. Nov 29 '11 at 22:28
1 Answer
active oldest votes
up vote 3 down vote
I figured it out myself ☺
So if someone wants to record their terminal sessions automatically (including SSH sessions(!)) using script utility here is how:
Add the following line at the end of /etc/bash.bashrc file (or to .bashrc in your home if you only want your own sessions to be recorded):
| test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f $HOME/$(date +"%d-%b-%y_%H-%M-%S")_shell.log)
|
(we test for shell's parent process not being script and then run script)
That's all! (assuming you have script installed, of course - script is part of bsdutils package) Now when you open new terminal you'll see:
Script started, file is /home/username/file_name.log
script will write your sessions to a file in your home directory naming them something like *30-Nov-11_00-11-12_shell.log*. Adjust this part to suit yourself - for example, you can append your sessions to one large file rather than creating a new one for every session with
| script -a /path/to/single_log_file
|
; or you can adjust where the files are written to - say, to /var/log/script/$USER_$(date +"%d-%b-%y_%H-%M-%S")_shell.log (make sure you've actually created /var/log/script and made it writable by others).
share|improve this answer
edited Nov 30 '11 at 6:51
community wiki
2 revs, 2 users 94%
knef
You can edit your question and its title yourself. Just click on the edit button that appears right below it. – Mat Nov 30 '11 at 6:51
1
You might want to consider adding a ${RANDOM} and/or $$ to the file name since starting two shells within a second of each other will cause a file-name collision. Personally, i often use
| script.$(date -u +%Y%m%dt%H%M%S).${HOSTNAME:-$(hostname)}.$$.${RANDOM}.log
|
to ensure the files are automatically sorted by date/time and they are consistent accross TZ, i know the host that initiated it, i know the owning process, and there are no name collisions. I rarely use ${USER} because it's typically something for only me. – nicerobot Dec 2 '11 at 16:33
How to run 'script' automatically on Konsole/Yakuake startup/new tab?
What I want to achieve is be able to record my terminal sessions to file automatically whenever I use Yakuake/Konsole. It's easy to achieve if at the start of my session I do: Quote:
script -f /home/$USER/bin/shell_logs/$(date +"%d-%b-%y_%H-%M-%S")_shell.log
But I want to run the above automatically whenever I start Yakuake or open a new tab. Using .bashrc does not work because it creates endless loop as 'script' opens a new session, which in turn reads .bashrc and starts another 'script' and so on. So presumably I need to script Yakuake/Konsole somehow to run 'script' once as a new tab gets opened. The question is how?
Quote: Originally Posted by klearview What I want to achieve is be able to record my terminal sessions to file automatically whenever I use Yakuake/Konsole. It's easy to achieve if at the start of my session I do: But I want to run the above automatically whenever I start Yakuake or open a new tab. Using .bashrc does not work because it creates endless loop as 'script' opens a new session, which in turn reads .bashrc and starts another 'script' and so on. So presumably I need to script Yakuake/Konsole somehow to run 'script' once as a new tab gets opened. The question is how? Well, if you only ever want the script to run at user login, remove the "#!/bin/bash" from the top of it, and let your user session do it. That way, you can shove it into .profile or .bashrc, and it'll just run once, as everything else in .bashrc does. You might also look into putting in the system wide profile (usually /etc/profile), that gets executed at each login. You can put things at the bottom, and probably get away with putting the entire script contents in that file, also eliminating the endless-loop problem.
Hello TB0ne, Thank you for the reply. I'm afraid though that I either do not understand your solution or you misunderstood my question - I do not mean some random script I made up, I'm talking about a rather unfortunately named program named 'script' that is part of bsdutils together with renice, wall, scriptreplay and logger. The problem, as I see it, is that whenever 'script' is run, it opens another shell. So if I have an instruction in bashrc to run script, this new shell reads it and starts another script, which opens another shell, which opens another script and so on. "Inception". So basically it seems that I need to have Yakuake/Konsole calling /usr/bin/script instead of /bin/bash when opening a new tab. Or did I not understand your answer?
As I understood, script "script" launches a new bash shell. What if you start it like "bash --norc which script
arg1 arg2" instead of "script arg1 arg2"?
Quote: Originally Posted by 10110111 As I understood, script "script" launches a new bash shell. What if you start it like "bash --norc which script
arg1 arg2" instead of "script arg1 arg2"? In this case I get: Code:
/usr/bin/script: /usr/bin/script: cannot execute binary file
Quote: Originally Posted by klearview Hello TB0ne, Thank you for the reply. I'm afraid though that I either do not understand your solution or you misunderstood my question - I do not mean some random script I made up, I'm talking about a rather unfortunately named program named 'script' that is part of bsdutils together with renice, wall, scriptreplay and logger. The problem, as I see it, is that whenever 'script' is run, it opens another shell. So if I have an instruction in bashrc to run script, this new shell reads it and starts another script, which opens another shell, which opens another script and so on. "Inception". So basically it seems that I need to have Yakuake/Konsole calling /usr/bin/script instead of /bin/bash when opening a new tab. Or did I not understand your answer? No, I misunderstood...my apologies. I didn't see BSD mentioned, and didn't know there was an actual program named "script", or what it did. So, totally ignore what i said.
Well, OK. You can make a simple script like the following, and make terminal emulator run it instead of /bin/bash. Code:
| #!/bin/bash script arg1 arg2
|
# do what you want bash # and run an interactive shell In this case you wouldn't need to set up .bashrc .
I did it! I did it! On my own! So if someone wants to record their terminal sessions automatically (including SSH sessions(!)) using script utility here is how: Add the following line at the end of /etc/bash.bashrc file (or to .bashrc in your home if you only want your own sessions to be recorded): Code:
| test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f $HOME/$(date +"%d-%b-%y_%H-%M-%S")_shell.log)
|
That's all! (assuming you have script installed, of course - script is part of bsdutils package) Now when you open new terminal you'll see: Code:
Script started, file is /home/username/file_name.log
What this does: We test for Code:
- here we get a parent process of the shell we've started. Code:
- we strip all the unnecessary info to end up with 'script'. If the test is true we exit, if not - we run script: Code:
| script -f $HOME/$(date +"%d-%b-%y_%H-%M-%S")_shell.log
|
- script will write your sessions to a file in your home directory naming them something like '30-Nov-11_00-11-12_shell.log'. Adjust this part to suit yourself - for example, you can append your sessions to one large file rather than creating a new one for every session with '
| script -a /path/to/single_log_file
|
'; or you can adjust where the files are written to - say, to /var/log/script/$USER_$(date +"%d-%b-%y_%H-%M-%S")_shell.log (make sure you've actually created /var/log/script and made it writable by others). The thread title is somewhat misleading now as this solution is terminal-agnostic and will work with everything not just Yakuake/Konsole. Should be something like:'How to automatically record all your terminal sessions with script utility'?
OK, die beiden letzten Zitate sind ziemlich ähnlich, aber...: "Doppelt gemoppelt hält eben besser!!!"