5

I have an Amazon EC2 instance (Ubuntu Server 13.04 - 64 bit [ami-bf1d8a8f]) running my website. I need to setup a Cron Job to get an email alert everyday.

Does anyone have any advise or possible solutions?

Thanks for your time.

user1491022
  • 171
  • 2
  • 3
  • 10

1 Answers1

8

It's just the same as setting up a cron job on any other server via command line.

  1. Connect via SSH
  2. Navigate to /etc/cron.daily
  3. Make a new script that runs / calls a PHP script to send the email/other tasks
  4. Make sure its executable

You can use a command such as wget -q -O temp.txt http://www.site.com/cron.php to call the PHP script, or via command line php /var/www/site/cron.php.

Regarding the wget method, temp.txt will contain the output of the script, or you can redirect the output to /dev/null. This will just discard all output. If you need to save the output of the cron script and you decided to go with the command line method then you can output the data to a file php /var/www/site/cron.php > output.txt

If you need more explanation/detail post a comment. Also take a look at Basic cron job set up to execute php file daily

Step 1: Create a bash script in the cron.daily folder

Connect to the instance via SSH, navigate to the daily cron folder with cd /etc/cron.daily. Next type sudo nano mailscript, notice there is no .sh bash extension. I had a problem with the extension which caused the script to not run.

Step 2: Bash script contents

Enter this into the command line text editor nano

#!/bin/bash

php /var/www/mail-script.php > /var/www/mail-script-log.txt

Now save the text file and exit, to save use CTRL + O and then press enter, to exit press CTRL + X.

Step 3: Make it executable

We need to allow the file to be executed, type sudo chmod +x mailscript.

Step 4: Try it out

To test it out type ./mailscript to run the cron in cron.daily. Now check your emails!

Community
  • 1
  • 1
SamV
  • 7,548
  • 4
  • 39
  • 50
  • Can you please explain, exactly what command should I pass through SSH console. – user1491022 Sep 13 '13 at 10:02
  • I need to know how you want to call the PHP script? Where is it located etc? – SamV Sep 13 '13 at 10:19
  • Out now for the day sorry, will update this when I get back. See the link I posted on my answer and do some googling around the subjects I mentioned. – SamV Sep 13 '13 at 10:21
  • I have written the email function within the file name "mail-script.php" and it's located as /var/www/mail-script.php. I put the destination email-id within that email function and just want to trigger this page (i.e mail-script.php) everyday to generate email. The cron folder (i.e cron.daily, cron.hourly etc.) is located under "/etc/" folder. – user1491022 Sep 13 '13 at 10:33
  • hi @SamV have you ever created Custom Crontab on AWS (amazon ec2) ? i need your help for it. – Sona Oct 31 '14 at 09:32