AWS弹性魔豆,运行一个cronjob [英] AWS Elastic Beanstalk, running a cronjob

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

问题描述

我想知道是否有一种方法来设置一个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

任何想法?

推荐答案

创建一个文件夹在您的应用程序名为.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

这是弹性魔豆一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.

然后创建一个名为some_cron_job.txt的.ebextensions文件夹内的文件。你会把你的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 /斌/ PHP是通向PHP。然后用路径到PHP文件替换一些-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.

更新: 有一个问题与此解决方案时,弹性魔豆扩展您的实例。例如,假设你有cron任务运行的一个实例。你会得到一个增加流量,弹性魔豆扩展你到两个实例。该leader_only将确保你只有两个实例之间运行的一个cron作业。你的流量会降低,弹性魔豆你扩展到一个实例。但是,而不是终止二审,弹性魔豆终止,这是领导的第一个实例。现在,您不必运行,因为它们只运行在被终止一审任何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.

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

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