php 如何在 AWS EC2 服务器中编写 cron 作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15470016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:17:38  来源:igfitidea点击:

How to write cron job in AWS EC2 server

phpcrontab

提问by fedorqui 'SO stop harming'

I've created a cron job in AWS EC2 but it is not working.

我在 AWS EC2 中创建了一个 cron 作业,但它不起作用。

I followed below steps to create cron tab:

我按照以下步骤创建 cron 选项卡:

  • Step 1: I logged in to AWS EC2 Instace
  • step 2: crontab -e
  • Step 3: Insert mode
  • Step 4: I entered * * * * * php/var/www/html/welcome.php(To run every min.)
  • Step 5: :wq
  • 第 1 步:我登录到 AWS EC2 实例
  • 第2步: crontab -e
  • 第 3 步:插入模式
  • 第 4 步:我输入* * * * * php/var/www/html/welcome.php(每分钟运行一次)
  • 第 5 步: :wq

Cron tab is created but not running.

Cron 选项卡已创建但未运行。

Please can you any one help me if is there any PHP script means please provide me. Do I need to give spaces between every star?

如果有任何PHP脚本意味着请提供我,请您能帮助我。我需要在每颗星星之间留出空格吗?

回答by fedorqui 'SO stop harming'

First of all, you need to put an space between phpand /var:

首先,您需要在php和之间放置一个空格/var

From

* * * * * php/var/www/html/welcome.php

to

* * * * * php /var/www/html/welcome.php
             ^

Then, you'd better use /bin/phpinstead of just php. To determine where the phpexecutable is located, type which phpin your console, it will give you the file path. So it will become something like this:

那么,你最好使用/bin/php而不仅仅是php. 要确定php可执行文件所在的位置,请which php在您的控制台中键入,它将为您提供文件路径。所以它会变成这样:

* * * * * /bin/php /var/www/html/welcome.php
          ^^^^^^^^

More things:

更多的东西:

  • check if crontab is saved properly? Type crontab -l. Your new crontab line should be there.
  • is the script exactly in this dir? Try ls -l /var/www/html/welcome.php.
  • is the script executing if you do from console? Try /bin/php var/www/html/welcome.phpto see if it is a script or crontab problem.
  • does the script have executing mode? Try chmod 755 /var/www/html/welcome.php
  • 检查 crontab 是否正确保存?键入crontab -l。您的新 crontab 行应该在那里。
  • 脚本正好在这个目录中吗?试试ls -l /var/www/html/welcome.php
  • 如果从控制台执行,脚本是否正在执行?试试看/bin/php var/www/html/welcome.php是脚本问题还是crontab问题。
  • 脚本有执行模式吗?尝试chmod 755 /var/www/html/welcome.php

Keep us updated so we can find what can be causing the error.

让我们保持最新状态,以便我们可以找到导致错误的原因。

回答by rrbathula

May be a too late but anyways if you intend to run the script every minute the command should probably be

可能为时已晚,但无论如何,如果您打算每分钟运行一次脚本,则命令可能应该是

* * * * * php /var/www/html/welcome.php

回答by Fergus

Running cron on EC2 is not any different from running on any *nix server - as far as I know. I would check of the system messages for any errors. You can also redirect stderr/stdout to a file as in

在 EC2 上运行 cron 与在任何 *nix 服务器上运行没有什么不同——据我所知。我会检查系统消息是否有任何错误。您还可以将 stderr/stdout 重定向到文件,如

 * * * * * <your script>  >> /var/tmp/out.log 2>&1

and check for any issues for starters.

并检查初学者的任何问题。