在腳本文件的第二行添加下面一句便可html
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin ubuntu
下面是分析解決問題的步驟:vim
1.不執行的緣由是環境變量引發的bash
2.目前沒有找到配置cron的默認環境變量配置方法oop
vim /etc/crontab 能夠看到這個文件裏的環境變量是正確的ui
3. * * * * * /root/test.sh >ifconfig.txt 2>&1 & 每分鐘執行一次test.shspa
test.sh的內容以下,能夠看到輸出結果環境變量是 /usr/bin:/bin.net
#!/bin/bashunix
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binhtm
ifconfig
echo $PATH
輸出:
/root/test.sh: line 3: ifconfig: command not found
/usr/bin:/bin
4.查看ifconfig的位置
root@ubuntu:~# whereis ifconfig
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
5.把test.sh腳本的第二行註釋掉能夠看到輸出結果以下
eth0 Link encap:Ethernet HWaddr aa:00:04:00:0a:04
inet addr:192.168.1.60 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fef8:73b6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2093659 errors:0 dropped:0 overruns:0 frame:0
TX packets:1532815 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:519122457 (519.1 MB) TX bytes:447226592 (447.2 MB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:433596 errors:0 dropped:0 overruns:0 frame:0
TX packets:433596 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:23228192 (23.2 MB) TX bytes:23228192 (23.2 MB)
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
6.test.sh腳本只是臨時改變了PATH的值,它只在當前腳本生效