Crontab

Hihiii, karena tutorial cara bikin daemon dilinux hilang..! pas ada orang nyebutin CRON, tuing-tuing ide pada bertaburan masuk kedalam otak ku. Biar gak lupa ini referensinya. Met mencoba..!

Eh saya punya tips nih, jadi memungkinkan kita untuk bikin DAEMON di linux dengan berbagai macam jenis Programing

Saya misalkan program dijalankan setiap 1 jam
1. Untuk menjalankan script PHP

0 * * * * php /var/www/htdoc/mamat/tes.php

2. Untuk menjalankan script JAVA

0 * * * * java /home/mamat/tes.class
——————————————————–

Crontab-ilmuit

Apa itu Cron?
Cron atau ‘cron daemon’ ialah tugas yang dilakukan oleh komputer secara automatik. Kemudahan cron boleh didapati dalam sistem operasi Unix dan Linux bagi melakukan sesuatu tugas pada waktu tertentu tanpa melibatkan pengguna. Cron tab atau ‘Cron Table’ pula ialah fail teks yang mengandungi beberapa fungsi cron.

Cron amat berguna jika anda ingin menghantar email kepada pelanggan anda pada waktu tertentu, membuang ahli, mengurus mailing list, memproses skrip, memeriksa pautan yang terdapat di dalam link exchange dan sebagainya.

Untuk menyunting fail crontab, gunakan arahan ini:

crontab -e

Arahan Perintah
$ crontab -e

Untuk membuang fail crontab, anda hanya perlu menggunakan arahan:

crontab -r

Arahan Perintah
$ crontab -r

Secara asasnya, format cron adalah seperti berikut:

Minit Jam Haribulan Bulan Hariminggu arahan

Minit: 0-59
Jam: 0-23
Haribulan: 1-31
Bulan: 1-12
Hariminggu: 0-6

Untuk menerangkan cara penggunaan, saya lebih gemar menunjukkan anda contoh-contoh di bawah berikut, di mana saya akan memproses skrip send.cgi. Skrip ini akan menghantar email kepada ahli-ahli saya:

Proses setiap minit

* * * * * arahan

Lihat contoh di bawah ini, skrip send.cgi akan diproses setiap minit:

KOD
* * * * * /home/username/www/cgi-bin/members.cgi

Proses setiap 10 minit

0,10,20,30,40,50 * * * * arahan

Skrip send.cgi akan diproses setiap 10 minit:

KOD
0,10,20,30,40,50 * * * * /home/username/www/cgi-bin/send.cgi

Proses setiap 1 jam

0 * * * * arahan

Skrip send.cgi akan diproses setiap 1 jam:

KOD
0 * * * * /home/username/www/cgi-bin/send.cgi

Proses pada pukul 7:30 a.m

30 7 * * * arahan

Skrip send.cgi akan diproses setiap 7:30 a.m:

KOD
30 7 * * * /home/username/www/cgi-bin/send.cgi

Proses pada pukul 12:30 tengah malam

30 0 * * * arahan

Skrip send.cgi akan diproses setiap 12:30 tengah malam:

KOD
30 0 * * * /home/username/www/cgi-bin/send.cgi

Proses pada pukul 4:15 p.m

15 16 * * * arahan

Skrip send.cgi akan diproses setiap 4:15 p.m:

KOD
15 16 * * * /home/username/www/cgi-bin/send.cgi

Proses pada pukul 4:30 p.m (hari pertama setiap bulan)

30 16 1 * * arahan

Skrip send.cgi akan diproses setiap 4:30 p.m (hari pertama setiap bulan):

KOD
30 16 1 * * /home/username/www/cgi-bin/send.cgi

Proses pada pukul 3:30 p.m setiap Isnin

30 15 * * mon arahan

Skrip send.cgi akan diproses setiap 3:30 p.m setiap Isnin:

KOD
30 15 * * mon /home/username/www/cgi-bin/send.cgi

Proses pada pukul 7:30 a.m setiap 25hb Desember

30 7 25 12 * arahan

Skrip send.cgi akan diproses setiap 7:30 a.m setiap 25hb Desember:

KOD
30 7 25 12 * /home/username/www/cgi-bin/send.cgi

Referensi Lain bila gak sempat coding cgi-nya

Introducing Cron
Getting PHP and Cron to Work Together

By Aaron Brazell Reader Rating: 7.7

Manipulate strings of data. Query an assortment of databases. Send multi-part mail messages. Parse XML.

We�ve all heard of the many powerful and efficient uses for PHP as a Web language. But you can combine PHP with other tools to achieve a range of different results. One of those tools is Cron.
What is Cron?

Cron is very simply a Linux module that allows you to run commands at predetermined times or intervals. In Windows, it�s called Scheduled Tasks. The name Cron is in fact derived from the same word from which we get the word chronology, which means order of time.

Using Cron, a developer can automate such tasks as mailing ezines that might be better sent during an off-hour, automatically updating stats, or the regeneration of static pages from dynamic sources. Systems administrators and Web hosts might want to generate quota reports on their clients, complete automatic credit card billing, or similar tasks. Cron has something for everyone!
The PHP Script

