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

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

问题描述

我想知道是否有一种方法来设置一个cronjob /任务来执行每一分钟。目前我的任何实例都应该能够运行此任务。



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

  container_commands:
01cronjobs:
命令:echo* / 1 * * * * root php /etc/httpd/myscript.php

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



任何想法?

解决方案

这是我如何添加一个cron作业到Elastic Beanstalk:



如果应用程式根本不存在,请建立一个名为.ebextensions的资料夹。然后在.ebextensions文件夹中创建一个配置文件。我将使用example.config用于说明目的。然后将它添加到example.config

  container_commands:
01_some_cron_job:
命令: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错误。



这样做是创建一个名为01_some_cron_job的命令。命令以字母顺序运行,所以01确保它作为第一个命令运行。



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



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



leader_only键确保命令只在被认为是领导者的ec2实例上运行。



然后在.ebextensions文件夹中创建一个名为some_cron_job.txt的文件,而不是在每个ec2实例上运行。



例如:

 #这个文件末尾的换行符是非常重要的。 Cron不会运行没有它。 
* * * * * root / usr / bin / php some-php-script-here> / dev / null

因此,这个cron作业将作为根的每一天每小时运行一次用户并放弃输出到/ dev / null。 / usr / bin / php是到php的路径。然后替换一些php脚本 - 这里与您的PHP文件的路径。这显然是假设你的cron工作需要运行一个PHP文件。



此外,确保some_cron_job.txt文件在文件的末尾有一个换行符,就像评论说。

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



更新2:

AWS现在可以防止自动实例终止。只需在你的leader实例上启用它,你就可以去了。评论时间70年01月01日原作者:NicolásArévalo


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

Any ideas?

解决方案

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

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.

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.

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.

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

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.

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

So for example:

# 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

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.

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.

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.

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天全站免登陆