Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

5) فایل‌های تنظیمات

فایل run-loop.ps1

...

Code Block
languagebash
titleکد فایل PowerShell
collapsetrue
$exePath = "D:\SMSSender\iMaster.ERP.SmsSender.Console.exe"
$processName = "iMaster.ERP.SmsSender.Console"
$intervalSeconds = 60
$logFile = "D:\SmsService\runner.log"

while ($true) {
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

    try {
        $alreadyRunning = Get-Process -Name $processName -ErrorAction SilentlyContinue

        if (-not $alreadyRunning) {
            Add-Content $logFile "[$timestamp] Starting $exePath"

            $process = Start-Process -FilePath $exePath -PassThru
            $process.WaitForExit()

            $exitCode = $process.ExitCode
            Add-Content $logFile "[$timestamp] Finished with exit code $exitCode"
        }
        else {
            Add-Content $logFile "[$timestamp] Process already running. Skipped."
        }
    }
    catch {
        Add-Content $logFile "[$timestamp] ERROR: $($_.Exception.Message)"
    }

    Start-Sleep -Seconds $intervalSeconds
}

...

فایل فایل SmsRunnerService.xml

...

Code Block
languagebash
titleکد فایل XML سرویس
collapsetrue
<service>
  <id>SmsRunnerService</id>
  <name>SMS Runner Service</name>
  <description>Runs iMaster.ERP.SmsSender.Console.exe every 1 minute</description>

  <executable>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</executable>
  <arguments>-NoProfile -ExecutionPolicy Bypass -File "D:\SmsService\run-loop.ps1"</arguments>

  <logpath>D:\SmsService\logs</logpath>
  <log mode="roll-by-size">
    <sizeThreshold>10240</sizeThreshold>
    <keepFiles>8</keepFiles>
  </log>

  <startmode>Automatic</startmode>

  <onfailure action="restart" delay="10 sec"/>
  <onfailure action="restart" delay="30 sec"/>
  <onfailure action="restart" delay="1 min"/>
</service>

...

6) تست فعال بودن سرویس

روش 1: بررسی در Services

...