Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts

    Question about Cron Jobs

    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.


  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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.


    .
    Last edited by mlseim; 07-21-2008 at 06:11 PM.

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    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'

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by masterofollies View Post
    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.
    Quote Originally Posted by masterofollies View Post
    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].

  • Users who have thanked Inigoesdr for this post:

    masterofollies (07-22-2008)

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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.

  • Users who have thanked mlseim for this post:

    masterofollies (07-22-2008)

  • #6
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Daniel you totally loss me on the wget -- q- spider stuff. Is that some kind of secret language?

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

  • #7
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    I get the following error. It points directly to the file so I don't understand.

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

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    Won't it let you use just the URL?
    http://www.rodgame.org/testcron.php

  • #9
    New Coder
    Join Date
    Jul 2007
    Location
    Colorado, USA
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    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.

  • #11
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    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.

  • #12
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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

  • #13
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Timezone doesn't really matter I don't think. Just as long as they get deleted every 60 days.

  • #14
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Quote Originally Posted by masterofollies View Post
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #15
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    I ended up using

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •