ช่วยแนะนำ โค้ดสคริป ด้วยครับ

รูปภาพของ p_boonwang
p_boonwang
Rating 1
Posts: 2
Joined: 19-02-2008

ผมเขียนสคริปเพิลให้มันเช็ค เวลาว่าถึง 15 นาฬิกา หรือยัง ถ้าใช่ให้เปลี่ยนไฟล์โดเมน 8 นาฬิกา ให้เปลี่ยนกลับ

ผมไม่แน่ใจว่าโค้ดนี้ จะทำให้เครื่องทำงานหนักหรือเปล่า

โดยใส่ค่าใน  crontab

05 * * * * root /etc/squid/local/bad/swicth_domains.pl
05 * * * * root run-parts /etc/cron.hourly

  แล้วไฟล์สคริป swicth_domain.pl

 

#!/usr/local/bin/perl
my($sec,$min,$hour,$day,$month,$year,$wday,$dst) = localtime(time);
$hourx = $hour;
$minx = $min;
print "$hourx:$minx:$sec\n" ;
my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)=stat("/etc/squid/local/bad/domains");
$size_current = $size;
my($dev2,$ino2,$mode2,$nlink2,$uid2,$gid2,$rdev2,$domainshi5_size,$atime2,$mtime2,$ctime2,$blksize2,$blocks2)=stat("/etc/squid/local/bad/domains-hi5");
$size_spare = $domainshi5_size;
if ($hourx == 15)
{
if ($size_current > $size_spare)
{
rename("/etc/squid/local/bad/domains","/etc/squid/local/bad/domains-tmp");	
rename("/etc/squid/local/bad/domains-hi5","/etc/squid/local/bad/domains");
rename("/etc/squid/local/bad/domains-tmp","/etc/squid/local/bad/domains-hi5");
exec "/etc/init.d/squid reload"
#print "size domains = $size_current \n"; 
#print "size domain-hi5 = $size_spare \n";
#print "Allow Hi5 , YouTube, GoogleVideo\n";
}
}
if ($hourx == 8)
{
#print "Block Hi5, YouTube, GoogleVideo\n";    
if ($size_current < $size_spare)
{
rename("/etc/squid/local/bad/domains","/etc/squid/local/bad/domains-tmp");
rename("/etc/squid/local/bad/domains-hi5","/etc/squid/local/bad/domains");
rename("/etc/squid/local/bad/domains-tmp","/etc/squid/local/bad/domains-hi5");
exec "/etc/init.d/squid reload"
}
}

เผื่อมีบางคำสั่งทำงานน้อยกว่า ก็แนะนำด้วยนะครับ 

 

รูปภาพของ จักรนันท์
จักรนันท์
Rating 7
Posts: 551
Joined: 10-12-2004
ไม่จำเป็น..

ถ้าตั้งใจกำหนดเวลาตายตัวแน่นอนให้ Script ทำงานที่เวลา 15:00 น. แบบนี้ ไม่จำเป็นต้องตรวจสอบเวลาครับ และไม่จำเป็นต้องให้ Script ทำงานทุกๆ นาทีที่ 5 ของทุกชั่วโมงเช่นนี้ด้วยครับ ถ้าหากจะคิดว่า "เครื่องทำงานหนัก" (ที่จริงก็ไม่ได้หนักหนาอะไร) การเช็คทุกๆ ชั่วโมงอย่างนี้ ย่อมทำงานหนักกว่า ทั้งๆ ที่มีเวลาทำงานตายตัวแน่นอนอยู่แล้ว ก็ให้ระบุตรงๆ ไปเลยครับ ว่าให้ทำงานที่ 15:00 ก็จะทำให้ cron เรียน Script ทำงานเพียงครั้งเดียวของทุกวันที่เวลานั้นครับ

ที่คุณกำหนดคือ 05 * * * * ซึ่งก็คือ นาทีที่ 5 ของทุกชั่วโมงของทุกวัน ก็ให้เปลี่ยนเป็น 0 15 * * * ก็จะกลายเป็นทุกเวลา 15 นาฬิกา 0 นาทีของทุกวันครับ อีก Script ก็ 0 8 * * * ครับ

อย่างนี้ไม่ต้องห่วงว่าจะทำงานหนักแน่แล้ว

รูปภาพของ p_boonwang
p_boonwang
Rating 1
Posts: 2
Joined: 19-02-2008
เออ ใช่จริงด้วย
ขอบคุณครับEmbarassed
Syndicate content