Creating a cronjob that runs every after one minute
Posted November 25, 2008on:
What is crontab? crontab is a program which is used to install, deinstall or list the tables used to drive the cron(8) daemon in Vixie Cron. You can easily have your own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly.
If the /etc/cron.allow file exists, then you must be listed therein in order to be allowed to use this command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does exist, then you must not be listed in the /etc/cron.deny file in order to use this command. If neither of these files exists, then depending on site-dependent configuration parameters, only the super user will be allowed to use this command, or all users will be able to use this command. For standard Ubuntu or Debian systems, all users may use this command.
The options of crontab command are given below:
If the -u option is given, it specifies the name of the user whose crontab is to be tweaked. If this option is not given, crontab examines “your” crontab, i.e., the crontab of the person executing the command.
crontab -l will show you your currently set up cron jobs on the server.
crontab -r will delete your current cron jobs.
crontab -e will allow you to add or edit your current cron jobs by using your default text editor to edit your “crontab file”. In my case Nano
Note that, vi has a bug in it and won’t work to edit your crontab! We recommend nano (which is just like pico), which is the default editor anyway!
Your crontab includes all the cron jobs you’d like, with one cron entry per line. A cron entry looks like this:
*/2 * * * * /home/user/bash_script.pl #This will run in every other minutes.
The first number is the minute of the hour for the command to run on.
The second number is the hour of the day for the command to run on.
The third number is the day of the month for the command to run on.
The fourth number is the month of the year for the command to run on.
The fifth number is the day of the week for the command to run on.
You may divide any number by a number to meet your requirements like this */2 * * * *
Now let’s create a new cronjob for a PHP file which will simply echo the running date and time of cronjob. Let’s say the cron will run every after one minute.
Step-1: Create a file named my_cron.php at /var/www/my_cron.php with the following code:
echo ‘This cronjob run at: <b>’.date(‘d-F-Y, h:sa’,time()).'</b>’;
Step-2: Open terminal and type the following command:
Step-3: Now NANO editor should be opened. Write the following text and save it by pressing the F3 key and hitting ENTER. Alternatively you may some keyboard combinations to meet the goal as per the instructions given at the bottom of NANO. The following command will be executed in every minutes.
* * * * * PHP /usr/bin/php -q /var/www/mycron.php
Now you may see the list of existing cronjob by executing the command crontab -l. If your cronjob is successfully done, then you should get a report every after one minute from the system. Check your mail!!