Friday 3 June 2016

How to set up Cron Job on Amazon EC2 Cloud Servers


How to set up Cron Job on Amazon EC2 Cloud Servers

I am writing this blog because I faced issue for adding cron jobs in Amazon Ec2 cloud server. I was using Linux 64bit .
To add cron jobs open command prompt (via putty if windows) and login via ssh, when you are login then type:
  1. which php
this will return: /usr/bin/php
keeping response in mind from above, now type:
  1. #!/usr/bin/php -q
-q parameter is passed so that whenever cron job runs, you will not get email.
now type command:
  1. crontab -e
then use vim editor to add the following lines to crontab to run your php script every minute:
  1. * * * * * /usr/bin/php /home/ec2-user/cron.php
Start a php file named cron.php and enter the following contents:
  1. ----------------------code----------------------------
  2. #!/usr/bin/php -q
  3. <?php
  4. //script to test cron on adam's cloud server:
  5. $message = "If ur reading this then the cron job worked!!!!nn";
  6. $myFile = "testFile.txt";
  7. $fh = fopen($myFile, 'w') or die("can't open file");
  8. fwrite($fh, $message);
  9. $stringData = "Yay for cloud cron! script was home/ec2-user/cron.php";
  10. fwrite($fh, $stringData);
  11. fclose($fh);
  12. ?>
  13. --------------------------------------------------------
Now also create file testFile.txt and leave it blank. Also give this file valid permissions to write. When the cron will run, it will going to write the file.
All done!
Thanks for reading the blog.
- See more at: http://findnerd.com/list/view/How-to-set-up-Cron-Job-on-Amazon-EC2-Cloud-Servers/17285/#sthash.aVYOFIzR.dpuf