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 11 of 11
Like Tree1Likes
  • 1 Post By leigh

Thread: cron jobs

  1. #1
    New Coder
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    cron jobs

    hi all am making a online game and not far of finishing but i dont know how to do cron jobs at all not learned that yet wondered if anyone out there would be able to help me thanks in advance
    Fraser likes this.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    How often does it have to run, and how precise must it be? And what does the CRON job run (the purpose of the executed script)?

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Also, are you running on your own server or through a web hosting company? Usually most webhosts have this feature but may have restrictions on them such as how often it can run (ex. only 1 instance per 5 mins.) and how long the script can last (ex. 60 seconds or 1 minute).

    If you're running on your own server, make sure you have the required software installed, you can Google around but I know the one I use for centos 6 is crontab (crond; I'm almost certain). There's many kinds out there but the difference is mostly the configuration of them.

    The two parts to a cron job are the file to be executed (I'm assuming it's php in your case) and the software that executes it (such as crontab). Research a little on cron jobs and setup for them as well so you'll know how to configure it.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Sometimes the exact execution time isn't really important. Example, you might have a script that needs to run at least 6 times per day, but it doesn't matter if it happens exactly at specific times. I've often used a webpage I have that I know gets at least 300 visits per day. If anyone visits the page, the PHP script "includes" a little script that checks the last time it was run and executes another script if needed. Sort of like my own "script trigger" in lieu of using CRON.

    But if a script needs to execute at exactly the same time(s) each day, you'll have to use CRON.

    Remote services like this: http://www.onlinecronjobs.com/ work 'sort-of' reliably. You're dependent on them, and sometimes they execute several minutes late. If your scripts are important, you may not want to use a service like that. For my "early lunch" text message with the current temperature and weather conditions, I use 'onlinecronjobs'. No big deal if the script is late or fails to run.
    Last edited by mlseim; 04-29-2014 at 10:14 PM.

  • #5
    New Coder
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for all the replies its for my mafia game i am making and basicly some things will need to be rolled over every 24hours some things like lucky dip they can do 1 times a day things like that i am new to all of it and finding it really hard to do this one thing if anyone can help me with this would be great as cant release my game untill this is done thanks

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    You posted this in PHP section, so I assume you're using PHP and MySQL/MySQLi?

    Create a stand-alone script that does all of the "clearing/reset" and other tasks that need to be done. Get that script to work all by itself and then redirect back to your main page.

    After you get that working, you create another script that runs at the beginning of your main page. It checks a timestamp that is written in a simple file. If the timestamp has gone past a preset time (like midnight), it will execute your "clearing" script and write a new timestamp. It checks the timestamp every time someone visits the page, but Your "clearing" script will only run once per day.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts
    That's not how to properly run a cron job. That can make page loading times slower if someone happens to be the one running the cron (for that one time) and could possibly end the cron before it finishes if they close the tab.

    You need to either check with your web hosting provider (if it's through a webhost with cPanel or something of the sort) or install a cron service on the server if it's your own (ex. vps, dedicated, etc.). If you do it this way then you can pretty much guarantee the script is run every 24 hours, unless the server is offline during the time it was supposed to run. You could also run the job manually via SSH if needed to.


    The first step is to specify whether this is through a webhost or your own server, and if it's your own server, whether or not you have a cron service installed. In the meantime, you can create the script you want executed every 24 hours which could send emails, clean up the database, check things, etc.

  • #8
    New to the CF scene
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by leigh View Post
    hi all am making a online game and not far of finishing but i dont know how to do cron jobs at all not learned that yet wondered if anyone out there would be able to help me thanks in advance
    ClickSchedule(https://www.easycron.com/clickschedule) is the easiest way to add a online cron job, you just need to click to choose some settings to complete an complex cron job configuration.

  • #9
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by Fraser View Post
    ClickSchedule(https://www.easycron.com/clickschedule) is the easiest way to add a online cron job, you just need to click to choose some settings to complete an complex cron job configuration.
    This requires the cron to be accessible via HTTP, meaning if anyone finds the link then anyone can run it when they want to, he might not want that (ex. free giveaway every day for the week, which could keep it running). It's not a BAD thing to have it accessible that way, but it's probably better to keep it outside of the www or html folder and call the file directly.

  • #10
    New Coder
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if anyone can help just do them would be great am willing to pay for your time as well if needed just cant seem to do it and its the only thing i need to do to release my game so kinda need it done asap thanks

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    leigh, what exactly is it that you need to do?

    You've posted a few times in this thread but never mentioned exactly what it is you're trying to do - just that you need to use cron jobs. What is your cron job / php script supposed to do? - email users, update a database, delete temporary files?

    How can we help you if you will not tell us?

    As many have pointed out above, you can write a php script and have it called via http remotely OR you can use the native cron scheduler on your host with no html output (in effect, a silent php script).
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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