Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to redirect program error to a file while using pipes without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
I’m running a command on a Linux machine to backup my DB:
(echo "`date`: START DUMPING"; db_dump.sh; echo "`date`: DONE DUMPING") >>db_dump.log
And in the db_dump.sh:
pg_dump -v --dbname=mydb | pigz | openssl enc -aes128 -k ssecret -out db_dump.gz.aes
How would I catch the informative output from pg_dump, pigz and openssl to db_dump.log?
Currently I’m thinking doing it:
pg_dump -v --dbname=mydb 2>>db_dump.log| pigz 2>>db_dump.log| openssl enc -aes128 -k ssecret -out db_dump.gz.aes 2>>db_dump.log
but that seems quite cumbersome…
Answer
Try grouping with brackets:
( pg_dump -v --dbname=mydb | pigz | openssl enc -aes128 -k ssecret -out db_dump.gz.aes ) 2>>db_dump.log
Individual stderr output from all parts of the pipe will go into the same destination.
We are here to answer your question about How to redirect program error to a file while using pipes - If you find the proper solution, please don't forgot to share this with your team members.