Hi guys after troubling from past two days i did alots of googling and trying alots of tricks. I finally successed to set up cron job using command prompt. Reason to posting this article is that there is no any proper documentation, article and help i find on google to set up cron using command prompt. So finally i decided to post this article to help other people. Here i am trying to clear all the process step by step to set up using command prompt :
Step-1 : Connect with your server using command prompt.In my case i am using putty to communicate with my server.
Step-2 : After login on putty type command nano testCron.php to create new php file testCron.php and it will open into NANO TEXT EDITOR. Actually (testCron.php) file is running
by the cron to check our cronjob is working properly or not by send mail on you given email id .
Step-3 : After typing nano testCron.php like above preview press enter. Now you will display a NANO TEXT EDITOR in your command prompt. Here you will see that a the file name you are give testCron.php with nano command will display on the top of the editor like below :
Step-4 : Now in this step we will creating our php script to sending mail. In our PHP script, we will simply be using the mail function to send a test mail to a specified address.You can simply copy the below bold highlited php code and paste it after replacing your email id into the above NANO TEXT EDITOR by clicking the right mouse button on command prompt.
<?php
mail('yourEmailid@domain.com','Cron Job Test Script','Hello you are successfully find the sollution on phpsollutions.blogspot.in');
?>
Step-5 : After copy and paste the php script in NANO TEXT EDITOR press (ctrl + O) button from your keyboard to save this changes. The file name you are given previous will also display here like below. Check the file name and press Enter key :
Step-6 : Now you will display the message [ Wrote 3 lines ] . like below preview. That means you file testCron.php successfully created :
Step-7 : Now press Ctrl + X key and then it will asking to save your changes then press Y button from your keyboard to save change .Now you will successfully created your testCron.php file on server.
Step-8 : Now you have to test you php file it will work properly or not .So now type command
php testCron.php on your command prompt like below and press Enter key .
Step-9 : If you successfully recieve an email on your email id then you can go a head for further procedure. If you do not receive the mail after a few minutes, go back to step two(2) to open the file in Nano and check the script against the example provided. Only the e-mail address should be different.
Step-10 : Go back to the command line and type command whereis php to find the location of the PHP binary file. This is the second piece of the Cron Job that we need. The path to the binary file is typically /usr/bin/php. Take note of this location.
Step-11 : Now we have the PHP binary (or executable) path, we now need to know the full path to our testCron.php file that has just been created. This is the third and last piece of the Cron Job that we need. Type command pwd to print working directory. This command will output the full path to the current folder you are in. Take note of this location like below preview
Step-12 : Now type command crontab -l and press ENTER to list all Cron Job entries currently set up in the Crontab. If a Cron Jobs is set on crontab so it display below in your case it has been displayed "no crontab for username".
Step-13 : Now type command crontab -e and press ENTER to edit the Crontab and to now add a new Cron Job.
Step-14 : Above command will open the crontab using the NANO TEXT EDITOR . There may be one or more instruction line already in the Crontab that starts with the pound sign (#). All lines starting with the pound sign are commented lines and will not be interpreted by Cron. Use your arrow keys to place the cursor at the end of the commented lines.
This new line will begin the first part of the Cron Job entry - the Schedule. To keep things simple, we will use the asterisk for each of the 5 fields with a space in between each. The entry should look like the image below so far.
Step-15 : Next, we will specify the executable. This is not required if the Cron Job is running a simple command (code that is not saved within a file) or if a file with an extension (e.g. somefile.php) is supplied however, it is recommended. Earlier in the article we ran the whereis command to find the location of the PHP binary (executable) file. Leave a space after the Schedule and type out this location. like below preview
Step-16 : Next, we will need to write out the command to run or the full path to the file containing the code. Since in this example, our code is contained in a file, we will need to specify the full path.
Earlier in this article we used the pwd command to show us the full path to the location where we created the cronTest.php file. Leaving a space after the Executable path, type the full path to the file like below preview
Step-17 : Next, don't forget to specify the filename at the end and precede it with a forward slash if necessary (in this case, add /testCron.php).
Step-18 : Now, all pieces of the Cron Job are entered and to save the file and exit, press Ctrl + X key from keyboard and then it asking you want to save all changes Select Y to save all you changes and then press ENTER to exit and save changes. like below preview
Check your e-mail. Within a minute or so, you should start receiving e-mails.
Step-19 : Once you have received at least one e-mail from the Cron Job script to confirm it works, we will now have to disable the Cron Job so that you do not keep receiving the e-mails.
Go back to the command line and type command crontab -e once again to edit the Crontab.
Once the Crontab is open, use your arrow keys to place the cursor at the beginning of the newly added Cron Job line and press the i key to insert text. Then simply add the pound sign(#) to the beginning of the line to comment out that Cron Job. This will keep Cron from reading that line and essentially stop that Cron Job.
Press Ctrl + X and then select Y to save all you changes and then press ENTER to exit and save changes
The saved all changes successfully
Step-20 : Keep an eye on your e-mail to ensure that you are no longer receiving mailing from the Cron Job script. Thats all guys
Quick Reference Examples :
Schedule to run every minute of every day
Schedule to run every 15 minutes of every day :
Schedule to run every day at 11 PM :
Schedule to run every other hour from 9AM-9PM (9AM, 11AM, 1PM, 3PM, 5PM, 7PM, 9PM) on Sundays :
Schedule to run every weekday at midnight :
If you like this post please don't forgot to post a comment to notify me.
Happy coding guys :)
Step-1 : Connect with your server using command prompt.In my case i am using putty to communicate with my server.
Step-2 : After login on putty type command nano testCron.php to create new php file testCron.php and it will open into NANO TEXT EDITOR. Actually (testCron.php) file is running
by the cron to check our cronjob is working properly or not by send mail on you given email id .
Step-4 : Now in this step we will creating our php script to sending mail. In our PHP script, we will simply be using the mail function to send a test mail to a specified address.You can simply copy the below bold highlited php code and paste it after replacing your email id into the above NANO TEXT EDITOR by clicking the right mouse button on command prompt.
<?php
mail('yourEmailid@domain.com','Cron Job Test Script','Hello you are successfully find the sollution on phpsollutions.blogspot.in');
?>
Step-5 : After copy and paste the php script in NANO TEXT EDITOR press (ctrl + O) button from your keyboard to save this changes. The file name you are given previous will also display here like below. Check the file name and press Enter key :
Step-6 : Now you will display the message [ Wrote 3 lines ] . like below preview. That means you file testCron.php successfully created :
Step-7 : Now press Ctrl + X key and then it will asking to save your changes then press Y button from your keyboard to save change .Now you will successfully created your testCron.php file on server.
Step-8 : Now you have to test you php file it will work properly or not .So now type command
php testCron.php on your command prompt like below and press Enter key .
Step-9 : If you successfully recieve an email on your email id then you can go a head for further procedure. If you do not receive the mail after a few minutes, go back to step two(2) to open the file in Nano and check the script against the example provided. Only the e-mail address should be different.
Step-10 : Go back to the command line and type command whereis php to find the location of the PHP binary file. This is the second piece of the Cron Job that we need. The path to the binary file is typically /usr/bin/php. Take note of this location.
Step-11 : Now we have the PHP binary (or executable) path, we now need to know the full path to our testCron.php file that has just been created. This is the third and last piece of the Cron Job that we need. Type command pwd to print working directory. This command will output the full path to the current folder you are in. Take note of this location like below preview
Step-12 : Now type command crontab -l and press ENTER to list all Cron Job entries currently set up in the Crontab. If a Cron Jobs is set on crontab so it display below in your case it has been displayed "no crontab for username".
Step-13 : Now type command crontab -e and press ENTER to edit the Crontab and to now add a new Cron Job.
Step-14 : Above command will open the crontab using the NANO TEXT EDITOR . There may be one or more instruction line already in the Crontab that starts with the pound sign (#). All lines starting with the pound sign are commented lines and will not be interpreted by Cron. Use your arrow keys to place the cursor at the end of the commented lines.
This new line will begin the first part of the Cron Job entry - the Schedule. To keep things simple, we will use the asterisk for each of the 5 fields with a space in between each. The entry should look like the image below so far.
Step-15 : Next, we will specify the executable. This is not required if the Cron Job is running a simple command (code that is not saved within a file) or if a file with an extension (e.g. somefile.php) is supplied however, it is recommended. Earlier in the article we ran the whereis command to find the location of the PHP binary (executable) file. Leave a space after the Schedule and type out this location. like below preview
Step-16 : Next, we will need to write out the command to run or the full path to the file containing the code. Since in this example, our code is contained in a file, we will need to specify the full path.
Earlier in this article we used the pwd command to show us the full path to the location where we created the cronTest.php file. Leaving a space after the Executable path, type the full path to the file like below preview
Step-17 : Next, don't forget to specify the filename at the end and precede it with a forward slash if necessary (in this case, add /testCron.php).
Step-18 : Now, all pieces of the Cron Job are entered and to save the file and exit, press Ctrl + X key from keyboard and then it asking you want to save all changes Select Y to save all you changes and then press ENTER to exit and save changes. like below preview
Step-19 : Once you have received at least one e-mail from the Cron Job script to confirm it works, we will now have to disable the Cron Job so that you do not keep receiving the e-mails.
Go back to the command line and type command crontab -e once again to edit the Crontab.
Once the Crontab is open, use your arrow keys to place the cursor at the beginning of the newly added Cron Job line and press the i key to insert text. Then simply add the pound sign(#) to the beginning of the line to comment out that Cron Job. This will keep Cron from reading that line and essentially stop that Cron Job.
Press Ctrl + X and then select Y to save all you changes and then press ENTER to exit and save changes
The saved all changes successfully
Step-20 : Keep an eye on your e-mail to ensure that you are no longer receiving mailing from the Cron Job script. Thats all guys
Quick Reference Examples :
Schedule Field | Minute | Hour | Day of Month | Month | Day of Week |
Example | * | * | * | * | * |
Explanation | every minute past the hour | of every hour (24hr time) | every day | of every month | every day of the week (Sun-Sat) |
Schedule to run every 15 minutes of every day :
Schedule Field | Minute | Hour | Day of Month | Month | Day of Week |
Example | */15 | * | * | * | * |
Explanation | every 15 minutes | of every hour (24hr time) | every day | of every month | every day of the week (Sun-Sat) |
Schedule to run every day at 11 PM :
Schedule Field | Minute | Hour | Day of Month | Month | Day of Week |
Example | 0 | 23 | * | * | * |
Explanation | 0 minutes past the hour | of the 23rd hour (24hr time) | every day | of every month | every day of the week (Sun-Sat) |
Schedule to run every other hour from 9AM-9PM (9AM, 11AM, 1PM, 3PM, 5PM, 7PM, 9PM) on Sundays :
Schedule Field | Minute | Hour | Day of Month | Month | Day of Week |
Example | 0 | 9-21/2 | * | * | 0 |
Explanation | 0 minutes past the hour | of every 2nd hour between the 9th-21st hours (24hr time) | every day | of every month | that is a Sunday |
Schedule to run every weekday at midnight :
Schedule Field | Minute | Hour | Day of Month | Month | Day of Week |
Example | 0 | 0 | * | * | 1-5 |
Explanation | 0 minute past the hour | of the midnight hour (24hr time) | every day | of every month | that is a Monday-Friday |
If you like this post please don't forgot to post a comment to notify me.
Happy coding guys :)