#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    4
    Rep Power
    0

    script run once an hour


    I wrote my site all in php. I needed a script that auto executes once an hour. In php, that is not possible. My webhosting service does not support cron. I was wondering if someone could write me a script in perl that automatically runs a php script once an hour. To run the php script, all it has to do is visit the page where the script is.

    I want to be able to upload this python script to my site and it will execute my php script once an hour.

    Thanks for your help!
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Most webhosts/admins will frown on the idea of a long running process, you should note this since they may kill the process. Thus, you may want to access the page locally. Not a problem though, this script doesn't care where it is .

    Code:
    #!/usr/bin/env python
    
    import time, urllib
    
    # the address of the page to be loaded once an hour. CHANGE THIS TO YOUR
    # PAGE.
    address = 'http://www.python.org/'
    
    try:
        while True:
            # Loops forever and access the page address stored in the address
            # variable.
            try: urllib.urlopen(address)
            except IOError:
                # If the page can't be accessed print a warning message and
                # continue on with the loop.
                print 'Warning: Failed to open', address
    
            time.sleep(3600) # Sleep for one hour and then run the loop again.
    
    except KeyboardInterrupt:
        # Exits gracefully from the program.
        print '\nTake care. Bye.'
    Of course this could be improved, say, if an IOError occurs you could keep trying to load the page until it succeeds. If you'd like I could rewrite it to do this without any problems.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    4
    Rep Power
    0
    How would I run this? if i access it in my web browser, it will time out.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The program is designed to be started via SSH or from the local command line or double clicking on Windows. That said you could write a simple Pythons CGI script to start this program for you.

    Code:
    #!/usr/bin/env python
    
    from subprocess import call
    
    print 'Content-Type: text/html\n'
    
    call(('path/to/other/script'))
    
    print 'Started program...'
    Note: that I haven't tested this. You will of course need CGI setup on your server in order to run it, in short. If you have SSH access use it to start the program like this.

    Code:
    $ python program &
    The & here causes Python to run in the background so you can enter more commands. Use fg to bring it back up or leave it out all together .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    53
    Rep Power
    11
    why don't you run a script locally that accesses all the php scripts and then uploads them to your host. that will solve the problem of the sysadmin terminating the process and you can use cron on your own machine.
  10. #6
  11. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    The common workaround for dealing with hosts which don't support cron (wtf?) is to put the php script on your webserver, and write a small cron job to load that page from a computer that does support cron using lynx, or curl/wget or LWP. There's also a service (costs $) called WebBasedCron.com which will handle all that for you.

    --Simon

    Comments on this post

    • CyBerHigh agrees
    Last edited by SimonGreenhill; July 29th, 2005 at 05:30 PM.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by SimonGreenhill
    The common workaround for dealing with hosts which don't support cron (wtf?) is to put the php script on your webserver, and write a small cron job to load that page from a computer that does support cron using lynx, or curl/wget or LWP. There's also a service (costs $) called WebBasedCron.com which will handle all that for you.

    --Simon
    Does this man know everything or what? I'd give you more rep Simon but it wont let me for the time being.

    There's no reason why the script I wrote above couldn't be ran locally as is, without the need to mess around with anything . Still. I think if you're going to do that just set up a cron job with a simple shell command.

    Have fun guys,

    Mark.
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    I did this for someone not to long ago. I first wrote a python script because he needed post data to be sent to the php script. Then he changed it for get then i just wrote a sh command that would do it with cron

    heres the python script with post data support, also this script will nut work on any pythons interpriters younger than 2.2

    Code:
    #!/usr/bin/env python
    import urllib2
    import urllib
    
    data = {'pid': '40478458384'}
    data = urllib.urlencode(data)
    for line in urllib2.urlopen('http://url.com/to/script", data):
            pass
    and for the cron script for just regular old get or just to load a page you can get do something like:

    Code:
    /bin/sh
    wget --output-document=/tmp/loadPage http://url.com/to/php/script
    rm /tmp/loadPage
    I know know that i can do the post data with wget, i learned that after he changed the script to get. However there isn't to meny docs on how to do it with python.

    hope this helps, also netytan's script will work perfectly for get data as well, i just figured mine wouldn't need to have python installed.

IMN logo majestic logo threadwatch logo seochat tools logo