Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

No cron, how to run a scheduled job?

Options
  • 12-11-2010 2:20pm
    #1
    Registered Users Posts: 4,475 ✭✭✭


    I've got a small site that sends out emails to people (not spam, all double opt in) but the actual sending process is a standard php script that I suspect will start to timeout as more and more people come on. I've contacted my host and cron is out for the time being, but I wonder if I can use my PC at home to "fake" a cron job. Basically put up a script somewhere that picks say 100 emails and sends them, marking them as sent. Run that every 10mins or whatever through my own windows scheduler. Have it run from dusk to dawn every day.

    I can't see any downside, but I'm sure there is. Is there a better solution for what I'm trying to do.


Comments

  • Moderators, Education Moderators, Home & Garden Moderators Posts: 8,174 Mod ✭✭✭✭Jonathan


    You could run cron on a separate host and have that call an obfusticated PHP url on your site to launch a script?


  • Registered Users Posts: 4,475 ✭✭✭corblimey


    Tks Jonathan, after posting my question, I stumbled across cronless.com which purports to do just that, although the free version only lets you run it twice a day. I'm thinking of that for testing purposes, and then find the 20 bucks somewhere to let me do it more often :D

    Have you or anyone else come across and/or used cronless.com or similar?


  • Registered Users Posts: 304 ✭✭PhantomBeaker


    For a server-side idea: fake your own cron.

    All it takes is a some paper, stickback plastic and some scissors (or an adult cut the appropriate bits for you).

    Warning - this solution is hairy. If you want an easy life, just ignore this post, and stump up for the software.

    Let's see... assuming you have the exec(), system() or shell_exec() php calls open to you, you could manage something (any of them will do). I've worked with systems that have implemented these sorts of hacks (and far more painful ones)

    If I was in perl, I'd fork and call system() in the child process. The child would use system() (which calls a program and NEVER returns) to run the payload script. As this is php we need a slightly different mechanism, but it'd work.

    The payload script would do the following.
    1/ perform something like "ps ux | grep $0 | grep -v $$" (checks processlist for a copy of itself, omitting its own process) - the ps command depends on your host (or if it's on windows, I've not a clue how to check that)
    2/ if it finds a copy of itself running, terminate.
    3/ Otherwise run in a loop, sleeping and firing off emails.

    The loop itself can go one of two ways:
    1/ sleep for X interval of time, where X is the amount of time you want to wait. then fire off your mails. As in, you want to send mails every 24 hours, and don't care about what time of day it's fired off.
    2/ If you want it to trigger at specific points in the day: sleep for about a minute, check the time. If the time is your target time, fire the emails.

    Solution 1 is easier to code.

    Now, because you're in php, all of the calls I mentioned expect the child script to return. So you need to write a wrapper. This wrapper does one thing:
    #!/bin/sh
    /path/to/payload_script.sh & # The & is important because payload_script isn't going to halt
    exit 0 # Give a good return code.
    

    Now all you have to do is call this wrapper in your php script.

    By the way, this method is all kinds of hairy. If your provider has deliberately disabled crontabsfor users, they won't be happy seeing this running (it's essentially a very limited and unoptimised cron daemon). It could get you into trouble with your provider. Basically, check with your provider if this is ok.

    As a safety precaution, you'll also need a mechanism to kill all payload scripts just in case you need to take it down/modify it. I'll leave that as an exercise to the reader.

    Basically, this might be easier than using your pc, and doesn't require stumping cash for software, but it's not a way to an easy life.

    Aoife


  • Registered Users Posts: 6,510 ✭✭✭daymobrew


    By the way, this method is all kinds of hairy. If your provider has deliberately disabled crontabs for users, they won't be happy seeing this running (it's essentially a very limited and unoptimised cron daemon). It could get you into trouble with your provider. Basically, check with your provider if this is ok.
    With this in mind I suggest asking your provider if they have an alternative to crontab. They might even turn on crontab for your account - you never know until you ask.


  • Registered Users Posts: 4,475 ✭✭✭corblimey


    Yeah, apparently crontab is available, but at a higher price than I'm willing to pay right now :D. If this kicks off the way it should, I'll be upgrading, but that's for later. PB's solution and dire warnings scare me, so I think I'll have to start small. wget looks a reasonable way of running a php script using windows scheduler to run it regularly.


  • Advertisement
  • Registered Users Posts: 1 amgadhs


    corblimey wrote: »
    Tks Jonathan, after posting my question, I stumbled across cronless.com which purports to do just that, although the free version only lets you run it twice a day. I'm thinking of that for testing purposes, and then find the 20 bucks somewhere to let me do it more often :D

    Have you or anyone else come across and/or used cronless.com or similar?

    I've been using cronless.com for while now, and dont have any complaints.


  • Registered Users Posts: 4,769 ✭✭✭cython




  • Registered Users Posts: 647 ✭✭✭Freddio


    if your code is php you could
    set_time_limit(0);
    which will make the script run until its finished
    Im sure most languages will have an equivalent.


    (its just a workaround)


  • Registered Users Posts: 4,475 ✭✭✭corblimey


    Thanks freddio, I'll take a look at this command this evening see if there's anything in it. I assume though that any iis or apache timeout will trump it.

    pseudo-cron looks good except for one big caveat:
    Include pseudo-cron.inc.php in a high traffic page

    To me, this means that the first person to that page in any scheduled period gets it in the shorts as the cron job runs.


  • Registered Users Posts: 647 ✭✭✭Freddio


    Im not sure but i think IIS timeouts are finite, maybe someone could clarify


  • Advertisement
  • Closed Accounts Posts: 1,150 ✭✭✭Ross


    You probably don't have access to any of the system calls or script features that you would require to "hack" around the problem (if your host has any sense).

    I would imagine you're either going to have to pay or change your provider.


Advertisement