How to add a cron job to run php script

2019-08-16 05:14发布

I have a php script I'm trying to run using a cron job. I can execute the script from CLI doing

/usr/local/bin/php Import_Product_Data.php

So I have tried..

0 0 * * * /usr/local/bin/php Import_Product_Data.php

and

crontab 0 0 * * * /usr/local/bin/php Import_Product_Data.php

and

crontab -e 0 0 * * * /usr/local/bin/php Import_Product_Data.php

Vince V. says to open your cronfile and do it.. When I enter crontab -e, I get

*/5 * * * * /root/autosvnup.sh
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/tmp/crontab.ZBGTFu" 1L, 31C

Then I get stuck and not sure what to do.. Would someone help me out?

2条回答
放荡不羁爱自由
2楼-- · 2019-08-16 05:28

That's the vi editor, your default text editor (check man vi). Here are the commands to edit your crontab with vi:

  1. Ctrl-c your /usr/local/bin/php Import_Product_Data.php.
  2. Move the cursor to the first empty line
  3. Press i into the vi console
  4. Paste using your mouse.
  5. Hit ESC.
  6. Write :wq.
  7. Hit RETURN.
查看更多
等我变得足够好
3楼-- · 2019-08-16 05:34

What you're seeing is the vim text editor. When you do crontab -e it opens up your crontab in the default editor. The default editor can be changed by, for example:

export EDITOR=nano

..some find nano a lot easier to work with as vim has a somewhat steep learning curve.

To add your script to the crontab, just insert it on a new line, so that your crontab looks like this:

*/5 * * * * /root/autosvnup.sh
0 0 * * * /usr/local/bin/php Import_Product_Data.php

Then save+exit, and it should run once every midnight (as per the 0 0 * * *)

查看更多
登录 后发表回答