Quote:
Originally Posted by BS Support
hello
how make corn to corn to optimize and repair tables every day for more good server without sql problems
???
|
I believe you meant cron jobs, and since this was such an awesome question, and people every where can benefit from the information, I will provide help =)
First, everyone should be aware of what Cron is. Cron is the name of program that enables unix users to execute commands or scripts (groups of commands) automatically at a specified time/date. It is normally used for sys admin commands, like makewhatis, which builds a search database for the man -k command, or for running a backup script,
but can be used for anything. A common use for it today is connecting to
the internet and downloading your email. The unix version of Cron was written by Paul Vixie. What you'll find below is kind of a C# version of this.
Now, the specific cron you are refering to is known as the Cron scheduler and is implemented in a single class, CronService.Cron (CronService is the namespace I thought up for this quick little tutorial, but you may want to change this into the default namespace for your Cron application).
You can start an instance of this class using the start() method, which simply starts the scheduler and never returns. For instance, you can link the code below with the Cron class to create a regular Windows executable (for use in Windows' Startup Programs folder, for example):
Code:
using System;
namespace CronService
{
class CronMain
{
[STAThread]
static void Main(string[] args)
{
Cron cron = new Cron ();
cron.start ();
}
}
}
The Cron Service is mainly used to create a Windows Service, and learning to do that is beyond the scope of this answer, so I'll provide you with a link on how to do that
.
There is only one small issue with using this service: the OnStart method that you will implement here, is expected to return after 20 seconds or so, so you will have to start the perpetually running scheduler in a separate thread.
The example below provides you with a very stripped down version of doing just that. Please use this code only as a hint when creating your own service.
Code:
using System.Threading;
namespace CronService
{
public class CronService : System.ServiceProcess.ServiceBase
{
private Cron cron;
private Thread thr;
public RecoveryService()
{
cron = new Cron ();
thr = new Thread ( new ThreadStart (cron.start));
thr.Name = "cron";
}
protected override void OnStart(string[] args)
{
thr.Start ();
}
protected override void OnStop()
{
thr.Abort();
}
}
}
Usage:
Quote:
# DoThis every hour
0 * * * * C:\Program\ Files\MyProg\DoThis.exe arg1 arg2
# DoThat -- well, you figure out when :-)
8-9/2 12,13,14-18 * * * C:\Program\ Files\MyProg\DoThat.exe
|
Hope this helps ya.