Schedule scripts to run
|
Author: Mattias (mattias@problem.se) Categories: General www, Linux/Unix |
|
|
|
The tutorial: Digg this, Post to del.icio.us, We developed a site where the visitors can sell their used stuff. The add process is made up by three steps, and for some reason some people stop after step 1, and never finish the ad. To keep the database clean and quick, we decided to automaticly delete ads after some days if they are not complete. How to start?If you use a linux/unix server (and I hope you do ;-) there is a great way to get this to happen, Cronjob. You need shell access though or cPanel. Ask your web host if you don't know if you have access to any of these. The first time I tried cronjob it was a little confusing, but afterall... I didn't read a great tutorial about it first :-) If you have shell access, login and write "crontab -l". This is just to see if you have cronjob installed on the server. As long as you don't get an error like "command not found" you ready to rock n roll. Create a cronjobWrite crontab -e, to open up the configuration for all your cronjobs. Probably you don't have anything in the file, because you don't have any jobs created yet. Now you have the editor Vi opened to use for editing the config file. On a new row you write: 01 * * * * echo "I am a cronjob, that run one minute past every hour" 01 02 * * * echo "I am a cronjob, that run daily at 2:01 pm" Want an explanation?? The last part of every row is the command that should run. You can write what every you want there. Run a shell script, mail a log file or run a php script. The rest of the row is When this should happen. minute hour day_of_month month day_of_week You enter the variables you need, the rest you just write a star *. That's why the first row that we wrote will happen every minute every day, month... The only special thing is "day_of_week" which is what day you want it to run, monday, tuesday etc. Write this field like: Mon, Tue, Wed etc etc. I guess you get the idea... The output will be emailed to your email address for the account. If you don't want the output to be emailed to you, just add this after your command: >> logfile 2>&1 This makes both standard output and error output to be surpressed. Run a PHP script from cronjobThere are two different aproaches and you should try which one works for you. Write this in the crontab config file: * * * * * /path/to/php -q /path/to/script.php This will run script.php. If you want to ignore the output, use the technique from above. If this does not work you can use wget or lynx to get cron to "surf to the site" and in that way run the script. Write this as your command in cron: /usr/local/bin/wget -q --delete-after http://www.mydomain.com/theScript.php or /usr/local/bin/lynx -source http://www.mydomain.com/theScript.php (Which one will work depends on what software you have installed on the server) Try it now!The best way to learn is to try it... So go ahead and try and send me an email if you don't manage to get it working. :-) Good luck with your cronjobs!! Digg this, Post to del.icio.us, |
