While I type a command, this command will authenticate with a remote server and do a bunch of things. If pass, it will give me a bash shell with its environment.

My goal is to save these log which prints out into my terminal for future inspection. Let's say my command called target.

While I type target, it throws the following msg.

Version:        V1.94
Date:           Thu Jun  6 17:18:39 2019
OS:             CentOS release 6.10 (Final)

Notice the last line implied I've entered the target shell environment.

I've tried echo $(target) > output.log, but it will be stuck until I type CTRL+C.

Environment: CentOS 6.10


Let's say I want to make this script automatically run when I activate my machine. However, it will still be stuck while executing the target command because it entered a shell and didn't leave.

script -c target output.log
aws s3 cp "$FILENAME" s3://logs/

2 Answers 2


You may want to try script:

script -c target output.log

This would start the command target and save a transcript of the whole session, until target terminates, into the file output.log. If the command (your target) is several words long, quote the full command.

See the manual for script (man script).

  • Thanks, but is it possible to send terminates command after the target command? Since I need to send output.log to somewhere after that
    – rj487
    Jun 6, 2019 at 18:53
  • @CodaChang I'm not sure I understand what you mean. The script command will finish when your target command finishes, i.e. when you log out of the remote machine.
    – Kusalananda
    Jun 6, 2019 at 18:54
  • Hi, I had updated my questions. It should give you a better way to understand. Thanks
    – rj487
    Jun 6, 2019 at 19:07
  • @CodaChang It is not clear when you consider the transcript file (output.log) to be done.
    – Kusalananda
    Jun 6, 2019 at 19:13
  • It's quite hard to define whether it's done or not since it needs to connect with the remote server. Can I state 1 min timeout and exit it?
    – rj487
    Jun 7, 2019 at 16:01

If you can't change the target command itself, then this should be helpful:

target | tee output.bak

output.bak will contain a copy of all the output (just stdout in this example), so you'll have to trim it if you want just the initial info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.