Question

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.

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top