Jump to content

set up a cron job


travelerBT

Recommended Posts

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');

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

\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");

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

;D

Link to comment
Share on other sites

  • 2 weeks later...

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.