Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible in bash to finish command piped through gzip gracefully?

Tags:

bash

gzip

I have startup script which calls command that produces a lot of output. To preserve space I gzipped its output:

#!/bin/bash

my_command 2>&1 | tee >(gzip --stdout > "1.log.gz")

Sadly, when I press Ctrl+C, gzip stops abruptly and compressed log gets damaged. Is there a way to finish command gracefully to get valid gz?

like image 657
Andrey Starodubtsev Avatar asked Nov 20 '25 00:11

Andrey Starodubtsev


1 Answers

I was unable to reproduce your problem, using "yes" as a substitute for your program -- that is

yes 2>&1 | tee >(gzip --stdout > "1.log.gz")

created perfectly valid gz file every time, so wondering if there is something else going on.

However you can isolate the two parts of the command by using named pipes, like this;

# create a named pipe
mknod mypipe p 

# start a background job that reads from the pipe
gzip --stdout > "1.log.gz" <mypipe &  

# now do work -- ctrl-c only affect this process
my_command 2>&1 | tee mypipe  
like image 150
Soren Avatar answered Nov 21 '25 23:11

Soren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!