ubuntuusers.de

bsdutils

Status: Gelöst | Ubuntu-Version: Nicht spezifiziert
Antworten |
Dieses Thema ist die Diskussion des Artikels bsdutils.

Yanneck

(Themenstarter)
Avatar von Yanneck

Anmeldungsdatum:
25. September 2009

Beiträge: 1216

Das o. g. gennante Beispiel mit script vielleicht? Zu logger fällt mir leider gar nix ein, da bin ich auch nicht so im Thema.

kaputtnik

Anmeldungsdatum:
31. Dezember 2007

Beiträge: 9245

linux_joy schrieb:

Terminalsitzung loggen (oder eben später zum dann fertigen Artikel) stehen.

Es wäre nett, wenn Du die ganzen Links zum nicht mehr vorhandenen Baustellenartikel Baustelle/Terminal-Sitzung loggen wieder aus den anderen Artikeln entfernst.

Links auf Baustellenartikel sind eh nicht gerne gesehen.

Weil der tote Link zB im Artikel Shell/Umleitungen (Abschnitt „Links“) zu finden ist, habe ich mich gerade halb totgesucht, um den Artikel Baustelle/Terminal-Sitzung loggen zu finden...

aasche

Anmeldungsdatum:
30. Januar 2006

Beiträge: 14259

kaputtnik schrieb:

Weil der tote Link zB im Artikel Shell/Umleitungen (Abschnitt „Links“) zu finden ist

Dort auskommentiert, bis dieser Artikel aus der Baustelle ist.

aasche

Anmeldungsdatum:
30. Januar 2006

Beiträge: 14259

Yanneck schrieb:

Das o. g. gennante Beispiel mit script vielleicht?

Wo wird die Logdatei gespeichert und wie heisst diese?

Yanneck

(Themenstarter)
Avatar von Yanneck

Anmeldungsdatum:
25. September 2009

Beiträge: 1216

Erledigt. ☺

aasche

Anmeldungsdatum:
30. Januar 2006

Beiträge: 14259

ok, dann sehe ich keinen Grund mehr, der gegen eine Veroeffentlichung im Wiki spricht. Danke an Yanneck fuer die Anregung zu script.

linux_joy

Anmeldungsdatum:
6. Februar 2008

Beiträge: 803

Wohnort: Hannover

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.

1
$ script
Script started, file is typescript

Do whatever … and press Ctrl-D to exit script.

1
$ col -bx <typescript >cleanedfile
1
$ vim cleanedfile

If you don't have script (for example, during the boot process in the initramfs), you can use following instead.

1
$ 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.

1
$ script

Script started, file is typescript

... do whatever ...

Ctrl-D

1
$ col -bx <typescript >savefile

1
$ vi savefile

The following can be used instead of script:

1
$ 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):

1
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

1
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

1
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:

1
#!/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:

1
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:

1
ps -ocommand= -p $PPID

- here we get a parent process of the shell we've started. Code:

1
awk '{print $1}'

- we strip all the unnecessary info to end up with 'script'. If the test is true we exit, if not - we run script: Code:

1
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 '

1
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!!!"

noisefloor Team-Icon

Anmeldungsdatum:
6. Juni 2006

Beiträge: 29567

Hallo,

@linus_joy: Danke für die Links, aber sowas bitte zukünfigt in eine Pastebin auslagern (z.B. http://paste.ubuntuusers.de/. Posts dieser Länge sind sonst total unleserlich...

Gruß, noisefloor

linux_joy

Anmeldungsdatum:
6. Februar 2008

Beiträge: 803

Wohnort: Hannover

Hallo,

nit Revision 11. Februar 2013 20:29 (Unterschied zur vorigen Version) wurde von Yanneck der folgende Passus im Abschnitt script verändert:

Von

/usr/bin/script -a -q ~/$USER

nach

exec /usr/bin/script -a -q ~/$USER

Während aber ersteres bei mir problemlos funktioniert, so kann ich mich bei der exec-Variante überhaupt nicht mehr grafisch am System anmelden, bzw. das System springt immer wieder zum Display-Manager zurück. Dasselbe Verhalten habe ich übrigens auch bei LMDE!

Yanneck

(Themenstarter)
Avatar von Yanneck

Anmeldungsdatum:
25. September 2009

Beiträge: 1216

Hi,

mit Displaymanager habe ich nicht getestet, nutze ich nicht. Vielleicht testest du mal, ob es ohne funktioniert. Den Abschnitt mit der Login-Shell könnte man noch um Angaben erweitern, wie es mit .Xresources bzw. .Xdefaults funktioniert, eine Login-Shell zu bekommen, nutzt ja nicht jeder das fade Gnome-Terminal. Bzw. könnte man - das heißt, ungetesterweise wäre das hier sowieso sorgfältiger untergebracht - darauf verweisen, die /etc/bash.bashrc zu verwenden. Loggen nach $HOME ist wahrscheinlich auch nicht die Musterlösung. Aber es war ja auch nur ein Beispiel. ☺

Yanneck

(Themenstarter)
Avatar von Yanneck

Anmeldungsdatum:
25. September 2009

Beiträge: 1216

Das mit der .bashrc habe ich jetzt mal probiert, das läuft irgendwie nicht.

linux_joy

Anmeldungsdatum:
6. Februar 2008

Beiträge: 803

Wohnort: Hannover

Yanneck schrieb:

Das mit der .bashrc habe ich jetzt mal probiert, das läuft irgendwie nicht.

Läuft bei mir auch nicht, ich habe aber ein paar Posts weiter oben schon mal die mögliche Lösung dazu zitiert (allerdings bisher NICHT selber getestet):

linux_joy schrieb:

Zur weiteren Vertiefung des Themas script sind hier noch ein paar (hoffentlich) anregende Links:

(...)

How to automatically record all your terminal sessions with script utility

(...)
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.

(...)
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. –

(...)
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):

1
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

1
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).

(...)
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

1
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

(...)

Antworten |