How to track bash script - exit status and why it failed

Sometimes I need to run a command as follows:

cat file.txt | awk ' NR%4 == 2 { print $1 }' | sort | uniq -c | sort -gr >>output.txt &

on top of large files (size 2-32 GB). I start the team in the evening, and when I arrive in the morning, output.txt is sometimes empty and the process no longer works.

Please, how can I track what is happening? Why and when did my team fail? I know that the pipeline works because sometimes it ends successfully.

Thanks a lot!

UPDATE: Now I think that my process was killed, because the server, when I run this calculation, is only recommended for interactive use. If this is true, only one that I see from the log file - it was not successful - did not end.

Is there any way to find out that my process was actually killed? Thanks.

+4
source share
3 answers

First step, encapsulate that script in a file, and do not run it directly on the terminal (and lose the UUOC reward while we are on it).

#!/bin/bash

{
awk 'NR%4 == 2 { print $1 }' file.txt | sort | uniq -c | sort -gr >>output.txt
} 2>error.log

This captures all error messages in the file error.log. Then you can add diagnostic information.

#!/bin/bash

{
date >&2
set -x
awk 'NR%4 == 2 { print $1 }' file.txt | sort | uniq -c | sort -gr >>output.txt
date >&2
} 2>error.log

, . bash, , , , . , ( ), , ( , , , ).

script , output.txt, - error.log; script ( file.txt). , nohup & - .

file.txt ; . . . - script, .

+4

screen, pv tee, , , .

screen () , . , . screen, & CTRL-a,d. . , ( ) screen -r.

Furhtermore, cat pv (Pipe Viewer), :

pv -cN file.txt

-

611MB 0:00:11 [58.3MB/s] [=>      ] 15% ETA 0:00:59

, screen.

/ tee .

uniq -c | tee afteruniq.tmpfile | sort -gr

afteruniq.tmpfile uniq -c. , , . , tee 'd.

+2

You need to redirect both stderr, and stdoutto the output file.

Make your team as follows:

( awk 'NR%4 == 2{print $1}' file.txt| sort | uniq -c | sort -gr ) >> output.txt 2>&1 &

Pay attention to 2>&1to redirect stderrto where it goes stdout.

0
source

All Articles