Running Drupal's cron.php without access to crontab
in

Drupal requires to run a scheduled task periodically. It is implemented as a php file named cron.php. If you have access to crontab on your hosting server, you just need to add cron.php to it. But what if you don't? There are a couple of ways to run cron.php without using cron on the server.

There's a module to do that, called Poormanscron (what a nice naming!)

Poormanscron

I should have tried the module. But there are some disadvantages using Poormanscron, though they are minor. With Poormanscron, if no one visit the site, the cron doesn't run. On the flip side, if there are many visitors, the overhead of Poormanscron may be significant.

What I've done is to write a program for Windows service (I'm a Windows programmer). Well, it's a practice for me to write some C# code, and that overcomes the disadvantages of Poormanscron.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Net;
using System.Text;

namespace MoroCronCall
{
    public
partial class MoroCronCallService : ServiceBase
   
{
        private
readonly Uri pollingTarget = new Uri("http://yourserver/cron_rss.php");
        private
readonly object lockObj = new object();
        public
MoroCronCallService()
        {
           
InitializeComponent();
        }

        protected
override void OnStart(string[] args)
        {
           
timer.Enabled = true;
        }

        protected
override void OnStop()
        {
           
timer.Enabled = false;

        }

        private
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
           
timer.Enabled = false;
           
timer.Interval = 60 * 60 * 4 * 1000; // 4 hours in milliseconds
           
lock (lockObj)
            {
                try
                {
                   
WebClient client = new WebClient();
                   
System.IO.Stream st = client.OpenRead(pollingTarget);
                   
System.IO.StreamReader sr =
                        new
System.IO.StreamReader(st, System.Text.Encoding.GetEncoding("utf-8"));
                   
string str = sr.ReadToEnd();

                   
st.Close();
                   
client.Dispose();

                   
eventLog.WriteEntry("Cron was run\n\n" + str, EventLogEntryType.Information, 4);

                }
                catch (
Exception ex)
                {
                   
eventLog.WriteEntry("Failed to update.\n" + ex.Message, EventLogEntryType.Error);
                   
timer.Interval = 30 * 60 * 1000; // retry in 30 mintues
               
}

            }
           
timer.Enabled = true;
        }
    }
}

My cron.php is modified as cron_rss.php. The reason is that cron.php doesn't return any feedback. So, I made it the modified version to return rss. The code is something like this.

// Clean up
variable_set('cron_busy', false);
variable_set('cron_last', time());
watchdog('cron', t('Cron run completed'));

node_feed(); // <-- Add this line

I've installed the Windows service to my computer, which is running 24/7. The disadvantage of this approach is, of course, you need to keep a Windows PC up and running all the time. If you don't mind doing so, this is one clean way to run cron almost the same way as crontab does.

http://drupal.org/project/codefilter

新しいコメントの投稿

このフィールドの内容は非公開にされ、公表されることはありません。
  • ウェブページアドレスとメールアドレスは、自動的にハイパーリンクに変換されます。
  • 使用できるHTMLタグ: <a> <em> <strong> <code> <cite> <ul> <ol> <li> <dl> <dt> <dd> <p>
  • 行と段落は自動的に折り返されます。
  • Amazon製品へのリンクを次の形式で作成することが出来ます。[amazon product_id inline|full|thumbnail]. 例: [amazon 1590597559 thumbnail]
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • 画像を追加することが出来ます。

書式オプションに関するより詳しい情報...

認証コード
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
画像で表示されている数字および記号を入力してください。