travelerBT Posted May 14, 2007 Share Posted May 14, 2007 New to cron, I am looking to set up a cron job using PHP... I need a script to set up a cron job on the server to send out a blast email... any help would be greatly appreciated. thanks Quote Link to comment Share on other sites More sharing options...
travelerBT Posted May 14, 2007 Author Share Posted May 14, 2007 someone has got to know how to do this... please! Dying over here! Quote Link to comment Share on other sites More sharing options...
TEENFRONT Posted May 14, 2007 Share Posted May 14, 2007 Got cPanel on your host? use this. Quote Link to comment Share on other sites More sharing options...
travelerBT Posted May 15, 2007 Author Share Posted May 15, 2007 looking for something that is open source... cPanel is pretty expensive. I am using 1and1 for my hosting. Quote Link to comment Share on other sites More sharing options...
trq Posted May 15, 2007 Share Posted May 15, 2007 Setting up a cronjob via php is pretty easy. An example... <?php exec('echo "00 18 * * * /path/to/emailer.php" | crontab -'); ?> Be aware though that this last example will completely overide any jobs you already have setup. If you wanted to add a new job to an already existing crontab file, you would use something like.... <?php exec('crontab -l >> cronfile'); exec('echo "00 18 * * * /path/to/emailer.php" >> cronfile'); exec('cat cronfile | crontab -'); exec('rm cronfile'); ?> Quote Link to comment Share on other sites More sharing options...
marf Posted May 15, 2007 Share Posted May 15, 2007 Actually I'm a little curious about this topic too. I always thought that cron jobs had to be setup on linux box when you had yes.. cpanel or had terminal access to actually make a cron file. However when I was playin around a couple weeks ago I notice that Wordpress has a cron.php. Now only way I think that could be possible is the cron.php perhaps runs predefined script checks, according to when the page is accessed. Meaning an admin perhaps, has to login, then the cron.php is read. Cron.php looks in the MySQL for when it last performed its cron jobs, and if time is due, it executes the appropriate scripts. That makes the most sense to me. Touche, the post above kind of explains some of my question... Quote Link to comment Share on other sites More sharing options...
trq Posted May 15, 2007 Share Posted May 15, 2007 I always thought that cron jobs had to be setup on linux box when you had yes.. cpanel or had terminal access to actually make a cron file. Why would you need terminal access to make a file? You can both make files and execute programs through php. All my post above does is executes the commands you would normally type at the shell. Quote Link to comment Share on other sites More sharing options...
travelerBT Posted May 15, 2007 Author Share Posted May 15, 2007 this is PERFECT! Awesome and thanks so much... this will make my life a hell of alot easier! Again, fantastic... and thanks a bunch! Phil Quote Link to comment Share on other sites More sharing options...
travelerBT Posted May 16, 2007 Author Share Posted May 16, 2007 Can't get this to work! I am just not getting it... I thought it was my server, so I upgraded to what they recommended, but it still doesn't work I am using business package on 1and1 here is my code ... exec('crontab -l >> cronfile'); exec('echo "1 * * * * /classes/testEmailScript.php" >> cronfile'); exec('cat cronfile | crontab -'); exec('rm cronfile'); And here is the testEmailScript.php <? // test to see if the cron job is working. require_once("class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsSMTP(); $mail->Host = ""; $mail->SMTPAuth = true; $mail->Username = ""; $mail->Password = ""; $mail->From = ""; $mail->FromName = ""; $mail->AddAddress(""); //$mail->AddBCC(""); $mail->AddReplyTo("", ""); $mail->WordWrap = 50; $mail->IsHTML(true); $mail->Subject = "test Cron Job"; $mail->Body = "test cron job"; $mail->AltBody = "test cron job"; $mail->SetLanguage("en", ""); if(!$mail->Send()) { echo "Message could not be sent. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit; } ?> someone PLEASE HELP! thanks Quote Link to comment Share on other sites More sharing options...
travelerBT Posted May 16, 2007 Author Share Posted May 16, 2007 Oh, and I want it to go out 1 min later... is that is what is screwed up? Quote Link to comment Share on other sites More sharing options...
trq Posted May 16, 2007 Share Posted May 16, 2007 \Oh, and I want it to go out 1 min later... Later than what? The way you have it set now it will run the script at 1 minute past each hour. Anyway... before we go further (just to test).... what is the output of the following? <?php system("crontab -l"); ?> Quote Link to comment Share on other sites More sharing options...
trq Posted May 16, 2007 Share Posted May 16, 2007 Actually, I forgot (used to running my own servers). You may need to use the -u flag to specify your username. Otherwise cron will run under apache or something else. Have a look at man crontab for more details. Quote Link to comment Share on other sites More sharing options...
travelerBT Posted May 16, 2007 Author Share Posted May 16, 2007 I get no output for the script that you posted... I looked through the link you posted, and I still can't get it to work... where do I stick in my username? Password? thanks for the quick reply Quote Link to comment Share on other sites More sharing options...
travelerBT Posted May 16, 2007 Author Share Posted May 16, 2007 sorry... and I want it to go out 1 min after is is posted to cron... what would be the settings for that? thanks again Quote Link to comment Share on other sites More sharing options...
trq Posted May 16, 2007 Share Posted May 16, 2007 I want it to go out 1 min after is is posted to cron... what would be the settings for that? That is not the intended purpose of cron. In fact, there isn't really a way to do that with cron. What exactly are you trying to do? there may be another way of doing it, cron does not seem to fit your problem. Quote Link to comment Share on other sites More sharing options...
travelerBT Posted May 16, 2007 Author Share Posted May 16, 2007 Here is what I am trying to do: I have a website that users can set up events in particular states. Once the event is posted, I want to send an email out to all other users in that state to inform them that the event was posted and a link for them to sign up for that event. I want this to happen in the back ground so that the user who posted the event doesn't have to wait for the script that sends the email to run. Thanks for all your help... Gui Quote Link to comment Share on other sites More sharing options...
marf Posted May 16, 2007 Share Posted May 16, 2007 One other question, semi unrelated. Can you set up cron jobs on Windows machines with IIS, or does it have to be a Linux/Unix box? Quote Link to comment Share on other sites More sharing options...
travelerBT Posted May 17, 2007 Author Share Posted May 17, 2007 any suggestions folks? This is pretty much the only thing I have left to do to get this website up and running... thanks Quote Link to comment Share on other sites More sharing options...
trq Posted May 17, 2007 Share Posted May 17, 2007 Can you set up cron jobs on Windows machines with IIS, or does it have to be a Linux/Unix box? Cron is a *nix application, not windows. There is scheduled tasks though, not sure how scriptable (if at all) it is though. any suggestions folks? Take a look at at. Quote Link to comment Share on other sites More sharing options...
thedarkwinter Posted May 17, 2007 Share Posted May 17, 2007 just one point going back a few places... if you are submitting this through a web-page then the the "user" will be "apache" or "httpd" or some thing else and wont have write permissions for the crontab. 2 possible solutions 1. (not recommended): run chmod 666 /etc/crontab from your console to give write permissions to it. 2. Use cron.d files: to echo the time of the job and a jobfile-number etc as below. I'm just not sure off-hand if cron needs to be restarted to enable it? exec('echo "* * * * * /classes/testEmailScript.php" >> /etc/cron.d/job####'); But i probably agree that going the cron route is a bad idea. Writing a mini-daemon that waits for a new mysql entry or something might be better Cheers, tdw Quote Link to comment Share on other sites More sharing options...
travelerBT Posted May 17, 2007 Author Share Posted May 17, 2007 that is a fantastic idea! How do I write a daemon (mini) I have never done that either... So if my logic is correct, I would have a table that takes an entry, then a daemon that checks ever 5 min. or so to see if there is an entry... if there is, it executes the mail function to the selected list of neccessary folks. Then deletes the entry that is in that table. So I guess my question is 2 fold: 1. How do I write a daemon that executes every 5 min? Is that with Cron? 2. could someone give me a quick example? Thanks so much for all your comments... this is greatly appreciated! Quote Link to comment Share on other sites More sharing options...
trq Posted May 17, 2007 Share Posted May 17, 2007 Yeah... the easiest way would be to create a cron that runs every 5 minutes, checks a new record exists, and if so, sends the email. Quote Link to comment Share on other sites More sharing options...
travelerBT Posted May 17, 2007 Author Share Posted May 17, 2007 could someone give me an example of how to set up the cron job to run every 10 min? Also some instruction on how to set it up? thanks a bunch Quote Link to comment Share on other sites More sharing options...
thedarkwinter Posted May 28, 2007 Share Posted May 28, 2007 Hi sorry i've been away for a while. you've probably figured it out in the mean time, but just in case: if you stick a script in say "/usr/local/mything/" called "mything" which basically checks if it has a job queued and then quits, then yes you can have a cron job that runs every 10 or whatever. in /etc/cron.d/myjob 0,10,20,30,40,50 * * * * root /usr/local/mything/mything or 0,10,20,30,40,50 * * * * root cd /usr/local/mything/; ./mything (i think you can do *,10 * * * * as well to go every ten minutes) you might need to change the permissions of the file, sometimes cron doesn't like it, but shouldn't cause you a problem if you create it when logged in as root. otherwise: chown root:root /etc/cron.d/myjob chmod 644 /etc/cron.d/myjob ... and then restart cron hope that helps cheers, tdw Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.