How-To Run a Cron Job in Windows from the Command Prompt

Here is how you can Run a Cron Job on Windows from the Command Prompt

One of the advantages of having a PC over a mobile device like Android or iOS is the ability to really drill down and customize the PC to your liking. One of the biggest aspects of this customizability is access to the command-line and the incredible library of tools that come with it. We have covered some of these tools and utilities, from helping you find open ports to changing file extension associations and much more.

In this guide, we will take you the process running a Cron job or scheduling a task on Windows with the help of the Command Prompt (which can also be used for batch scripting). Our last article covered tasks scheduling using the Task Schedule utility that comes with Windows. However, that still involves a significant amount of manual work and clicks, from opening the software to picking your task type, duration, etc. and finally creating and running the task.

For power users, that is not an efficient option. Thus, in this guide, we will be covering how you can use the schtasks command in CMD to schedule tasks along with different parameters.

According to Windows, here is what you can do with this utility,

Schedules commands and programs to run periodically or at a specific time. Adds and removes tasks from the schedule, starts and stops tasks on demand, and displays and changes scheduled tasks.

It provides a total of 6 types of commands,

Here, we will demonstrate creating a command based on different parameters,

Using SCHTASKS in Command Prompt

schtasks /create /sc <ScheduleType> /tn <TaskName> /tr <TaskRun> [/s <Computer> [/u [<Domain>\]<User> [/p <Password>]]] [/ru {[<Domain>\]<User> | System}] [/rp <Password>] [/mo <Modifier>] [/d <Day>[,<Day>...] | *] [/m <Month>[,<Month>...]] [/i <IdleTime>] [/st <StartTime>] [/ri <Interval>] [{/et <EndTime> | /du <Duration>} [/k]] [/sd <StartDate>] [/ed <EndDate>] [/it] [/z] [/f]

This is the complete command with all it’s parameters. Now we will cover different parameters along with a few examples,

/sc <ScheduleType>

Specifies the schedule type. Valid values are MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, ONCE, ONSTART, ONLOGON, ONIDLE.

/tn <TaskName>

Specifies a name for the task. Each task on the system must have a unique name.

/tr <TaskRun>

Specifies the program or command that the task runs. With this parameter, you will give the path of the file/script which you want the system to run.

/mo <Modifier>

Specifies how often the task runs within its schedule type. This parameter is valid, but optional, for a MINUTE, HOURLY, DAILY, WEEKLY, and MONTHLY schedule. The default value is 1.

/d Day[,Day…] | *

Specifies a day (or days) of the week or a day (or days) of a month. Valid only with a WEEKLY or MONTHLY schedule.

/m Month[,Month…]

Specifies a month or months of the year during which the scheduled task should run. Valid values are JAN – DEC and * (every month). The /m parameter is valid only with a MONTHLY schedule.

/i <IdleTime>

Specifies how many minutes the computer is idle before the task starts. A valid value is a whole number from 1 to 999. This parameter is valid only with an ONIDLE schedule, and then it is required.

/st <StartTime>

Specifies the time of day that the task starts (each time it starts) in <HH:MM> 24-hour format. The default value is the current time on the local computer.

/ri <Interval>

Specifies the repetition interval in minutes. This is not applicable for schedule types: MINUTE, HOURLY, ONSTART, ONLOGON, and ONIDLE.

/et <EndTime>

Specifies the time of day that a minute or hourly task schedule ends in <HH:MM> 24-hour format. After the specified end time, schtasks does not start the task again until the start time recurs.

You can have a look at all the parameters along with their detailed specifications on Microsoft’s document on scheduling tasks over here.

Here are some examples for scheduling tasks,

Schedule a Task to Run Every 30 Minutes

schtasks /create /sc minute /mo 30 /tn My Task /tr c:\users\local\downloads\task.exe

Here we have specified the schedule with the mo parameter and given 30 minutes. The file is placed in Downloads and called task.exe, which is given along the tr parameter.

Schedule a Task to Run Every 7 Days

schtasks /create /tn My Task /tr c:\users\local\downloads\task.exe /sc daily /mo 7 /sd 01/01/2020

Here, we have specified the start date with the sd parameter but haven’t provided the start time – in this case, the current time of the PC will be given.

Schedule a Task to Run Every Monday

schtasks /create /tn My Task /tr c:\users\local\downloads\task.exe /sc weekly /d MON

In this case, we have created a task that will run weekly but only on a Monday, as specified with the d parameter.

You can also schedule a task to run only if you are logged in. For example, the following command will only run every 2 hours only if you are logged in.

schtasks /create /tn My Task /tr c:\users\local\downloads\task.exe /sc hourly /mo 2 /it
One more really useful option is the ability to schedule a task to run as soon as the system starts,
schtasks /create /tn My Task /tr c:\users\local\downloads\task.exe  /sc onstart
Similarly, you can use all these parameters to customize your task schedule to your liking and schedule it to run at any time you like.