Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

This is the last line in my .bashrc (lines breaks inserted for readability):

STARTTIME=`date +%F-%Hh-%Mm-%Ss-%N`; \
script -q -t 2> /home/USER/logs/$STARTTIME-timing.txt \
       -c 'bash --rcfile /home/USER/.bashrc-cp' \
       -f /home/USER/logs/$STARTTIME-log.txt; \
gzip -9 /home/USER/logs/$STARTTIME-timing.txt /home/USER/logs/$STARTTIME-log.txt; \
exit 0

There is a /home/USER/.bashrc-cp without this mentioned last line (but it's an exact copy of my .bashrc).

This terminal logging solution works great. There are only two problems:

Q1: If I exit the gnome-terminal with Alt+F4 then the logs aren't gzipped. Why? How can I gzip them in that case?

Q2: I don't want to use a .bashrc-cp file. Are there any solutions for it?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

When you ALT+F4 your terminal, it sends a SIGHUP to the shell. The shell then exits and sends a SIGHUP to everything running under that shell. Because the shell exits, it stops processing all commands, so everything after executing script isnt run.

The way to do this is to feed directly into gzip.

STARTTIME=`date +%F-%Hh-%Mm-%Ss-%N`
script -q -t -c 'bash --rcfile /home/USER/.bashrc-cp' \
-f >(nohup gzip -9 > /home/USER/logs/$STARTTIME-log.txt.gz) \
2> >(nohup gzip -9 > /home/USER/logs/$STARTTIME-timing.txt.gz)

What were doing here:
In bash, >(cmd) is special syntax that runs cmd and replaces >(cmd) with the path to a named pipe connected to cmd's STDIN. The nohup is needed so that when the shell quits, gzip doesnt get a SIGHUP and die. Instead it will get EOF on its STDIN so it can flush its buffer and then quit.

I'm not sure what .bashrc-cp is. If youre trying to avoid a recursive loop you can export STARTTIME (or some other variable) before launching script and then check for its existence. If it exists, dont launch script.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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