How to write cron job in AWS EC2 server

北城以北 提交于 2019-12-03 03:44:53

问题


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

I followed below steps to create cron tab:

  • 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

Cron tab is created but not running.

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?


回答1:


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

From

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

to

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

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

* * * * * /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.php to see if it is a script or crontab problem.
  • does the script have executing mode? Try chmod 755 /var/www/html/welcome.php

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




回答2:


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

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

and check for any issues for starters.




回答3:


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


来源:https://stackoverflow.com/questions/15470016/how-to-write-cron-job-in-aws-ec2-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!