Run Script Automatically

Discussion in 'Web Design and Development' started by Vitaminwater, Feb 14, 2010.

  1. Vitaminwater macrumors newbie

    Joined:
    Aug 18, 2009
    #1
    I'm looking for a way to run a php/mysql script without having a user visit the webpage, thus running it at the beginning of every month or something like that.

    What are my options for this?

    Thanks!
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    The most common is cron. Not all host allow for this though. You could always put the cronjob on your Mac though, but your Mac will need to be on when it needs to run.
     
  3. Vitaminwater thread starter macrumors newbie

    Joined:
    Aug 18, 2009
    #3
    I am currently using Bluehost as my host provider, they do support cron jobs. I have managed to set one up with just the right settings that i would like. It runs a certain website script every month.

    However, I'm curious how i can stop the script from running when your average user visits the page. For instance:

    Say I have the cron job calling ../autorun.php. But what if a user happens upon ../autorun.php, thus making the script run when i do not want it to to.

    Is there a way I can change the permissions so only the cron job can run the script?
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    You can store the file in a non-public location e.g., above the html_public folder (or whatever name your host uses for the main site). Alternatively, give the file 0700 permissions. A 3rd way would involve using a .htaccess file to keep it from being accessed by visitors. The first two options would be the easier route. I have a separate non-public folder setup to hold all my cronjob scripts and the like.
     
  5. Vitaminwater thread starter macrumors newbie

    Joined:
    Aug 18, 2009
    #5
    If i place a file above the public_html on my room, how am i able to access it for the cron job? For my cron job i run the command:

    lynx -source http://www.webname.com/create.php > /dev/null
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    So I take it you're running a local cronjob (as opposed to it being run on the web host)? If that's the case, then you will not be able to put it above the public_html folder. You said the host supports cronjobs so I would use that method. Don't use lynx to run the file though. Just use PHP.

    Code:
    /usr/bin/php /home/username/myfolder/create.php
    You'll want to run the command which php to make sure you have the right path to the PHP executable, and run pwd (on some servers it's cwd) to get your current working directory for the full path to the PHP file.
     
  7. Vitaminwater thread starter macrumors newbie

    Joined:
    Aug 18, 2009
    #7
    Is there a special way to set the unix permissions to allow only my webhost to access the file? but not everyone who just visits the page?
     
  8. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #8
    Permissions of 0700 (rwx------) would do that.
     
  9. designguy79 macrumors 6502

    Joined:
    Sep 24, 2009
    Location:
    Michigan
    #9
    I don't think that will work for what the OP is doing -- his cron job is running lynx to call the PHP, and if he changes the permissions to 0700, I am pretty sure that lynx would be blocked, too...

    cron -> lynx -> tcp/ip (yes, even though it is localhost) -> apache -> php -> script

    Your other suggestions would good! :)
     
  10. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #10
    Right, but I'm going off the assumption he took my advice and isn't trying the lynx method anymore since it's less direct than just calling the PHP processor directly, since there's not a user using lynx. He just needs to page code to execute.
     

Share This Page