shell脚本用crontab执行和手动执行结果不一致

加上  

PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin

这行就好了,
shell首部用  #!/usr/bin/env bash 这个移植性更好。

------------------------------

different results between crontab and running script manually

Your script lacks a shebang, so it might run with different shells depending on a crontab or manual launch.

Add the following as first line in your script (replace bash with your current user shell if needed):

#!/usr/bin/env bash

Don't use /bin/bash as it's less portable than /usr/bin/env bash.

Also, crontab runs won't have the PATH variable. Print your path variable with:

echo $PATH

And add it as second line of your script like:

#!/usr/bin/env bash
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin

This should ensure that your script runs in the same environment when run by crontab or manually.

原文地址:https://www.cnblogs.com/oxspirt/p/10282879.html