{"record":{"id":"563322a48aa4f4d0","repo":"jdx/mise","slug":"schtasks-failed-printed","errorCode":null,"errorMessage":"`schtasks {}` failed: {printed}","messagePattern":"`schtasks (.+?)` failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/system/scheduled_tasks.rs","lineNumber":407,"sourceCode":"        }\n        std::fs::write(&staging, &rendered)?;\n        if let Err(err) = schtasks(&create).await {\n            let _ = std::fs::remove_file(&staging);\n            return Err(err);\n        }\n        // written, not renamed: a rename does not replace an existing\n        // definition on Windows\n        std::fs::write(&path, &rendered)?;\n        let _ = std::fs::remove_file(&staging);\n        if end_first {\n            // it may have exited between the query and now: the HRESULT\n            // says so in every locale; the message is matched as a fallback\n            let (status, printed) = schtasks_output(&end).await?;\n            if !status.success()\n                && status.code() != Some(SCHED_E_TASK_NOT_RUNNING)\n                && !end_error_is_noop(&printed)\n            {\n                bail!(\"`schtasks {}` failed: {printed}\", shell_words::join(&end));\n            }\n        }\n        if start {\n            schtasks(&run).await?;\n        }\n    }\n    Ok(())\n}\n\n/// Delete the task mise registered for `name`. Returns whether one existed.\npub(crate) async fn remove_task(name: &str, dry_run: bool) -> Result<bool> {\n    let task = task_name(name);\n    let path = definition_path(name);\n    if !exists(name).await? {\n        if path.exists() && !dry_run {\n            std::fs::remove_file(&path)?;\n        }\n        return Ok(false);","sourceCodeStart":389,"sourceCodeEnd":425,"githubUrl":"https://github.com/jdx/mise/blob/533346cc374382b41ec5ff70536252b2e96e725c/src/system/scheduled_tasks.rs#L389-L425","documentation":"Applying start/stop changes to a Windows scheduled task shells out to schtasks; when the end (stop) invocation exits non-zero — and it is neither the benign SCHED_E_TASK_NOT_RUNNING code nor a locale-independent 'no-op' message — the apply step fails with the exact command and its printed output.","triggerScenarios":"Calling apply to stop (or restart) a scheduled task when `schtasks /end ...` returns a non-zero status other than 0x41306 (task not running) and its output doesn't match the known no-op error text.","commonSituations":"Task name typo or wrong path in schtasks arguments; task deleted by another process between checks; insufficient privileges to end a task running as another user; corrupted task definition.","solutions":["Read the printed schtasks output in the message for the root cause (access denied, task not found, etc.)","Verify the task name/path with `schtasks /query`","Re-run with elevated privileges if access was denied","If the task is genuinely not running, treat it as no-op and skip the /end call"],"exampleFix":"// before\napply(stop(\"MyTask\")) // fails: ERROR: Access is denied.\n// after: run apply with an account that has rights on the task, or\nschtasks /query /tn \"MyTask\" // confirm exact name first","handlingStrategy":"try-catch","validationCode":"const q = await run('schtasks', ['/query', '/tn', taskName]);\nif (q.status !== 0) throw new Error(`task ${taskName} not queryable: ${q.stdout}`);","typeGuard":null,"tryCatchPattern":"try { await apply(change); } catch (e) { if (String(e).includes('`schtasks') ) { logSchtasksOutput(e.message); if (isAccessDenied(e.message)) requireElevation(); else if (isTaskNotFound(e.message)) recreateTask(); } else throw e; }","preventionTips":["Confirm task names/paths with `schtasks /query` before applying start/stop changes","Run apply under an account with rights on the task's security descriptor","Check whether the task is running before issuing /end; treat not-running as success"],"tags":["windows","schtasks","scheduled-tasks","process"],"backgroundTag":"command-failed","analyzedSha":"533346cc374382b41ec5ff70536252b2e96e725c","analyzedAt":"2026-09-17T13:35:38.149Z","contentChangedAt":"2026-09-17T13:35:38.149Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}