加上  

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.




​share​​​​edit​




answered Sep 14 '16 at 8:51





  •  

    Thanks for this! It works now, could you provide me with a link or expand on why /bin/bash is less portable than /usr/bin/env bash – carrots Sep 22 '16 at 0:51
  •  

    /bin/bash is linux specific whereas /usr/bin/env is supposed to exist on all unix systems. See stackoverflow.com/questions/16365130/… for mre. – Orsiris de Jong Sep 23 '16 at 6:15