AWS Elastic Beanstalk,运行 cronjob [英] AWS Elastic Beanstalk, running a cronjob

查看:41
本文介绍了AWS Elastic Beanstalk,运行 cronjob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法设置一个定时任务/任务每分钟执行一次.目前,我的任何实例都应该能够运行此任务.

I would like to know if there is a way to setup a cronjob/task to execute every minute. Currently any of my instances should be able to run this task.

这是我在配置文件中尝试做的,但没有成功:

This is what I have tried to do in the config files without success:

container_commands:
  01cronjobs:
    command: echo "*/1 * * * * root php /etc/httpd/myscript.php"

我不确定这是否是正确的方法

I'm not really sure if this is the correct way to do it

有什么想法吗?

推荐答案

这是我向 Elastic Beanstalk 添加 cron 作业的方式:

在您的应用程序根目录创建一个名为 .ebextensions 的文件夹(如果该文件夹尚不存在).然后在 .ebextensions 文件夹中创建一个配置文件.我将使用 example.config 进行说明.然后将此添加到example.config

This is how I added a cron job to Elastic Beanstalk:

Create a folder at the root of your application called .ebextensions if it doesn't exist already. Then create a config file inside the .ebextensions folder. I'll use example.config for illustration purposes. Then add this to example.config

container_commands:
  01_some_cron_job:
    command: "cat .ebextensions/some_cron_job.txt > /etc/cron.d/some_cron_job && chmod 644 /etc/cron.d/some_cron_job"
    leader_only: true

这是 Elastic Beanstalk 的 YAML 配置文件.确保将其复制到文本编辑器时,文本编辑器使用空格而不是制表符.否则,当您将其推送到 EB 时,您将收到 YAML 错误.

This is a YAML configuration file for Elastic Beanstalk. Make sure when you copy this into your text editor that your text editor uses spaces instead of tabs. Otherwise you'll get a YAML error when you push this to EB.

所以它的作用是创建一个名为 01_some_cron_job 的命令.命令按字母顺序运行,因此 01 确保它作为第一个命令运行.

So what this does is create a command called 01_some_cron_job. Commands are run in alphabetical order so the 01 makes sure it's run as the first command.

该命令然后获取名为 some_cron_job.txt 的文件的内容,并将其添加到/etc/cron.d 中名为 some_cron_job 的文件中.

The command then takes the contents of a file called some_cron_job.txt and adds it to a file called some_cron_job in /etc/cron.d.

然后该命令会更改/etc/cron.d/some_cron_job 文件的权限.

The command then changes the permissions on the /etc/cron.d/some_cron_job file.

leader_only 键确保命令仅在被视为领导者的 ec2 实例上运行.而不是在您可能运行的每个 ec2 实例上运行.

The leader_only key ensures the command is only run on the ec2 instance that is considered the leader. Rather than running on every ec2 instance you may have running.

然后在 .ebextensions 文件夹中创建一个名为 some_cron_job.txt 的文件.您将在此文件中放置您的 cron 作业.

Then create a file called some_cron_job.txt inside the .ebextensions folder. You will place your cron jobs in this file.

例如:

# The newline at the end of this file is extremely important.  Cron won't run without it.
* * * * * root /usr/bin/php some-php-script-here > /dev/null

所以这个 cron 作业将以 root 用户的身份每天每小时每分钟运行一次,并将输出丢弃到/dev/null./usr/bin/php 是 php 的路径.然后将 some-php-script-here 替换为您的 php 文件的路径.这显然是假设您的 cron 作业需要运行 PHP 文件.

So this cron job will run every minute of every hour of every day as the root user and discard the output to /dev/null. /usr/bin/php is the path to php. Then replace some-php-script-here with the path to your php file. This is obviously assuming your cron job needs to run a PHP file.

另外,确保 some_cron_job.txt 文件在文件末尾有一个换行符,就像注释所说的那样.否则 cron 将不会运行.

Also, make sure the some_cron_job.txt file has a newline at the end of the file just like the comment says. Otherwise cron won't run.

更新:当 Elastic Beanstalk 扩展您的实例时,此解决方案存在问题.例如,假设您有一个运行 cron 作业的实例.您会增加流量,因此 Elastic Beanstalk 最多可将您扩展到两个实例.leader_only 将确保您在两个实例之间只运行一个 cron 作业.您的流量会减少,而 Elastic Beanstalk 会将您缩减为一个实例.但 Elastic Beanstalk 不是终止第二个实例,而是终止作为领导者的第一个实例.您现在没有运行任何 cron 作业,因为它们仅在终止的第一个实例上运行.请参阅下面的评论.

Update: There is an issue with this solution when Elastic Beanstalk scales up your instances. For example, lets say you have one instance with the cron job running. You get an increase in traffic so Elastic Beanstalk scales you up to two instances. The leader_only will ensure you only have one cron job running between the two instances. Your traffic decreases and Elastic Beanstalk scales you down to one instance. But instead of terminating the second instance, Elastic Beanstalk terminates the first instance that was the leader. You now don't have any cron jobs running since they were only running on the first instance that was terminated. See the comments below.

更新 2:只是从下面的评论中清楚地说明这一点:AWS 现在可以防止实例自动终止.只需在您的领导实例上启用它,您就可以开始了.– 尼古拉斯·阿雷瓦洛 16 年 10 月 28 日 9:23

Update 2: Just making this clear from the comments below: AWS has now protection against automatic instance termination. Just enable it on your leader instance and you're good to go. – Nicolás Arévalo Oct 28 '16 at 9:23

这篇关于AWS Elastic Beanstalk,运行 cronjob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