Is there an easy way to log all activity that a shell script does?
Solution 1
if you normally run your script with foo.sh
, try running it (assuming it's a bash script) with bash -x foo.sh
. If you want everything redirected to file, try bash -x foo.sh > file.log 2>&1
(note I'm redirecting stderr as well, remove the 2>&1
if you don't want this). If you also want to see what's going on, bash -x foo.sh 2>&1 | tee file.log
.
Solution 2
There is a very easy and handy way:
Using script
to make typescript of terminal session
Start the command
script
If the argument
file
is given, egscript ~/tmp/output
,script
saves the dialogue in this file. If no filename is given, the dialogue is saved in the filetypescript
Start your script or what ever you want to start
If your script is finished, stop
script
via Ctrl-DCheck the output in the default output file
typescript
To start your command in one step with script
, use the parameter -c
-c COMMAND
Run the COMMAND rather than an interactive
shell. This makes it easy for a script to capture
the output of a program that behaves differently
when its stdout is not a tty.
The usage of script
inside your script makes no sense because script
forks the shell or starts a new shell.
If the variable SHELL exists, the shell forked by script will be that shell. If SHELL is not set, the Bourne shell is assumed. (Most shells set this variable automatically).
j0h
been using Linux since 2005. Ubuntu since whenever edgy eft was new. Lucid Lynx Ubuntu was the best Ubuntu I have ever used.
Updated on September 18, 2022Comments
-
j0h over 1 year
Is there an easy way to log all activity that occurs from a shell script to a file?
I have a script. It outputs things like echo "instructions", as well as other program output. I know the commands:
command | tee -a "$log_file"
and
command >> logifle.log
What I'm asking is whether there is a shell parameter for logging, or a set command I can use or something like that. I don't necessarily want to add dozens of redirects or tee to files if I don't have to. I still want to get std output though - I just also want it to be logged.:wq
-
Admin about 8 yearsLOL, :wq like I were in vim'
-
Admin over 6 yearsit boggles my mind that most people seem to do that instead of :x
-
-
A.B. almost 9 years@Fabby I like my answer, but I don't see the little gray thing ;)
-
goo almost 9 yearsI use
script logfilename
, so I get to pick it. -
j0h almost 9 yearscan I call "script" from my shell program?
-
A.B. almost 9 yearsThis makes no sense, see my improved answer.
-
j0h almost 9 yearsI would like to run the command from inside the script , I will look into script this evening.
-
sudodus over 5 years+1. Thanks for this answer :-)
script
is useful also a monitoring via a fifo. Fromman script
: "-f, --flush
Flush output after each write. This is nice for telecooperation: one person doesmkfifo foo; script -f foo
, and another can supervise real-time what is being done usingcat foo
". It can also be used to monitor when a program is waiting for input or done for example by monitoring the timestamp of the fifo.