Before we can even think of running a program, we will need a script to run. This script could be anything, but for the sake of this tutorial, let�s deal with a little mailing script. Imagine, for instance, you�re a stockholder of McDonald�s. You eat there every day — breakfast, lunch, and dinner. You even make sure to have a McDonald�s employee wipe your seat before you sit down to enjoy your Super Sized Happy Meal, because, after all, you�re a stockholder!

So you write a script that checks the Dow Jones report every day after the closing bell rings at 4:00 pm. Because you�re a busy person and can�t afford to take a moment away from your PHP coding, you want to have this figure emailed to you. Let�s call this variable $mcdonalds. It will be fed to your mail script from a file called checkstock.php.

This is what this file (mailstock.php) might look like:

// mailstock.php
include(‘checkstock.php’);
mail(‘your@email.com’,’McDonalds Stock’,’Stock Price: ‘.$mcdonalds,’From:you@example.com’);
?>

As this is simply an illustrative example, the details of checkstock.php are beyond the scope of this discussion.
Breaking Cron Down

Now, let�s try to set up the automated running of mailstock.php. If you are a systems administrator, you�ll be able to interface directly with Cron. On a standard Redhat setup, jobs are executed from /etc/crontab at predetermined intervals. We set thee intervals from within four directories that sort the jobs into hourly, daily, weekly, and monthly intervals.

Sometimes, however, you may want to execute a script overnight, on a weekend, or at some other time at which the server is not likely to be under a great deal of strain. This is where the crontab program comes into play. This executable is actually a go-between that exists between the users, who do not have permissions over the Cron folders and files, and the system that�s needed to run the Cron jobs.

Many hosts provide a GUI to administer the Cron jobs. This can simplify the setting up of Cron jobs, however, in the interests of understanding the nuts and bolts of this process, in this tutorial we�ll look at cron jobs from the command line perspective. We will approach this from the point of view of a normal user, but the process is essentially identical for System Administrators as well.

The easiest way to use crontab is via the crontab command.

# crontab �e

This command �edits� the crontab. Upon employing this command, you will be able to enter the commands that you wish to run. My version of Linux uses the text editor vi. You can find information on using vi here.

The syntax of this file is very important � if you get it wrong, your crontab will not function properly. The syntax of the file should be as follows:

minutes hours day_of_month month day_of_week command

All the variables, with the exception of the command itself, are numerical constants. In addition to an asterisk (*), which is a wildcard that allows any value, the ranges permitted for each field are as follows:

* Minutes: 0-59
* Hours: 0-23
* Day_of_month: 1-31
* Month: 1-12
* Weekday: 0-6

We can also include multiple values for each entry, simply by separating each value with a comma.

command can be any shell command and, as we will see momentarily, can also be used to execute a Web document such as a PHP file.

So, if we want to run a script every Tuesday morning at 8:15 AM, our mycronjob file will contain the following content on a single line:

15 8 * * 2 /path/to/scriptname

This all seems simple enough, right? Not so fast! If you try to run a PHP script in this manner, nothing will happen (barring very special configurations that have PHP compiled as an executable, as opposed to an Apache module). The reason is that, in order for PHP to be parsed, it needs to be passed through Apache. In other words, the page needs to be called via a browser or other means of retrieving Web content.

For our purposes, I�ll assume that your server configuration includes wget, as is the case with most default configurations. To test your configuration, log in to shell. If you�re using an RPM-based system (e.g. Redhat or Mandrake), type the following:

# wget –help

If you are greeted with a wget package identification, it is installed in your system.

You could execute the PHP by invoking wget on the URL to the page, like so:

# wget http://www.example.com/file.php

Now, let�s go back to the mailstock.php file we created in the first part of this article. We saved it in our document root, so it should be accessible via the Internet. Remember that we wanted it to run at 4PM Eastern time, and send you your precious closing bell report? Since I�m located in the Eastern timezone, we can go ahead and set up our crontab to use 4:00, but if you live elsewhere, you might have to compensate for the time difference when setting this value.

This is what my crontab will look like:

0 4 * * 1,2,3,4,5 wget http://www.example.com/mailstock.php
Adding the Command to Cron

Okay, so we have a text file. Now what? Now we have to add the text file bearing the crontab to the list of scheduled items. To do that, we simply need to invoke the crontab command.

# crontab stockcron
Summary

There are literally tons of things that can be done with PHP, and as many that can be performed with Cron. Hopefully this gives you a brief overview of some of the possibilities.

You can actually combine more elaborate PHP schemes into a single cron job simply by executing them via a single PHP file. As I mentioned before, it�s even possible to bypass wget by using PHP as a shell scripting language. This would require PHP to be compiled as a standalone binary, and that would bring with it a number of risks, but it can (and has!) been done.

The bottom line is that anything you can accomplish with PHP, you can likely automate with Cron. Happy coding!

4 Tanggapan

  1. It\’s a great and valuable site!e

  2. Hi! Definitely nice and neat site you got there.u

  3. There is no plan nor capital for a so-called electric economy. ,

  4. Things are much better out here in the world of real show biz. ,

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: