다음과 같은 터미널을 사용하여 동시에 여러 명령의 출력을 동일한 파일로 파이프하고 있습니다.
cmd1 | tee -a /tmp/file
cmd2 | tee -a /tmp/file
cmd3 | tee -a /tmp/file
사용하는 것이 안전합니까? 이 방법으로 데이터 손실 또는 읽기 / 쓰기 권한 문제가 있습니까?
편집 : 출력이 섞여도 괜찮습니다. 모든 것이 파일에 기록되는지 확인하고 싶습니다. 두 명령이 정확히 같은 시간에 파일에 출력을 쓰려고하면 충돌이 발생합니까?
append
( -a
) 모드 와 함께 tee를 사용 하면 추가 작업이없는 파일 시스템 (예 : NFS)을 제외하고 데이터 손실 위험이 없습니다. 에서 append
모드 tee
으로 파일이 열립니다 O_APPEND
플래그.
$ echo 1 | strace tee -a /tmp/file 2>&1 | grep open | grep /tmp/file
openat(AT_FDCWD, "/tmp/file", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
에서 man 2 openat
:
O_APPEND The file is opened in append mode. Before each write(2), the file offset is positioned at the end of the file, as if with lseek(2). The modification of the file offset and the write operation are performed as a single atomic step.
핵심 문장 은 파일 오프셋 수정 및 쓰기 작업이 단일 원자 단계로 수행됩니다 . 의 write()
인스턴스에 대한 모든 호출 tee
은 파일 오프셋을 파일 끝에 원자 적으로 배치 한 다음 데이터를 씁니다.
openat
매뉴얼 페이지 사용이 패턴 (앞에서 언급 한 바와 같이) NFS 파일 시스템에 안전하지 않습니다 유의을 수행합니다
O_APPEND may lead to corrupted files on NFS filesystems if more than one process appends data to a file at once. This is be‐ cause NFS does not support appending to a file, so the client kernel has to simulate it, which can't be done without a race condition.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다