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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM