简体   繁体   中英

How can I run a Rake task as a Windows “scheduled task”?

I have a rake task I need to run as a daily job on a Windows XP machine. I discovered the Windows "scheduled tasks" control panel. I'm trying to figure out how to get it to run my Rake task.

Two of the fields are "run" (with a browse button) and "start in". I tried to enter rake mycategory:mytask into "run" and my Rails project dir containing the Rake task into "start in". The result was a message saying the task "could not start".

How can I get set up a Windows "scheduled task" to run a Rake task?

If you can build a batch file that can execute it properly I would do so, and then you can direct the batch file to run with the task.

Something like this should work:

:: Run task every hour
@call rake stats RAILS_ENV="production"

Also, aside from the (correct) batch file advice above, AFAIK, you may need to run the task on an account that has a non-empty password set. Property of Scheduler.

Just adding an updated/fleshed out answer for those interested...

Create a file called rake.bat - make sure to save with ANSI encoding (the default with Windows Notepad). You can save this file anywhere, but I put it in C:\\ror\\rake.bat

rake.bat

@call bundle exec rake %*

Now when you create a scheduled task, you can set it to run the .bat file and the arguments are simply what comes after rake . Set it to run inside the directory of your choice. Run whether or not the user is logged in, but do NOT run with the highest privileges. Screenshots below for clarity; my rake task is namespaced manager , and the task itself is sync :

编辑操作对话框

编辑任务对话框

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM