I want to run a task frequently with auto-trigger?
I want to perform some task without HTTP protocol?
I want to generate a report as a background process?
I don’t want my web-app (heavy traffic) hang for a while to resize (process) a large image?
I want to process some of my maths (apps/requirements) to run in parallel to show the users data very fast.
I want to create a scheduled backups for my server?
lists goes on….
First thing which comes into most of our mind is cron-job or a daemon script which will take away some of the load of any particular task, a quick example:-
Let’s say one of your web-site gives the user to upload any kind of image of any size and the site will cut/resize them into different pieces which they can use it for different purpose.
As a provider you definitely don’t want to slow down your web-site due to heavy amount of data goes into your server – think if 100 people uploading 10MB of an image each & your script will have to cut them into various sizes which is more than enough to bring down your server so fast.
CRONJOB or a DAEMON script:-
Yes, you are right split the job of a particular task into pieces and assure the rest.
I mean from the above example I would split my photo-upload script into two (or many) pieces like one will take care of uploading and the other will handle the resizing into different sizes.
This is OKAY, what is a cronjob then? Cronjob or daemon script is a time-based self triggered (at regular intervals) executable script written to perform defined task.
Facts about a CRONTAB
- Any number of cronjobs can be setup.
- Crontab editor uses the default “vi” editor in LINUX.
- Each OS user will have his/her own crontab editor, having said that each cron will run as that user.
Both of the following option can be tried once after you login into the server:
- To view your cronjob, type the following:
- To edit a cronjob, type the following:
– lists all the cronjobs for that user
– let’s you edit/add/delete the crontab
Format or Syntax or Understanding of CRONTAB:
Take a look at the below picture. I sure it shouldn’t confuse you :-).
Commands to be executed – write a command to execute each time when this job is triggered. You can write almost anything that Linux can parse and execute.
I hope you understand the use of symbols “*” very well. Take a look at some of the examples below.
To execute a file “backup.sh” for every Saturday 3PM, then:-
0 15 * * 6 /var/scripts/backup.sh
– this will execute “/var/scripts/backup.sh” for every Friday @ 3.00 PM, remember for Sunday it is “0” (zero).
To execute a command for every ten minutes, then:-
*/10 * * * * /usr/bin/ntpdate nsdate.datetime.com
– this will be triggered for every 10 mins and it will update the server time. Not “*/10” if you want to trigger a command for every min/hour/day of month/month/day of week, then prefix it with “*/”.
I hope you would have understand the basic use of a crontab in LINUX. Let me show/guide you for windows.
Frankly, I have not used much of these into my windows machine and basically I’m a LINUX guy and not used WINDOWS for my work, BTW, most of my projects is on LAMP, but definitely I will guide to the place where it will for WINDOWS people to understand this technique on their base. Please make use of this (http://www.basiccomputerinformation.ca/schedule-a-task-in-windows/) article.
I hope I’ve helped some people who were getting confused with what crontab or a cronjob is, if not please let me know I can for sure help you out.