Batch (.bat)
किसी भी job के चारों ओर एक .bat wrapper; curl.exe इस्तेमाल करता है, जो Windows 10 और 11 में पहले से है। Job का output एक temporary फ़ाइल में जाता है; failure पर उसकी आखिरी 20 lines alert के साथ जाती हैं।
Code
यह वह code है जो Code टैब बनाता है (start और errors पर निशान लगाकर)। App में इसमें पहले से आपके job का अपना पता होता है।
@echo off
REM Save as C:\jobs\taskbeacon-run.bat
REM Task Scheduler > Action: Start a program
REM Program: C:\jobs\taskbeacon-run.bat
REM Runs your job, then reports success or failure from its exit code.
set "TB=https://taskbeacon.io/ping/your-job-id"
curl.exe -fsS -m 10 "%TB%/start" >nul
REM --- your job here (a program, a script, another .bat with call): replace the path ---
REM its output goes to a temporary file: shown below, and its last 20 lines go with a failure
set "TB_LOG=%TEMP%\taskbeacon-%RANDOM%.log"
call "C:\path\to\your-job.bat" > "%TB_LOG%" 2>&1
set TB_RC=%ERRORLEVEL%
type "%TB_LOG%"
type nul > "%TB_LOG%.tail"
if not "%TB_RC%"=="0" powershell.exe -NoProfile -Command "Get-Content -LiteralPath $env:TB_LOG -Tail 20 | ForEach-Object { if ($_.Length -gt 300) { $_.Substring(0, 300) } else { $_ } }" > "%TB_LOG%.tail" 2>nul
REM the exit code goes at the end of the address: /0 = OK, any other number = failed
curl.exe -fsS -m 10 --data-binary "@%TB_LOG%.tail" -H "Content-Type: text/plain" "%TB%/%TB_RC%" >nul
del "%TB_LOG%" "%TB_LOG%.tail" 2>nul
exit /b %TB_RC%यह कैसे काम करता है
Job शुरू होने पर .bat /start ping करता है और खत्म होने पर वह address, जिसके अंत में उसका exit code होता है: /0 OK है, कोई और नंबर failure, जो output की आखिरी 20 lines के साथ भेजा जाता है। यह row count नहीं भेजता।
किसी हिस्से को छोड़ने के लिए Code टैब पर विकल्पों का निशान हटाएँ; कोई output न भेजकर सिर्फ़ error या exit code भेजने के लिए Send output lines (output लाइनें भेजें) का निशान हटाएँ। देखें क्या रिपोर्ट किया जा सकता है।
Schedule पर चलाएँ
Code को C:\jobs\taskbeacon-run.bat नाम से save करें, जहाँ your job here लिखा है वहाँ अपने job का command रखें, और एक task बनाएँ (एक line, Command Prompt में):
schtasks /Create /TN "Nightly export" /SC DAILY /ST 02:00 /TR "C:\jobs\taskbeacon-run.bat"
अगर आपका job relative paths इस्तेमाल करता है, तो Task Scheduler में Start in को अपने job के folder पर सेट करें।
आम समस्याएँ
- Failed job OK दिखता है। Wrapper
callके बाद वाले command का exit code भेजता है। अगर वह कोई दूसरी .bat है, तो कुछ गड़बड़ होने पर उसेexit /b 1(या कोई और संख्या) से ख़त्म होना चाहिए; .bat अपने आख़िरी command के code के साथ ख़त्म होती है। - curl.exe नहीं मिला। यह Windows 10 और 11 में पहले से मौजूद है। पुराने Windows पर किसी
curl.exeका पूरा path दें। - % का चिह्न गायब हो जाता है। .bat के अंदर literal
%को%%लिखना होता है। - हाथ से चलाने पर चलता है, schedule पर नहीं। Run whether user is logged on or not पर tick करें, और mapped drive letters की जगह UNC paths (
\server\share\…) इस्तेमाल करें।
उपयोग के उदाहरण: Task Scheduler Last Run Result 0x0, लेकिन script fail हुई। और सवाल: FAQ.