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
There is a very easy and handy way:
Using
script
to make typescript of terminal sessionStart 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
The usage of
script
inside your script makes no sense becausescript
forks the shell or starts a new shell.if you normally run your script with
foo.sh
, try running it (assuming it's a bash script) withbash -x foo.sh
. If you want everything redirected to file, trybash -x foo.sh > file.log 2>&1
(note I'm redirecting stderr as well, remove the2>&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
.