2013-06-06 7 views
0

편집 : Barmar의 대답을 통해 해결책을 찾았습니다. 전체 smartctl 명령 경로가 추가되었으며 crontab을 통해 작동합니다. Cronjob 메일 본문의 내용이 누락되어 수동으로 올바르게 작동합니다.

나는 아래의 스크립트가 :

내 사서함에서이 같이 bash는 /root/scripts/diskhealth.sh을 수행하여 잘 실행
#!/bin/bash 
#set -x 
EMAIL="[email protected]" 
FILE="/root/scripts/hddreport.txt" 
HOST=`hostname` 
HDD01="/dev/sda" 
P=`ping -c 1 $HOST | sed '1 ! d' | awk '{print $3}'` 

cd /root/scripts/ 
echo -en "HDD health check on the server hosting" $HOST $P > $FILE 
echo -e "\n" >> $FILE 
smartctl -H $HDD01 >> $FILE 
# The above commands do correctly write the content to $FILE (proved by removing the rm command at the bottom and doing cat on the file after) 

smartctl -H $HDD01 

echo "\nEmailed you the health of the Hard Drive $HDD01\n" 
mailx -s "HDD health check complete on `date`" $EMAIL < $FILE 
rm $FILE 

: 나는 할 때

HDD health check on the server hosting domain.co.uk (0.0.0.0) 

smartctl 5.40 2010-07-12 r3124 [x86_64-unknown-linux-gnu] (local build) 
Copyright (C) 2002-10 by Bruce Allen, http://smartmontools.sourceforge.net 

SMART Health Status: OK 

을하지만, 다음 구문 중 하나를 사용하여 crontab을 통해 실행됩니다.

X 20 * * * /bin/bash /root/scripts/diskhealth.sh 
X 20 * * * /bin/sh /root/scripts/diskhealth.sh 
X 20 * * * /root/scripts/diskhealth.sh 

smartctl 디스크 검사를 제외하고 모든 것을 넣습니다.

This is a test 
HDD health check on the server hosting domain.co.uk (0.0.0.0) 

개정 스크립트 아래 "이것은 테스트입니다":

여기
#!/bin/bash 
#set -x 
EMAIL="[email protected]" 
FILE="/root/scripts/hddreport.txt" 
HOST=`hostname` 
HDD01="/dev/sda" 
P=`ping -c 1 $HOST | sed '1 ! d' | awk '{print $3}'` 

cd /root/scripts/ 
echo "This is a test" > $FILE 
echo -en "HDD health check on the server hosting" $HOST $P >> $FILE 
echo -e "\n" >> $FILE 
smartctl -H $HDD01 >> $FILE 

smartctl -H $HDD01 

echo "\nEmailed you the health of the Hard Drive $HDD01\n" 
mailx -s "HDD health check complete on `date`" $EMAIL < $FILE 
rm $FILE 

는은/var/로그입니다 여기에

HDD health check on the server hosting domain.co.uk (0.0.0.0) 

내가 여분의 에코 라인을 추가하는 경우를 보여줍니다 무엇/syslog 출력 : cron :

Jun 6 20:25:01 hostname /USR/SBIN/CRON[1018112]: (root) CMD (bash /root/scripts/diskhealth.sh) 
Jun 6 20:25:01 hostname postfix/pickup[1016576]: 5740356613F: uid=0 from=<root> 
Jun 6 20:25:01 hostname postfix/cleanup[1018125]: 5740356613F: message-id=<[email protected]> 
Jun 6 20:25:01 hostname postfix/qmgr[292015]: 5740356613F: from=<[email protected]>, size=465, nrcpt=1 (queue active) 
Jun 6 20:25:01 hostname postfix/pickup[1016576]: 631F156613E: uid=0 from=<root> 
Jun 6 20:25:01 hostname postfix/cleanup[1018125]: 631F156613E: message-id=<[email protected]> 
Jun 6 20:25:01 hostname postfix/qmgr[292015]: 631F156613E: from=<[email protected]>, size=759, nrcpt=1 (queue active) 
Jun 6 20:25:01 hostname pvemailforward[1018132]: forward mail to <[email protected]> 
Jun 6 20:25:01 hostname postfix/pickup[1016576]: B597B566148: uid=65534 from=<nobody> 
Jun 6 20:25:01 hostname postfix/cleanup[1018125]: B597B566148: message-id=<[email protected]> 
Jun 6 20:25:01 hostname postfix/local[1018131]: 631F156613E: to=<[email protected]>, orig_to=<root>, relay=local, delay=0.39, delays=0.16/0/0/0.23, dsn=2.0.0, status=sent (delivered to command: /usr/bin/pvemailforward) 
Jun 6 20:25:01 hostname postfix/qmgr[292015]: 631F156613E: removed 
Jun 6 20:25:01 hostname postfix/qmgr[292015]: B597B566148: from=<[email protected]>, size=963, nrcpt=1 (queue active) 
Jun 6 20:25:01 hostname postfix/smtp[1018135]: B597B566148: to=<[email protected]>, relay=none, delay=0.16, delays=0.12/0/0.04/0, dsn=5.4.4, status=bounced (Host or domain name not found. Name service error for name=localhost.localdomain type=A: Host not found) 
Jun 6 20:25:01 hostname postfix/qmgr[292015]: B597B566148: removed 
Jun 6 20:25:01 hostname postfix/cleanup[1018125]: D6570566147: message-id=<[email protected]> 
Jun 6 20:25:01 hostname postfix/smtp[1018130]: 5740356613F: to=<[email protected]>, relay=ASPMX.L.GOOGLE.COM[173.194.67.27]:25, delay=0.68, delays=0.12/0/0.19/0.36, dsn=2.0.0, status=sent (250 2.0.0 OK 1370546701 iy4si8635735wic.1 - gsmtp) 
Jun 6 20:25:01 ds9453 postfix/qmgr[292015]: 5740356613F: removed 

이메일을 받았으며 smartctl 출력이 누락되었습니다.

+0

무슨 일이 일어나는지 보려면 crontab 줄을 보여주십시오. – fedorqui

+0

내 crontab 줄은 단지 30입니다. * * * 그런 다음 구문을 언급 한 줄 중 하나 (예 : 밤 8시 30 분에 실행). 나는/var/log/syslog가 보여주는 것을 보여주기 위해 나의 질문을 편집했다. – Zippyduda

+0

구문은'* * * * */bin/sh/path/to/script.sh'와 동일해야합니다. 즉,'/ bin/sh' 또는 그와 동등한 것을 사용하십시오. – fedorqui

답변

2

크론 작업은 .profile을 실행하지 않습니다. 프로필에 $PATH에 추가하는 디렉토리에 smartctl이있는 경우 cron을 (를) 통해 실행하면 해당 파일을 찾을 수 없습니다. 명령에 대한 전체 경로 이름을 사용해보십시오.

+0

root로 로그인하면됩니다. /root/.profile은 [ "$ BASH"] 인 경우, [-f ~/.bashrc]이면 ~ .bashrc는 .bashrc 파일과 .bashrc 파일에만 색이 걸려 있음 – Zippyduda

+0

환상적입니다. 하하. 내가 스크립트의 명령 앞에/usr/sbin/smartctl을 놓았는데 이제는 작동한다. :) Barnar에게 감사를 표한다. :) – Zippyduda

+0

'/ root/.bash_profile'도 확인하자. 시작 스크립트 중 하나가'/ usr/sbin'을 루트의 경로에 추가해야합니다. – Barmar