...

View Full Version : Question about Cron Jobs



masterofollies
07-21-2008, 04:43 PM
Hi, my website is in PHP, and I have a message system where logged in users can send messages to people. I need a cron job to automaticly delete all messages after 60 days.

Below is a screenshot of my hosts cron input. I understand the time and date things, but for the Command to Run...... What would go there? Can anyone explain a little about Crons to me?

The only thing I know is that you set them up to automaticly run something over and over.

http://www.rodgame.org/downloads/cron.jpg

mlseim
07-21-2008, 06:07 PM
The command can be a PHP script (the URL to the script).
Example: http://www.mysite.com/messages/clear_it.php

You would have a PHP script that clears the messages and
perhaps the script can send you a courtesy email (or cell phone message),
that tells you it was successful.

The CRON simply runs that script (or command) whenever you want.

Any output from your script (like an output to a browser) is usually ignored
by the CRON ... some CRON's keep a simple log showing what the output was ...
That's why it's nice to get an email letting you know it worked.


.

masterofollies
07-21-2008, 06:40 PM
Yeah there is an email setting with it, to receive messages everytime it happens.

So in the script it runs, do I need to add things like opening and closing php tags, a title, messages, and stuff?

Or can I just have a query update in it?

I assume the script would have an update query to delete messages WHERE daysold > '59'

Inigoesdr
07-22-2008, 01:52 AM
So in the script it runs, do I need to add things like opening and closing php tags, a title, messages, and stuff?
It should be a regular PHP script. You don't need to add a title, or any HTML at all.

I assume the script would have an update query to delete messages WHERE daysold > '59'Yes, presumably.

To get the server to execute the script you usually have a couple of options. Depending on permissions you can execute the script directly with "php /path/to/script.php", or "wget -q --spider http://yoursite.com/script.php", or "lynx -d http://yoursite.com/script.php". There are other ways of course, but these are the easiest, and most likely to work[properly].

mlseim
07-22-2008, 03:01 AM
Write a PHP that you can run just by entering the URL, make it work, test it out,
and then just put that URL in for your CRON job. It basically executes your PHP
script itself instead of you doing it with your browser.

Test it out by writing a simple PHP script that sends you an email or text message
on your cell phone. Set a time for CRON and set back and see if your phone beeps
at a particular time. This will also let you know the time difference between the
server and your phone ... most likely a different time zone.

Go to your cell phone's website (like Nextel or whatever) and they'll tell you how
to use a PHP script or an email client to send an email to your phone.

masterofollies
07-22-2008, 04:52 AM
Daniel you totally loss me on the wget -- q- spider stuff. Is that some kind of secret language? :confused:

Mlseim I will just do something that updates a small piece of my account, quicker to test.

masterofollies
07-22-2008, 06:11 AM
I get the following error. It points directly to the file so I don't understand.




/bin/sh: http://www.rodgame.org/testcron.php: No such file or directory

mlseim
07-22-2008, 02:18 PM
Won't it let you use just the URL?
http://www.rodgame.org/testcron.php

MECU
07-22-2008, 02:34 PM
On some hosts, GoDaddy for one, you have to preface the cron job with the program to run. For example, my cron jobs look like:

/web/cgi-bin/php5 "$HOME/html/db_backup/db_backup.php"

The path to the PHP file to run should be the local path, not a web URL. The above PHP would be located at: http://www.mysite.com/db_backup/db_backup.php You might be able to get away without calling the php/php5 command if it's included in your path.

masterofollies
07-22-2008, 03:20 PM
This is what my webhost says.

From the cPanel, go to Cron jobs, select Standard, and enter (between the quotes) "/usr/bin/php -q /home/USERNAME/public_html/PathToFile" into the Command to run field. You may also want to adjust the settings specifying when to run the cron job. After you finished, click on the SaveCrontab button.

*Note: You will need to replace USERNAME with your user ID and PathToFile with the file name or the rest of the path to the file.

masterofollies
07-22-2008, 03:45 PM
Sorry about the double post, but I finally after many tries got it working. Now I need to post a seperate message in this forum for time/date help.

mlseim
07-22-2008, 09:23 PM
I'm sort of guessing you want to make your script use a certain timezone?

See this:
http://us2.php.net/date_default_timezone_set

masterofollies
07-23-2008, 12:56 AM
Timezone doesn't really matter I don't think. Just as long as they get deleted every 60 days.

_Aerospace_Eng_
07-23-2008, 02:13 AM
Sorry about the double post, but I finally after many tries got it working. Now I need to post a seperate message in this forum for time/date help.
What did you end up using in the end?

I had a cron job running but its seemed to have stopped working now.

masterofollies
07-23-2008, 03:15 AM
I ended up using


/usr/bin/php -q /home/USERNAME/public_html/locationoffille



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum