在AWS服务器上触发定时事件

时间:2013-05-16 16:44:21

标签: php events cron

我在AWS上运行了一个连接到mysql数据库的Web服务器。数据库有一个每天运行一次的事件。我需要在相同的时间表上运行一个php脚本来检查数据库,然后发送推送通知。我不相信我可以在数据库中执行此操作,因此需要在Web服务器上完成。我只是不知道如何在AWS上做这类事情。

我从未运行过cron工作,但这似乎是我想要的,但我不知道这是如何在AWS上完成的,它可以启动PHP脚本吗?

1 个答案:

答案 0 :(得分:0)

这里有几个问题,最重要的是建立SSH访问权限(就像你所说的那样,“控制台访问”)。没有它,您将无法设置您的cron作业。

至于cron作业,您需要按照以下步骤操作:

1)设置crontab文件,可能在您的主目录中。这是一个示例文件,可以帮助您入门:

#This file is: /home/<your-user-name>/mycronfile.cron
#Use "crontab mycronfile.cron" to reinstall this file
#Use "crontab -l" to list the contents of the current crontab
#Use man 5 crontab for info
#Format is: minute hour dayofmonth month dayofweek
# * * * * * *
# | | | | | |
# | | | | | +-- Year              (range: 1900-3000)
# | | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
# | | | +------ Month of the Year (range: 1-12)
# | | +-------- Day of the Month  (range: 1-31)
# | +---------- Hour              (range: 0-23)
# +------------ Minute            (range: 0-59)
# , and - can be used as in 0,15,30,45 or 1-5

# run my script every WEEKDAY at 3pm
0 15 * * 1-5 php -f /Library/Developer/myscript.php
# note that it's okay to leave out Year since I'm not using it

2)按照上面的cron示例中的指示从您的主目录安装此文件:

crontab mycronfile.cron

3)输入以下命令确认已安装:

crontab -l

最后一点需要注意的是,在PHP脚本中,您不能使用$ _SERVER全局变量,因为CLI模式不支持这些全局变量。你所有的路径都必须是绝对的。