Cannot get PHP cron script to run

前端 未结 4 691
轮回少年
轮回少年 2020-12-06 22:52

I have a PHP script that I need to run every minute. I have made sure that the script works from the command line, and I\'m using absolute paths to avoid any environment iss

4条回答
  •  我在风中等你
    2020-12-06 23:02

    There are usually a significant number of things you need to do to move an executable from the command line to a cron job.

    By default, cron jobs get a minimal environment which will almost certainly not have the full path (and a host of other environment variables) that your login sessions have. You may also not be in the same directory (as you have discovered).

    What I tend to do is to execute:

    env | sed 's/^/export /' >$HOME/cron.env
    

    from a login session to get the full environment, then make sure that my cron jobs execute that script before attempting to do the real work. The resultant script may need a small amount of tidying up (quoting, removing transient environment variables like _ and PWD and so forth).

    That way I can be sure that the login and cron environments are identical.

提交回复
热议问题