2016-12-04 1 views
0

IF 문에 나열된 명령 중 하나를 수행하지 못하는 셸 스크립트에서 작은 문제가 1 개 있습니다.예약 된 작업으로 실행했을 때 UNIX 셸 스크립트가 실행되지 않습니다.

터미널 창을 열고 전체 스크립트를 \ 붙여 넣으면 스크립트는 100 % 작동합니다.

당신은 단지 그것을 호출하여 스크립트를 실행하는 경우, 모든 작동 당신이 맨 아래에 볼이 라인을 제외하고 :

는 에코 $ NEWID> $ FileID에

환경은 GitBASH를 실행하는 Windows Server 2012입니다. (그게 C :/파일 경로를 설명 할 것입니다)

"스크립트"로 실행했을 때만 스크립트가 그 코드를 터미널에 복사하여 붙여 넣는 것에 비해 왜 스크립트가 그 1 작업을 수행하지 못하는지에 대한 아이디어가 있습니까?


다음은 전체 스크립트입니다. 감사합니다

# Clean directory 
cd C:/Scripts/Shoretel_Export/ 
rm -f C:/Scripts/Shoretel_Export/output/*.csv 

# <---- Define: Date variables (not all dates used but available if needed \ just modify the wget string) ----> # 

twoDaysAgo=$(date +%Y-%m-%d -d '2 days ago') 
yesterday=$(date +%Y-%m-%d -d 'yesterday') 
today=$(date +%Y-%m-%d) 
tomorrow=$(date +%Y-%m-%d -d 'tomorrow') 

# <---- Define: Date timestamp variables for file naming ----> # 
stampTwoDaysAgo=$(date +%m-%d-%Y -d '2 days ago') 
stampTomorrow=$(date +%m-%d-%Y -d 'tomorrow') 

# <---- Define: Credentials variable ----> # 
account="--http-user=APIuser --http-password=APIpassword" 

# <---- Define: Export output directory variable ----> # 
sourceFile="C:/Scripts/Shoretel_Export/output/currentLastID.csv" 
output=$"C:/Scripts/Shoretel_Export/output/"${stampTwoDaysAgo}"_"${stampTomorrow}".csv" 
tempIDsource=$"C:/Scripts/Shoretel_Export/output/archive/"${stampTwoDaysAgo}"_"${stampTomorrow}".csv" 

# <---- Define: Export lastID variable ----> # 
callID=$(awk -F'","|^"|"$' 'BEGIN { max=0 } $1 > max { max=$1 } END {print max}' "C:/Scripts/Shoretel_Export/output/ID/ID.txt") 

# <---- Execute WGET function to export Shoretel calls from the specified date range ----> # 
wget --auth-no-challenge --no-check-certificate --output-document=${output} "https://portal.shoretelsky.com/DataExport/CDRData?startDate=${twoDaysAgo}&endDate=${tomorrow}&lastId=${callID}" ${account} 

# Perform empty file validation, if file is 0 kb the script will terminate and retry 
if [ -s "$output" ] 
then 
    echo "File is not empty continue to next IF statement" 
else 
    ./retry.sh 
fi 


#### At this point we should have a valid file 1 kb or larger. If the export contains 0 new records the script will terminate and the previous callID will remain #### 

# Define tempID to check for invalid or empty ID field 
tempID=$(awk -F'","|^"|"$' 'BEGIN { max=0 } $18 > max { max=$18 } END {print max}' $output) 

# Define ID paramaters and rename files 
fileID="C:/Scripts/Shoretel_Export/output/ID/ID.txt" 

if [ "$tempID" -gt "$callID" ] 
then 
    cp $output C:/Scripts/Shoretel_Export/output/archive/ 
    mv $output $sourceFile 
    newID=$(awk -F'","|^"|"$' 'BEGIN { max=0 } $18 > max { max=$18 } END {print max}' $tempIDsource) 
    echo $newID > $fileID 
    echo "Export and Timestamp complete" 
else 
    echo "Export contains 0 new call records \ the script will now terminate" 
fi 
+0

당신이 콘솔에서 실행할 때 사용하는 동일한 사용자를 사용하여 예약 된 작업으로 실행해야합니다. 또는'System' 사용자 (또는 예약 된 작업을 실행하는 모든 사용자가 Windows 시스템을 가지고 있지 않음)가 파일을 작성하는 데 필요한 권한을 가지고 있는지 확인하십시오. – axiac

+0

Windows 작업에서 문제가 있다는 것을 알고있었습니다. 나는 그것이 어떻게 작동하는지 보여주기 위해 질문을 업데이트 할 것이다. 의견을 보내 주셔서 감사합니다! – CWB

답변

0

확실하지 그 거 창에서 작동하지만 당신은 디버그 모드에서 스크립트를 실행하려고하는 경우는 :

#!/bin/bash -x 

그래서 당신은 일이 무엇인지 라인으로 라인을 확인할 수 있습니다. 또한이 스크립트의 출력을 표시 할 수 있습니까?

+0

스크립트의 출력은 호출 로그 .csv 파일입니다. 특별한 것은 없지만 기밀로 생각되는 정보가 들어있어 공유 할 수 없습니다. 나는 그것을 시도했다. 이미 터미널 창을 열고 "bash -x ./Export.sh"를 실행하면 스크립트가 아무런 문제없이 완료됩니다. 스크립트가 헤드리스로 실행될 때만 ECHO> 명령을 통해 1 .txt 파일 업데이트를 수행하지 못합니다. – CWB

1

해결 :문제는 Windows의 예약 된 작업 때문이었습니다. 여기 그것이 내가 작동하도록 한 것입니다.

작업 스케줄러> 일정을 수정할 : 이 cmd를 인수를 (선택 사항) 추가 : /K C : 프로그램> 프로그램/스크립트 시작 \ 스크립트 \ Shoretel_Export \ Shoretel-Export.sh & 출구


+0

질문이 :-) 인 경우 답변으로 표시해야합니다. – andlrc

관련 문제