18

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

2
  • 5
    LOL, :wq like I were in vim'
    – j0h
    Mar 3, 2016 at 13:33
  • 1
    it boggles my mind that most people seem to do that instead of :x
    – user428517
    Nov 28, 2017 at 20:41

2 Answers 2

20

There is a very easy and handy way:

Using script to make typescript of terminal session

  1. Start the command script

    If the argument file is given, eg script ~/tmp/output, script saves the dialogue in this file. If no filename is given, the dialogue is saved in the file typescript

  2. Start your script or what ever you want to start

  3. If your script is finished, stop script via Ctrl-D

  4. Check 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 scriptinside 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).

6
  • 4
    @Fabby I like my answer, but I don't see the little gray thing ;)
    – A.B.
    Jul 30, 2015 at 17:43
  • I use script logfilename, so I get to pick it.
    – waltinator
    Jul 30, 2015 at 20:23
  • can I call "script" from my shell program?
    – j0h
    Jul 30, 2015 at 21:24
  • 1
    This makes no sense, see my improved answer.
    – A.B.
    Jul 31, 2015 at 5:05
  • 1
    +1. Thanks for this answer :-) script is useful also a monitoring via a fifo. From man script: "-f, --flush Flush output after each write. This is nice for telecooperation: one person does mkfifo foo; script -f foo, and another can supervise real-time what is being done using cat 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.
    – sudodus
    Jan 3, 2019 at 23:47
18

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .