codeofaninja
website

Cron Job Example with PuTTy

Photo of Mike Dalisay
Modified Saturday, August 11, 2012
by - @ninjazhai
Today we are going to run a PHP script on schedule, in other words, this PHP script should be executed in certain period of time. This could be any script you want, it can be used to send email to your users, make changes to your file system or database, crawl a website, etc. Running a script on schedule has a lot of benefits because it does the job automatically, without you worrying about it.

Cron Job Example with PuTTy
Execute a program via command line

We're gonna be able to achive this using:
  1. PuTTy - a secure shell or terminal emulator used to access remote systems over the internet.
  2. Cron - the time based job scheduler in unix-like operating systems which powers most servers today.
  3. cURL - a command line tool for getting and sending files using URL syntax. This is usually installed already in your server.
You can skip step 1, 2 and 3 if you know how to log in using PuTTy.

Step 1
If you don't have PuTTy in your computer, you can download it here, choose putty.exe if you're on windows.

Step 2
Run putty.exe, it will look like this:

  • Arrow # 1 is where you're gonna put your host name or IP address of your server.
  • Arrow # 2 is the button you're gonna click (or you can simply press enter) immediately after entering your server host name of IP address.

Step 3
You'll be asked to enter your server username and password. It looks like this:

Click to enlarge.

Step 4
You should be in the crontab, type: crontab -e and press enter. By the way, a crontab is a simple text file in your server with a list of commands meant to be run at specified times, more info here.


It will list the the cron jobs (not yet editable)

Click to enlarge.

Step 5
To make it editable, you should press the "insert" button on your keyboard.

Click to enlarge.

Now you can edit your cron jobs or schedules, there are so many tutorials on how to construct a cron job, you can find some here and here.

On my example above, the cron runs two php script every one minute via cURL. A brief explanation:

# MIN  HOUR  MDAY  MON  DOW  COMMAND 
  */1   *     *     *    *   curl http://mydomain.com/myphp_script.php

MIN - Minute 0-60
HOUR - Hour [24-hour clock] 0-23
MDAY - Day of Month 1-31
MON - Month 1-12 OR jan,feb,mar,apr...
DOW - Day Of Week 0-6 OR sun,mon,tue,wed,thu,fri,sat
COMMAND - Command to be run Any valid command-line

Step 6
After you're done editing, press the "Esc" key on your keyboard to exit from the edit mode. It will again look like this:

Click to enlarge.

Step 7
To exit cron tab, you should type: :wq and press enter.

Click to enlarge.

Then it will look like this:


That's it! Our PHP scripts will be executed every one minute! :D
For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
 
 
Fundamentals
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.