Execution environment of a cron job

The cron daemon was designed in such a way that it does NOT execute commands within your normal shell environment. This means you cannot use bare commands in cron the way you would from the SSH shell command line. This is because the PATH environment variable is /usr/bin:/bin, and the SHELL environment variable is set to /bin/sh. This is an example using the 'wp' command.


* * * * * wp plugin update --all --path=example.com


The cron daemon doesn't know where the 'wp' command is located. You must specify the full path to all commands not in the default PATH, and always specify the full path for files. For example:

* * * * * /usr/bin/wp plugin update --all --path=example.com


If you have any trouble locating where a command is, you can use the following command:

[server]$ which command-name

Example:

[server]$ which wp

The following prints to your terminal:

/usr/bin/wp

来源: Execution environment of a cron job


备注:定时任务中用到的命令如果不是默认路径,需要明确路径

评论1条