Selsames Verhalten wenn exe als Dienst läuft

  • hi leute,

    ich zerbreche mir schon eine ganze weile den kopf wo mein fehler liegen kann.
    dieser auszug aus meinem sricpt (backup lösung für ein erp system) ist als dienst auf einem datenbankserver eingerichtet. es lauft . was aber schwierigkeiten macht ist die erkennung der wochentag.
    wenn der dienst am tag 2 der woche also montag neugestartet wird dann werden alle tag(montag -sonntag) gesichert obwohl sonabend und sonntag ausgeschlossen sind. wird der dienst am tag 7 der woche gestartet dann wird nur sonnabend und sonntag gesichert.woran mag das liegen? hat das etwas damit zu tun das es als dienst eingetragen ist?
    ich hoffe ich habe es verständlich rüber gebracht mein problem.

    mfg thomasb

    Spoiler anzeigen
    [autoit]

    #NoTrayIcon
    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_Outfile=..\..\..\
    #AutoIt3Wrapper_Res_Comment=Datensicherung , T
    #AutoIt3Wrapper_Res_Description=
    #AutoIt3Wrapper_Res_Fileversion=3.0.0.44
    #AutoIt3Wrapper_Res_FileVersion_AutoIncrement=y
    #AutoIt3Wrapper_Res_LegalCopyright=
    #AutoIt3Wrapper_Res_Language=1031
    #AutoIt3Wrapper_res_requestedExecutionLevel=highestAvailable
    #Tidy_Parameters=/ sfc
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

    [/autoit] [autoit][/autoit] [autoit]

    #include "date.au3"
    #include "file.au3"
    #include "Array.au3"
    #include "String.au3"
    #include <Process.au3>
    #include "ServiceControl.au3"
    ;
    ;Opt("WinWaitDelay", 50)
    Opt("WinTitleMatchMode", 4)
    Opt("WinDetectHiddenText", 1) ;0=don't detect, 1=do detect
    Opt("WinSearchChildren", 1) ;0=no, 1=search children also
    ;
    $file = "C:\Datensicherung\nav.zup"; Startdatei
    $vas = "C:\Datensicherung\CDR.ini" ; Datei für CDR Export an Ecotel
    $fehler = StringLeft(IniRead($file, "Server", "004", "error"), 14) & _NowDate() & StringRight(IniRead($file, "006", "009", ""), 12); bei Fehler
    $sFeiertag = "C:\" & @YEAR & ".zup" ; Feiertagsdatei mit variablen Feiertagen
    ;
    Global $pathMain = IniRead($file, "002", "001", "Nichts gefunden")
    Global $pathCompare = IniRead($file, "002", "002", "Nichts gefunden")
    Global $PROTFILE = IniRead($file, "006", "008", "Nichts gefunden")
    Global $mainFile_A
    Global $compare_A
    Local $dates
    ;Mail
    Global $oMyRet[2]
    Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
    ;Mail
    Dim $HDays[32], $year = @YEAR
    $iWeekday = _DateToDayOfWeek(@YEAR, @MON, @MDAY)
    $sNewDate = _DateAdd('d', -1, _NowCalcDate()); ein Tag
    $sNewDate = StringReplace($sNewDate, "/", ".")
    $sNewData = _DateAdd('d', -2, _NowCalcDate()); zwei Tage
    $sNewData = StringReplace($sNewData, "/", ".")
    $sNewStart = _DateAdd('d', -3, _NowCalcDate()); drei Tage
    $sNewStart = StringReplace($sNewStart, "/", ".")

    [/autoit] [autoit][/autoit] [autoit]

    $Ti = @HOUR & ":" & @MIN & ":" & @SEC
    $new = @MDAY + 1 & "." & @MON & "." & @YEAR
    $sDatum = @YEAR & "." & @MON & "." & @MDAY

    [/autoit] [autoit][/autoit] [autoit]

    $var = IniReadSection($vas, "001")
    $ftp = IniRead($vas, "006", "001", "Nichts gefunden")
    $user = "xxx"
    $pw = "xxx"
    $user_network = "xxx"
    $pw_Network = "xxx"
    $LPath = ""

    [/autoit] [autoit][/autoit] [autoit]

    $f = StringLeft(IniRead($file, "006", "008", "Nichts gefunden"), 3) & _NowDate() & StringRight(IniRead($file, "006", "014", ""), 5)
    $drive1 = IniReadSection($file, "Laufwerke")
    $drive2 = IniReadSection($file, "Server")
    ;Mail
    $s_SmtpServer = IniRead($file, "Mailroot", "001", "Nichts gefunden")
    $s_FromName = IniRead($file, "Mailroot", "002", "Nichts gefunden")
    $s_FromAddress = IniRead($file, "Mailroot", "003", "Nichts gefunden")
    $s_ToAddress = IniRead($file, "Mailroot", "004", "Nichts gefunden")
    $s_Subject = IniRead($file, "Mailroot", "005", "Nichts gefunden");IniRead($file, "Mailroot", "5", "Nichts gefunden")
    $as_Body = IniRead($file, "007", "001", "Nichts gefunden")
    $s_AttachFiles = StringLeft(IniRead($file, "006", "008", "error"), 13) & StringRight(IniRead($file, "006", "009", ""), 4)
    $s_CcAddress = ""
    $s_BccAddress = IniRead($file, "Mailroot", "006", "Nichts gefunden")
    $s_Username = $user
    $s_Password = $pw
    $IPPort = 25
    $ssl = 0
    ;Mail
    While 1
    If _isFeiertag($sFeiertag) = 1 Then
    MsgBox(64, "Heute ausführen?", "Nein es ist Feiertag", 10)
    Else
    If $iWeekday = 2 Then ; Montag
    _Netzlaufwerk()
    Sleep(15000)
    _Robocopy()
    Sleep(15000)
    DirRemove(IniRead($file, "Laufwerke", "003", "error") & "\" & $sNewStart, 1)
    Sleep(15000)
    _Netzlaufwerk()
    ElseIf $iWeekday = 3 Then ; Diensatg
    _Netzlaufwerk()
    Sleep(15000)
    _Robocopy()
    Sleep(15000)
    DirRemove(IniRead($file, "Laufwerke", "003", "error") & "\" & $sNewStart, 1)
    Sleep(15000)
    _Netzlaufwerk()
    ElseIf $iWeekday = 4 Then ; Mittwoch
    _Netzlaufwerk()
    Sleep(15000)
    _Robocopy()
    Sleep(15000)
    DirRemove(IniRead($file, "Laufwerke", "003", "error") & "\" & $sNewData, 1)
    Sleep(15000)
    _Netzlaufwerk()
    ElseIf $iWeekday = 5 Then ; Donnerstag
    _Netzlaufwerk()
    Sleep(15000)
    _Robocopy()
    Sleep(15000)
    DirRemove(IniRead($file, "Laufwerke", "003", "error") & "\" & $sNewData, 1)
    Sleep(15000)
    _Netzlaufwerk()
    ElseIf $iWeekday = 6 Then ; Freitag
    _Netzlaufwerk()
    Sleep(15000)
    _Robocopy()
    Sleep(15000)
    DirRemove(IniRead($file, "Laufwerke", "003", "error") & "\" & $sNewData, 1)
    Sleep(15000)
    _Netzlaufwerk()
    EndIf
    Sleep(1500)
    If $iWeekday = 5 Then
    ;
    EndIf
    EndIf
    Sleep(100)
    WEnd
    ;
    Func _Netzlaufwerk()
    If Not FileExists(IniRead($file, "Laufwerke", "001", "")) Then
    For $i = 1 To $drive1[0][0]
    DriveMapAdd($drive1[$i][1], $drive2[$i][1], 0, $user_network, $pw_Network)
    Sleep(100)
    Next
    If _FileCreate($f) = 1 Then IniWrite($file, "007", "001", "Datensicherung wurde erfolgreich gestartet")
    Sleep(500)
    FileWriteLine(IniRead($file, "006", "008", "Nichts gefunden"), "Laufwerke wurden verbunden um " & _NowTime() & @CRLF)
    Sleep(500)
    $rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_AttachFiles, $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl)
    Else
    For $i = 1 To $drive1[0][0]
    DriveMapDel($drive1[$i][1])
    Sleep(100)
    Next
    IniWrite($file, "007", "001", "Datensicherung wurde erfolgreich beendet")
    Sleep(500)
    FileWriteLine(IniRead($file, "Server", "001", "Nichts gefunden") & StringRight(IniRead($file, "006", "008", "Nichts gefunden"), 16), "Laufwerke wurden getrennt um " & _NowTime() & @CRLF)
    $rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_AttachFiles, $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl)
    EndIf
    EndFunc ;==>_Netzlaufwerk
    ;
    Func _Robocopy(); Verschiebt die erstellten Dateien für Printshop
    $begin = TimerInit()
    $ro = _RunDOS("C:\Datensicherung\robocopy D:\Backup \\Server\datensicherung\" & $sDatum & " /MIR *.fbk")
    $dif = TimerDiff($begin)
    $dif = $dif / 60000
    FileWriteLine(IniRead($file, "006", "008", "Nichts gefunden"), "Die Datensicherung wurde in " & Round($dif, 1) & " Minuten kopiert")
    Sleep(1000)
    If ProcessClose("robocopy.exe") Then
    If $iWeekday = 5 Then
    MsgBox(0, $iWeekday, "nichts tun", 5)
    Else
    FileRecycle("D:\Backup\*.fbk")
    EndIf
    EndIf
    EndFunc ;==>_Robocopy
    ;
    Func _isFeiertag($sFeiertag) ; Feiertag
    Local $dates
    If Not _FileReadToArray($sFeiertag, $dates) Then Return -1
    For $i = 1 To $dates[0]
    If StringLeft($dates[$i], 10) = _NowDate() Then
    Return 1
    EndIf
    Next
    Return 0
    EndFunc ;==>_isFeiertag
    ;
    Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
    $objEmail = ObjCreate("CDO.Message")
    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress
    Local $i_Error = 0
    Local $i_Error_desciption = ""
    If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
    If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
    $objEmail.Subject = $s_Subject & " " & _Now()
    If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
    $objEmail.HTMLBody = $as_Body & IniRead($file, "007", "001", "Nichts gefunden")
    Else
    $objEmail.Textbody = IniRead($file, "007", "001", "Nichts gefunden") & @CRLF
    EndIf
    If $s_AttachFiles <> "" And FileExists(StringLeft(IniRead($file, "006", "008", "Nichts gefunden"), 13) & _NowDate() & StringRight(IniRead($file, "006", "009", ""), 5)) Then
    Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
    For $x = 1 To $S_Files2Attach[0]
    $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
    If FileExists($S_Files2Attach[$x]) Then
    $objEmail.AddAttachment($S_Files2Attach[$x])
    Else
    $i_Error_desciption = $i_Error_desciption & @LF & 'File not found to attach: ' & $S_Files2Attach[$x]
    SetError(1)
    Return 0
    EndIf
    Next
    EndIf
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    ;Authenticated SMTP
    If $s_Username <> "" Then
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
    EndIf
    If $ssl Then
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
    ;Update settings
    $objEmail.Configuration.Fields.Update
    ; Sent the Message
    $objEmail.Send
    If @error Then
    SetError(2)
    Return $oMyRet[1]
    EndIf
    EndFunc ;==>_INetSmtpMailCom
    ; Com Error Handler
    Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $oMyRet[0] = $HexNumber
    $oMyRet[1] = StringStripWS($oMyError.description, 3)
    ConsoleWrite("### COM Error ! Number: " & $HexNumber & " ScriptLine: " & $oMyError.scriptline & " Description:" & $oMyRet[1] & @LF)
    SetError(1); something to check for when this function returns
    Return
    EndFunc ;==>MyErrFunc
    ;
    Func _JahrMonatstring()
    If @MON = 1 Then
    $monat = 12
    $jahr = @YEAR - 1
    Return $jahr & "\" & $monat & " " & $jahr
    EndIf
    Return @YEAR & "\" & StringFormat("%02d", @MON - 1) & " " & @YEAR
    EndFunc ;==>_JahrMonatstring
    ;
    Func _Monatsstring()
    If @MON = 1 Then
    $monat = 12
    $jahr = @YEAR - 1
    Return $monat & " " & $jahr
    EndIf
    Return StringFormat("%02d", @MON - 1) & " " & @YEAR
    EndFunc ;==>_Monatsstring
    ;
    Func _Feiertage($year)
    Dim $4AdvDat, $3AdvDat, $2AdvDat, $1AdvDat, $TotSoDat, $BuBDat, $MutterDat, $ErnteDat, $tmp, $Delimiter = "\"
    ;fixe Feiertage
    $HDays[0] = $year & "/01/01" & $Delimiter & "Neujahr"
    $HDays[1] = $year & "/01/06" & $Delimiter & "Heilige Drei Könige"
    $HDays[2] = $year & "/02/14" & $Delimiter & "Valentinstag"
    $HDays[3] = $year & "/05/01" & $Delimiter & "Maifeiertag"
    $HDays[4] = $year & "/10/03" & $Delimiter & "Tag der Deutschen Einheit"
    $HDays[5] = $year & "/10/31" & $Delimiter & "Reformationstag"
    $HDays[6] = $year & "/11/01" & $Delimiter & "Allerheiligen"
    $HDays[7] = $year & "/12/24" & $Delimiter & "Heiligabend"
    $HDays[8] = $year & "/12/25" & $Delimiter & "1. Weihnachtsfeiertag"
    $HDays[9] = $year & "/12/26" & $Delimiter & "2. Weihnachtsfeiertag"
    $HDays[10] = $year & "/12/31" & $Delimiter & "Silvester"
    ;variable Feiertage
    $aa = Mod($year, 19)
    $bb = Mod($year, 4)
    $cc = Mod($year, 7)
    $H1 = Int($year / 100)
    $H2 = Int($year / 400)
    $N = 4 + $H1 - $H2
    $MM = 15 + $H1 - $H2 - Floor(Int((8 * $H1 + 13) / 25))
    $dd = Mod((19 * $aa + $MM), 30)
    $ee = Mod((2 * $bb + 4 * $cc + 6 * $dd + $N), 7)
    If $dd + $ee = 35 Then
    $Easter = 50
    Else
    If $dd = 28 And $ee = 6 And $aa > 10 Then
    $Easter = 49
    Else
    $Easter = 22 + $dd + $ee
    EndIf
    EndIf
    If $Easter < 32 Then
    $EasterDay = $Easter
    $EasterMonth = "03"
    Else
    $EasterDay = $Easter - 31
    $EasterMonth = "04"
    EndIf
    If $EasterDay < 10 Then
    $EasterDay = "0" & $EasterDay
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $year < 1900 Then ;Datumsoperationen nur mgl. wenn > 1900 , Jahr wird konvertiert
    $RestJahr = Mod($year, 100)
    If _DateIsLeapYear($year) Then
    If $RestJahr < 10 Then
    $RestJahr = "0" & $RestJahr
    EndIf
    $Tempyear = 20 & $RestJahr
    Else
    If $RestJahr < 10 Then
    $RestJahr = "0" & $RestJahr
    EndIf
    $Tempyear = 19 & $RestJahr
    EndIf
    $EasterDate = $Tempyear & "/" & $EasterMonth & "/" & $EasterDay
    Else
    $EasterDate = $year & "/" & $EasterMonth & "/" & $EasterDay
    EndIf
    $WFastDate = _DateAdd('d', -52, $EasterDate)
    $RosDat = _DateAdd('d', -48, $EasterDate)
    $FastDat = _DateAdd('d', -47, $EasterDate)
    $AschDat = _DateAdd('d', -46, $EasterDate)
    $GrDoDat = _DateAdd('d', -3, $EasterDate)
    $KarDat = _DateAdd('d', -2, $EasterDate)
    $OSaDat = _DateAdd('d', -1, $EasterDate)
    $OSoDat = $EasterDate
    $OMoDat = _DateAdd('d', 1, $EasterDate)
    $HiFaDat = _DateAdd('d', 39, $EasterDate)
    $PfSoDat = _DateAdd('d', 49, $EasterDate)
    $PfMoDat = _DateAdd('d', 50, $EasterDate)
    $FroDat = _DateAdd('d', 60, $EasterDate)

    [/autoit] [autoit][/autoit] [autoit]

    ;Ermitteln nicht von Ostern abhängiger, veränderlicher Feiertage
    ;Muttertag = 2. Sonntag im Mai ABER wenn Pfingsten = 2.Sonntag im Mai dann ist Muttertag am 1. Sonntag
    ;Der 2. Sonntag kann nur zw. dem 8. u. 14.5. liegen
    For $maitag = 8 To 14
    If _DateToDayOfWeek($year, 5, $maitag) = 1 Then
    If $maitag < 10 Then
    $maitag = "0" & $maitag
    EndIf
    $MutterDat = $year & "/05/" & $maitag
    If $MutterDat = $PfSoDat Then
    $MutterDat = _DateAdd('d', -7, $year & "/05/" & $maitag)
    EndIf
    ExitLoop
    EndIf
    Next

    [/autoit] [autoit][/autoit] [autoit]

    ;Erntedankfest 1. Sonntag im Oktober (zw. 1. u. 7.10.)
    For $oktobertag = 1 To 7
    If _DateToDayOfWeek($year, 10, $oktobertag) = 1 Then
    $oktobertag = "0" & $oktobertag
    $ErnteDat = $year & "/10/" & $oktobertag
    ExitLoop
    EndIf
    Next
    ;4.Advent = Sonntag vor 25.12. (zw. 18. u. 24.12.)
    For $deztag = 18 To 24
    If _DateToDayOfWeek($year, 12, $deztag) = 1 Then
    $4AdvDat = $year & "/12/" & $deztag
    $3AdvDat = _DateAdd('d', -7, $4AdvDat)
    $2AdvDat = _DateAdd('d', -14, $4AdvDat)
    $1AdvDat = _DateAdd('d', -21, $4AdvDat)
    $TotSoDat = _DateAdd('d', -28, $4AdvDat)
    $BuBDat = _DateAdd('d', -32, $4AdvDat)
    ExitLoop
    EndIf
    Next
    $HDays[11] = $WFastDate & $Delimiter & "Weiberfastnacht"
    $HDays[12] = $RosDat & $Delimiter & "Rosenmontag"
    $HDays[13] = $FastDat & $Delimiter & "Fastnacht"
    $HDays[14] = $AschDat & $Delimiter & "Aschermittwoch"
    $HDays[15] = $GrDoDat & $Delimiter & "Gründonnerstag"
    $HDays[16] = $KarDat & $Delimiter & "Karfreitag"
    $HDays[17] = $OSaDat & $Delimiter & "Ostersamstag"
    $HDays[18] = $OSoDat & $Delimiter & "Ostersonntag"
    $HDays[19] = $OMoDat & $Delimiter & "Ostermontag"
    $HDays[20] = $HiFaDat & $Delimiter & "Christi Himmelfahrt"
    $HDays[21] = $PfSoDat & $Delimiter & "Pfingstsonntag"
    $HDays[22] = $PfMoDat & $Delimiter & "Pfingstmontag"
    $HDays[23] = $MutterDat & $Delimiter & "Muttertag"
    $HDays[24] = $FroDat & $Delimiter & "Fronleichnam"
    $HDays[25] = $ErnteDat & $Delimiter & "Erntedankfest"
    $HDays[26] = $BuBDat & $Delimiter & "Buß- und Bettag"
    $HDays[27] = $TotSoDat & $Delimiter & "Totensonntag"
    $HDays[28] = $1AdvDat & $Delimiter & "1. Advent"
    $HDays[29] = $2AdvDat & $Delimiter & "2. Advent"
    $HDays[30] = $3AdvDat & $Delimiter & "3. Advent"
    $HDays[31] = $4AdvDat & $Delimiter & "4. Advent"
    _ArraySort($HDays)
    ;Datum konvertieren zu TT.MM.JJJJ
    For $i = 0 To 31
    $tmp = StringSplit($HDays[$i], $Delimiter)
    $HDays[$i] = StringMid($tmp[1], 9) & "." & StringMid($tmp[1], 6, 2) & "." & StringMid($tmp[1], 1, 4) & $Delimiter & $tmp[2]
    Next
    Return $HDays
    ;
    _FileWriteFromArray($sFeiertag, $HDays)
    EndFunc ;==>_Feiertage

    [/autoit]

    MFG ThomasB

    *
    Ich lese viel und gerne (und wenn ich alle Mahnungen durch habe, schmeiße ich sie weg)
    *

    Einmal editiert, zuletzt von bernd670 (27. Januar 2009 um 22:19)

    • Offizieller Beitrag

    Hallo,

    das liegt wohl daran das die Datum/Zeit-Variablen (Zeile 41-52) nur einmal beim Starten des Dienstes berechnet werden und dann immer mit dem gleichen Werten gerechnet wird. Du musst die Berechnung der Werte am Anfang der While-Schleife durchführen dann sollte es gehen.