Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Vitaminwater

macrumors newbie
Original poster
Aug 18, 2009
21
0
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!
 
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.
 
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?
 
Is there a way I can change the permissions so only the cron job can run the script?

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.
 
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

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.
 
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?
 
Permissions of 0700 (rwx------) would do that.

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! :)
 
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...

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.