在 Windows 中设置 cron 作业

我每天都要从 SFTP 服务器上下载一个文件。我有一个从服务器检索文件的程序,但我想设置一个 cron 作业(或类似的任何东西)来实现自动化。我们是一个 Windows 商店,需要在 Windows 中设置 cron 作业。

262069 次浏览

http://windows.microsoft.com/en-US/windows7/schedule-a-task

maybe that will help with windows scheduled tasks ...

There's pycron which I really as a Cron implementation for windows, but there's also the built in scheduler which should work just fine for what you need (Control Panel -> Scheduled Tasks -> Add Scheduled Task).

The windows equivalent to a cron job is a scheduled task.

A scheduled task can be created as described by Alex and Rudu, but it can also be done command line with schtasks (if you for instance need to script it or add it to version control).

An example:

schtasks /create /tn calculate /tr calc /sc weekly /d MON /st 06:05 /ru "System"

Creates the task calculate, which starts the calculator(calc) every monday at 6:05 (should you ever need that.)

All available commands can be found here: http://technet.microsoft.com/en-us/library/cc772785%28WS.10%29.aspx

It works on windows server 2008 as well as windows server 2003.

  1. Make sure you logged on as an administrator or you have the same access as an administrator.
  2. Start->Control Panel->System and Security->Administrative Tools->Task Scheduler
  3. Action->Create Basic Task->Type a name and Click Next
  4. Follow through the wizard.

If you don't want to use Scheduled Tasks you can use the Windows Subsystem for Linux which will allow you to use cron jobs like on Linux.

To make sure cron is actually running you can type service cron status from within the Linux terminal. If it isn't currently running then type service cron start and you should be good to go.

I would like to thank @Vincent Stevenson, @s-hunter

Go to Control Panel --> Administrative Tools --> Task Scheduler--> Create Task

  1. Task Scheduler, Create Task

  2. Give the Task a title

  3. Go to Actions

  4. Go to CMD to find the path,

    Python, import sys, sys.executable

    (this tells you what the Program/script field should be populated with: "some path with Appdata mostly")

    like:C:\Users\admin\AppData\Local\Programs\Python\Python38-32\python.exe

  5. Arguments: name of the python script (like run.py)

  6. Start in: dir location of python script (like:C:\Users\admin\Documents\my_python_project)

  7. Go to Triggers, schedule as you like

  8. Test the script by running it

There are also cmdlets in powershell for this:

https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/new-scheduledtask?view=windowsserver2022-ps#example-2-define-a-scheduled-task-with-multiple-actions

The linked example:

PS C:\> $actions = (New-ScheduledTaskAction -Execute 'foo.ps1'), (New-ScheduledTaskAction -Execute 'bar.ps1')
PS C:\> $trigger = New-ScheduledTaskTrigger -Daily -At '9:15 AM'
PS C:\> $principal = New-ScheduledTaskPrincipal -UserId 'DOMAIN\user' -RunLevel Highest
PS C:\> $settings = New-ScheduledTaskSettingsSet -RunOnlyIfNetworkAvailable -WakeToRun
PS C:\> $task = New-ScheduledTask -Action $actions -Principal $principal -Trigger $trigger -Settings $settings


PS C:\> Register-ScheduledTask 'baz' -InputObject $task