나는 bash 스크립팅을 처음 접한다는 점부터 시작하겠습니다. 저는 "kiltslog.txt"라는 텍스트 파일에 기록되는 콘텐츠를 네트워크의 다른 디렉터리로 리디렉션하는 작업을 맡았습니다. 참고: 다음 스크립트는 작성하지 않았습니다. 10분마다 실행됩니다... 위의 로그 파일이 다음 스크립트에서 어떻게 생성/업데이트되는지 전혀 모릅니다. 한 가지 더... "kiltslog.txt" 파일은 10분마다 업데이트되며 "echo StartExec:"로 시작하는 모든 "echo" 문은 위의 로그 파일에 기록됩니다. 또한 스크립트 어디에도 "kiltslog.txt" 파일에 대한 참조가 없습니다.
#!/bin/bash
start_ts=`date +%s`
out_date=$(date -d @$start_ts --rfc-3339 ns)
echo StartExec: $out_date $start_ts
# min free space, in KB
# equal to 4TB
minSpaceThreshold=4294967296
echo "Checking free space..."
myUsed=$(df -k /nielsen_extracts | tail -1 | awk '{print $3}')
## this can be collapsed into one awk function here:
df -k /nielsen_extracts | tail -1 | /bin/awk '{if (int ($3)>4294967296) print "Enough space: " $3;else print "Not enough space " $3 }'
if [[ $myUsed -gt $minSpaceThreshold ]] ;
then
echo "We have enough free space:" $myUsed
else
echo "We do not have enough free space:" $myUsed
exit
fi
start_ts=`date +%s`
out_date=$(date -d @$start_ts --rfc-3339 ns)
echo DEVStartExec: $out_date $start_ts
# Use a lockfile containing the pid of the running process
# If script crashes and leaves lockfile around, it will have a different pid so
# will not prevent script running again.
#
lf=/tmp/pidLockFileKiltsProd
# create empty lock file if none exists
cat /dev/null >> $lf
read lastPID < $lf
# if lastPID is not null and a process with that pid exists , exit
[ ! -z "$lastPID" -a -d /proc/$lastPID ] && exit
echo not running
# save my pid in the lock file
echo $$ > $lf
nielsen_extracts_root=/nielsen_extracts
app_data_root=/nielsen_extracts/KiltsFilesRequests/AppData
requests_root=/nielsen_extracts/KiltsFilesRequests/AppData/Requests
queue_root=/nielsen_extracts/KiltsFilesRequests/AppData/Requests/queue
processing_root=/nielsen_extracts/KiltsFilesRequests/AppData/Requests/processing
complete_root=/nielsen_extracts/KiltsFilesRequests/AppData/Requests/complete
request_name=
request_id=
request_user=
request_parent_path=
tmp_curl_url=
tmp_curl_param_NewStatus=
##ls $requests_root
# check queue for requests
##echo $(date +Y%m%d)
# tar request in scratch space
#find $requests_root/queue -maxdepth 1 -type d -print
DIRECTORIES=$(find $queue_root -mindepth 1 -type d)
for d in $DIRECTORIES
do
echo "Processing $d directory..."
echo "Moving $d from queue to processing directory..."
#cat $d/requestinfo | awk 'NR==2' | awk BEGIN { FS = ': " };
request_id=$(cat $d/requestinfo | awk 'BEGIN { FS = ":" } ; { print $2 }' | awk 'NR==1' | sed -e 's/^ *//g' -e 's/ *$//g')
request_name=$(cat $d/requestinfo | awk 'BEGIN { FS = ":" } ; { print $2 }' | awk 'NR==2' | sed -e 's/^ *//g' -e 's/ *$//g')
request_user=$(cat $d/requestinfo | awk 'BEGIN {FS = ":" } ; {print $2}' | awk 'NR==3' | sed -e 's/^ *//g' -e 's/ *$//g')
request_parent_path=$(cat $d/requestinfo | awk 'BEGIN {FS = ":" } ; {print $2}' | awk 'NR==4' | sed -e 's/^ *//g' -e 's/ *$//g')
request_id="$(echo $request_id | tr '[a-z'] '[A-Z]')"
echo "request_id: $request_id"
echo "request_name: $request_name"
echo "request_user: $request_user"
echo "request_parent_path: $request_parent_path"
echo "Updating status on front-end to PROCESSING..."
tmp_curl_url="https://kiltsfiles.chicagobooth.edu/Services/UpdateRequestStatus.aspx?RequestID="
tmp_curl_url="$tmp_curl_url$request_id"
tmp_curl_url="$tmp_curl_url&NewStatus=P"
echo "curling $tmp_curl_url ..."
curl $tmp_curl_url | grep updated
#exit
cd $d
pwd
cd ..
pwd
echo "Moving request data to processing..."
mv $request_id/ ../processing
#mv $d $processing_root
# create the subfolder in scratch
echo "Creating subdirectory in scratch /mnt/kiltGlobus/scratch/$request_id"
mkdir -p /mnt/kiltGlobus/scratch/$request_id
#tar the file list
echo "Running tar process with cvz args on request..."
tar cvz -T /nielsen_extracts/KiltsFilesRequests/AppData/Requests/processing/$request_id/filelist -f /mnt/kiltGlobus/scratch/$request_id/$request_name.tgz
#exit
echo "tar complete"
echo "Moving request data to complete..."
cd $processing_root
pwd
mv $request_id/ ../complete
#move to the globus endpoint
mkdir /mnt/kiltGlobus/RMS/$request_user
echo "Moving file to Globus endpoint (RMS)..."
mv /mnt/kiltGlobus/scratch/$request_id/$request_name.tgz /mnt/kiltGlobus/RMS/$request_user
#finish with email notification and front-end update
echo "Updating status on front-end to COMPLETE..."
tmp_curl_url="https://kiltsfiles.chicagobooth.edu/Services/UpdateRequestStatus.aspx?RequestID="
tmp_curl_url="$tmp_curl_url$request_id"
tmp_curl_url="$tmp_curl_url&NewStatus=C"
echo "curling $tmp_curl_url ..."
curl $tmp_curl_url | grep updated
echo "Cleaning up scratch dir..."
rm -rf /mnt/kiltGlobus/scratch/$request_id
done
end_ts=`date +%s`
out_date=$(date -d @$end_ts --rfc-3339 ns)
echo EndExec: $out_date $end_ts
ts_diff=$(($end_ts-$start_ts))
echo ExecTime: $ts_diff
답변1
스크립트의 표준 출력은 파일로 다시 라우팅될 수 있습니다.
thescript >/path/to/kiltslog.txt
따라서 이름(크론 작업 등)을 변경해야 합니다.