![오전 12시까지 소스로부터 파일을 받지 못한 경우 경고 이메일을 보내는 UNIX 스크립트는 수신된 파일이 0바이트인 경우 경고 이메일을 보냅니다.](https://linux55.com/image/160937/%EC%98%A4%EC%A0%84%2012%EC%8B%9C%EA%B9%8C%EC%A7%80%20%EC%86%8C%EC%8A%A4%EB%A1%9C%EB%B6%80%ED%84%B0%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%EB%B0%9B%EC%A7%80%20%EB%AA%BB%ED%95%9C%20%EA%B2%BD%EC%9A%B0%20%EA%B2%BD%EA%B3%A0%20%EC%9D%B4%EB%A9%94%EC%9D%BC%EC%9D%84%20%EB%B3%B4%EB%82%B4%EB%8A%94%20UNIX%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EB%8A%94%20%EC%88%98%EC%8B%A0%EB%90%9C%20%ED%8C%8C%EC%9D%BC%EC%9D%B4%200%EB%B0%94%EC%9D%B4%ED%8A%B8%EC%9D%B8%20%EA%B2%BD%EC%9A%B0%20%EA%B2%BD%EA%B3%A0%20%EC%9D%B4%EB%A9%94%EC%9D%BC%EC%9D%84%20%EB%B3%B4%EB%83%85%EB%8B%88%EB%8B%A4..png)
저는 매일 받는 파일 수와 파일 크기를 확인하는 자동화된 프로세스를 찾고 있습니다. 현재 위치로 이동하여 확인하여 수동으로 이 작업을 수행하고 있습니다. 항상 하는 일이라 지루한 일이에요. 누구든지 여기에서 나를 안내할 수 있나요? 내가 실행할 때 나 자신과 다른 사람들에게 이메일을 보내는 서버의 쉘 스크립트일 수 있습니다.
또한 우리 끝에서 0바이트를 수신하는 경우 파일 이름을 언급해야 합니다.
우리가 매일 받는 문서:
test_file1_20190919_20190918.txt.gz
test_file2_20190919_20190918.txt.gz
test_file3_20190919_20190918.txt.gz
test_file4_20190919_20190918.txt.gz
test_file5_20190919_20190918.txt.gz
test_file_20190919_20190918.mfst
sample_abc1_20190919_20190918.txt.gz
sample_abc2_20190919_20190918.txt.gz
sample_abc_20190919_20190918.mfst
매일 수동으로 확인할 필요가 없도록 하루에 9개의 파일이 수신되는지 확인하고, 그렇지 않은 경우 소스 팀에 이러한 파일을 보내도록 알려야 합니다.
날짜는 어떻게 확인하나요? 우리 스크립트는 매일 /folder1/folder2/folder3/ 아래에 날짜(yyyymmdd) 디렉터리를 생성하고 파일은 오늘 날짜 디렉터리에 수신됩니다. 예: 경로: /folder1/folder2/folder3/ 20190918 20190919 --> 날짜 디렉터리는 9월 18일과 19일입니다.
파일 이름은 항상 동일합니까? 동일하지만 날짜가 다른가요? 완전히 다를 수 있습니까? 파일 패턴은 항상 동일하게 유지되지만 날짜는 매일 변경되며 제가 언급한 패턴을 벗어난 파일은 수신되지 않습니다. 예: test_file1_20190919_20190918.txt.gz (파일은 20190919 폴더로 수신됩니다) test_file1_20190918_20190917.txt.gz (파일은 20190918 폴더로 수신됩니다)
9개가 아닌 10개의 파일을 받으면 어떻게 되나요? 그게 문제인가요? 예상치 못한 이름의 파일이 9개 있으면 어떻게 되나요? 예, 문제가 될 수 있습니다.
장소:/folder1/folder2/folder3/
어떤 도움이라도 대단히 감사하겠습니다 :)
답변1
... cronjob으로 실행하는 것과 같은 것?
#!/bin/bash
address="[email protected]"
basepath=/folder1/folder2/folder3
err=0
expected=9
msg=''
subject='Filecheck'
today=$(date +%Y%m%d)
null_files="$(find ${basepath}/${today} -type f -size 0)"
null_files="$(printf ' %s\n' $null_files )"
count=-1 # '.' counts. One-off-error :-)
# if there are restricions on the filenames,
# replace this with an appropriate find-command
# and iterate over that instead...
for item in ${basepath}/${today}/* ; do
((count++))
done
[[ $count -eq $expected ]] && msg='[ OK ] Count of files as expected\n\n'
[[ $count -gt $expected ]] && \
msg="[ERROR] Count of files too BIG: ${count}\n\n" ; err=1
[[ $count -lt $expected ]] && \
msg="[ERROR] Count of files too SMALL: ${count}\n\n" ; err=1
if [[ -n "$null_files" ]]; then
msg+='[ERROR] Found empty files:\n'
msg+="$null_files"
err=1
else
msg+='[ OK ] No files with with 0 bytes found.'
fi
# echo -e "$msg"
[[ $err -ne 0 ]] && subject="[ERROR] $subject" || subject="[OK] $subject"
sendEmail -t "$address" -u "$subject" -m "$msg"
답변2
아래 스크립트를 사용해 보세요. 잘 작동합니다.
12시 이후에 스크립트를 실행해주세요.
#!/bin/bash
d=`date +%Y-%m-%d -d "1 days ago"`
#echo $d
echo "Below are list of files present"
find . -maxdepth 1 -type f -iname "*.txt" -newermt $d| sed "s/\.\///g"
count=`find . -maxdepth 1 -type f -iname "*.txt" -newermt $d| sed "s/\.\///g"| wc -l`
if [ $count -eq 9 ]
then
echo "Total Number of file exsists in present directory is $count"
zero_sizefile=`find . -maxdepth 1 -type f -iname "*.txt" -newermt $d -size 0| wc -l`
if [ $zero_sizefile > 0 ]
then
echo "Below are zero sized files"
find . -type f -iname "*.txt" -newermt $d -size 0
else
echo "No files are zero sized files"
fi
else
echo "All files doesnt exists"
fi