Home > Enterprise >  Create a schedule by the Schtask.exe
Create a schedule by the Schtask.exe

Time:05-04

I try to create a schedule, but I always get the error (exit status 0x80004005) when I run the

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    cmd := exec.Command("schtasks.exe", "/Create",
        "/SC ONLOGON",
        "/TN MyTask",
    )
    if err := cmd.Run(); err != nil {
        fmt.Println(err.Error())
    }
}

Even if I run it with administrator privileges, I still get the same result.

How do I solve it, or is there any other way?

CodePudding user response:

First of all, you must put every parameter into separate array items, and you must add the /TR paramteter to specify which program it should run:

cmd := exec.Command("schtasks.exe", "/Create",
    "/SC",
    "ONLOGON",
    "/TN",
    "MyTask",
    "/TR",
    "calc.exe",
)
if err := cmd.Run(); err != nil {
    fmt.Println(err.Error())
}
  • Related