在腳本文件的第二行添加下面一句即可
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
下面是分析解決問題的步驟:
1.不執(zhí)行的原因是環(huán)境變量引起的
2.目前沒有找到配置cron的默認環(huán)境變量配置方法
vim /etc/crontab 可以看到這個文件里的環(huán)境變量是正確的
3. * * * * * /root/test.sh >ifconfig.txt 2>1 每分鐘執(zhí)行一次test.sh
test.sh的內容如下,可以看到輸出結果環(huán)境變量是 /usr/bin:/bin
#!/bin/bash
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
ifconfig
echo $PATH
輸出:
/root/test.sh: line 3: ifconfig: command not found
/usr/bin:/bin
4.查看ifconfig的位置
a href="mailto:root@ubuntu">root@ubuntu/a>:~# 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)/p>
p> /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
6.test.sh腳本只是臨時改變了PATH的值,它只在當前腳本生效