简体   繁体   English

无法在Amazon EC2上设置cron-job

[英]Unable to setup cron-job on Amazon EC2

I have an Amazon EC2 instance (Ubuntu Server 13.04 - 64 bit [ami-bf1d8a8f]) running my website. 我有一个运行我的网站的Amazon EC2实例(Ubuntu Server 13.04 - 64位[ami-bf1d8a8f])。 I need to setup a Cron Job to get an email alert everyday. 我需要设置一个Cron Job来每天收到一封电子邮件提醒。

Does anyone have any advise or possible solutions? 有没有人有任何建议或可能的解决方案?

Thanks for your time. 谢谢你的时间。

It's just the same as setting up a cron job on any other server via command line. 它与通过命令行在任何其他服务器上设置cron作业相同。

  1. Connect via SSH 通过SSH连接
  2. Navigate to /etc/cron.daily 导航到/etc/cron.daily
  3. Make a new script that runs / calls a PHP script to send the email/other tasks 创建一个运行/调用PHP脚本的新脚本来发送电子邮件/其他任务
  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 . 您可以使用诸如wget -q -O temp.txt http://www.site.com/cron.php类的命令来调用PHP脚本,或者通过命令行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 . 关于wget方法, temp.txt将包含脚本的输出,或者您可以将输出重定向到/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 如果你需要保存cron脚本的输出而你决定使用命令行方法,那么你可以将数据输出到文件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 另外看看Basic cron job设置每天执行php文件

Step 1: Create a bash script in the cron.daily folder 第1步:在cron.daily文件夹中创建一个bash脚本

Connect to the instance via SSH, navigate to the daily cron folder with cd /etc/cron.daily . 通过SSH连接到实例,使用cd /etc/cron.daily导航到每日cron文件夹。 Next type sudo nano mailscript , notice there is no .sh bash extension. 接下来输入sudo nano mailscript ,注意没有.sh bash扩展名。 I had a problem with the extension which caused the script to not run. 我的扩展程序有问题导致脚本无法运行。

Step 2: Bash script contents 第2步:Bash脚本内容

Enter this into the command line text editor nano 在命令行文本编辑器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 . 现在保存文本文件并退出,保存使用CTRL + O然后按Enter键退出按CTRL + X

Step 3: Make it executable 第3步:使其可执行

We need to allow the file to be executed, type sudo chmod +x mailscript . 我们需要允许执行文件,输入sudo chmod +x mailscript

Step 4: Try it out 第4步:尝试一下

To test it out type ./mailscript to run the cron in cron.daily . 要测试它,请输入./mailscript以在cron.daily运行cron。 Now check your emails! 现在查看你的电子邮件!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM