How do I keep logs of all the work done via terminal? For example, I want a record of all the work done on database via terminal.
-
This is also built into sudo. – Wilhelm Erasmus Jul 08 '15 at 19:53
-
I have added my answer you can review – Mansur Ul Hasan Dec 08 '16 at 07:23
4 Answers
When you are ready to start recording a log file, type:
script screen.log
Now, until you stop the script, all input and output in the Terminal will be stored in screen.log. When you are done, just type:
exit
Your screen.log file will stored in the local directory. If you want to redirect it, use an absolute pathname such as ~/screen.log
. This will do exactly what you are looking for.

- 243
- 2
- 9

- 1,347
-
Hi, thanks for the reply. But will this work if I login to the mysql database from the shell using the following sequence:- 1) script screen.log 2)mysql -u
-p – Ankit Jul 10 '12 at 14:153) some work on the database 4) exit; I see that the file screen.log is created but it doesn't contain logs for the database. I'll share the file asap. Thanks Ankt -
2it should work with everything running in the console reading from "standard input" (usually your keyboard) and writing to standard output (usually your terminal window). So it should include the mysql command-line client. The man page (
man script
) even mentionsvi
(editor)... – Izzy Jul 10 '12 at 14:21
I have a better way to use syslog for logging every shell command this can be vary upon linux distribution but method will remain same
You need to follow some steps:
Step # 1 (Create Syslog service)
# vim /etc/rsyslog.d/bash.conf
local6.* /var/log/commands.log
Step # 2 (Open bashrc and enter below command)
# vim /root/.bashrc
# Enable CLI Logging by Mansur 08-12-2016
whoami="$(whoami)@$(echo $SSH_CONNECTION | awk '{print $1}')"export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$whoami [$$]: $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" ) [$RETRN_VAL]"'
Ste # 3 (Restart Syslog Service)
# /etc/init.d/rsyslog
Here is a log sample:
Dec 7 22:13:00 validationdb2 mahasan: root@export [13291]: tail -f /var/log/mysql/mysql.log [0] Dec 7 22:13:09 validationdb2 mahasan: root@export [13291]: ls -lh [0] Dec 7 22:13:27 validationdb2 mahasan: root@export [13291]: touch test command [0]

- 14,236
- 4
- 48
- 98

- 301
-
-
Hi Mansur, Could you please explain what you are doing with those commands, its difficult to understand, atleast to me. – Jasser Oct 16 '17 at 11:03
-
In Step 1 i am naming a service for command logging that will be used by process to send logs to it "local6" would be the end point for service for logging In Step 2 i am setting things what actually i want "PROMPT_COMMAND='RETRN_VAL=$?" this portion will record user's input and "logger -p local6.debug" this will send the log file to service which record logs data with the help syslog process – Mansur Ul Hasan Oct 16 '17 at 12:08
-
nhi will perfectly solve your problem. This tool automatically captures all potentially useful information about each executed command (as well as its output).
With nhi
you can easily retrieve any shell session (or terminal output) in its entirety, or only commands that you are interested in, by using an advanced querying mechanism provided by nhi
.
Note:
I am the creator of this tool. If you have questions, please feel free to ask.

- 11
-
hi @strang1ato could you please add the details on how to install and use
nhi
tool. Thanks. – Jags Nov 21 '21 at 00:38 -
-
1This is the same answer as posted here, https://unix.stackexchange.com/a/677683/97255, and https://unix.stackexchange.com/a/678401/97255. Whilst this is not strictly spam as you have disclosed affiliation, it is a bit odd to multiple post the same answer... – Greenonline Nov 21 '21 at 04:14
-
Would nhi allow me to log a session that leads to a system crash, so I cannot neatly close the logging process? – Rasmus Jan 02 '22 at 22:10
-
@Rasmus I am not sure what you mean, can you please describe more precisely your situation – strang1ato Jan 04 '22 at 19:44
-
@strang1ato, I have resolved the situation that needed this. My issue was that GPU crashed, and I wanted to monitor the GPU temperatures when it did so. But that left me without access to the terminal in which the monitoring was going on, so I wouldn't be able to end any processes there, except by killing them by hard booting machine. – Rasmus Jan 07 '22 at 13:58
-
@Rasmus nhi saves to db everything written to terminal by a process – strang1ato Jan 17 '22 at 21:44