0

So I am trying to use the script command to always log the output of my commands (this is for work); however, when I place the script command in my ~/.zshrc file (since I'm using zshell), it seems that it continuously runs when I open zsh.

So here's what my ~/.zshrc file looks like towards the bottom:

LOGDATE=$(date +"%m_%d_%Y_%H_%M_%S_%p.tlog")
script $LOGDATE

and then when I open a new terminal, this is what I get:

Script started, file is 01_16_2019_07_15_05_AM.tlog
Script started, file is 01_16_2019_07_15_05_AM.tlog
Script started, file is 01_16_2019_07_15_05_AM.tlog
Script started, file is 01_16_2019_07_15_05_AM.tlog
Script started, file is 01_16_2019_07_15_05_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_06_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_07_AM.tlog
Script started, file is 01_16_2019_07_15_08_AM.tlog
Script started, file is 01_16_2019_07_15_08_AM.tlog
Script started, file is 01_16_2019_07_15_08_AM.tlog
Script started, file is 01_16_2019_07_15_08_AM.tlog
Script started, file is 01_16_2019_07_15_08_AM.tlog
Script started, file is 01_16_2019_07_15_08_AM.tlog
Script started, file is 01_16_2019_07_15_08_AM.tlog
Script started, file is 01_16_2019_07_15_08_AM.tlog
Script started, file is 01_16_2019_07_15_08_AM.tlog
Script started, file is 01_16_2019_07_15_08_AM.tlog
Script started, file is 01_16_2019_07_15_08_AM.tlog

and this runs until I hold ctrl-c to stop it from doing it.

What am I doing wrong?

4
  • Stack Overflow's scope is limited to questions about writing code. For UNIX tool configuration or use, consider Unix & Linux or SuperUser. Commented Jan 16, 2019 at 15:19
  • That said, what you're doing wrong is starting script from your rc. That file is sourced when a shell starts, and when you run script, it... starts a new shell to log the output of. Generally speaking, it shouldn't be involved from a rc file at all, but if you are going to do that, you need guardrails to prevent recursion. Commented Jan 16, 2019 at 15:20
  • Ahhh... I gotcha. Makes sense. Not sure why I didn't even think of that, smh! Much appreciated Commented Jan 16, 2019 at 15:20
  • 1
    Frankly, there are other tools built more to purpose for logging console sessions for security or auditing purposes that don't require cooperation of the user for whom that logging takes place; I'd be selecting a different tool altogether. Commented Jan 16, 2019 at 15:22

1 Answer 1

1

I don't advise using script in shell init files at all (and our sister site Unix & Linux Stack Exchange is a better fit for a wider question about what to use instead), but a way to avoid your narrow issue is to use an environment variable as a sigil that logging is already taking place, and avoid starting a new session should it be set:

if ! [ -n "$script_log" ]; then
    script_log=$(date +"%m_%d_%Y_%H_%M_%S_%p.tlog")
    export script_log
    script "$script_log"
fi
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Found another solution that I ended up using here unix.stackexchange.com/questions/25639/…, but I appreciate your help as well!
The ps-grepping approach is pretty awful -- I'd personally avoid that one in favor of something based on auditd (particularly if you care about the commands run more than their output; using auditd to trace execve syscalls will track commands run through means that don't generate a shell or any console-level trace) or sysdig (the sysdig approach lets you run just one process to trace every session on your system, or at least, every session that matches your filters).
Gotcha. Sounds great. I appreciate the input and the solution here man! Going to go with this one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.