How to redirect stdout and stderr to log file

Posted In Shell - By Tum. On Sunday, November 3rd, 2013 With 0 Comments

terminal bash shell
วิธีการ redirect standard output และ standard error ไปเก็บไว้ยัง log file

การ redirect stdout and stderr ไปยัง log file ที่ถูกต้อง

ระบบจะทำการ redirect stdout ไปยัง log file และพร้อมกับ redirect stderr ไปยัง stdout ซึ่งส่งผลให้ทั้ง stdout และ stderr ถูก redirect เข้า log file

ตัวอย่างเช่น

ทดสอบการ redirect ด้วยคำสั่ง ls -l

ตรวจสอบข้อมูลข้างในพบว่ามีข้อมูลที่ได้ redirect ไป

ทดสอบการ redirect ด้วยคำสั่ง lss -l (ตั้งใจให้เกิด error)

ตรวจสอบข้อมูลข้างในพบว่ามีข้อมูล error ที่ได้ redirect ไป

การ redirect stdout and stderr ไปยัง log file ที่ผิด

ระบบจะทำการ redirect stderr ไปยัง stdout ที่จะส่งผลลัพธ์ออก console และ stdout ที่ได้ทำการ redirect จะส่งเข้าไปยัง log file ซึ่งส่งผลให้เมื่อเกิด error ขึ้นจะถูกแสดงออกที่หน้า console ส่วนผลลัพธ์จะถูก redirect เข้า log file

ทดสอบการ redirect ด้วยคำสั่ง ls -l

ตรวจสอบข้อมูลข้างในพบว่ามีข้อมูลที่ได้ redirect ไป

ทดสอบการ redirect ด้วยคำสั่ง lss -l (ตั้งใจให้เกิด error) จะพบ error ถูกส่งออกมายัง console แทนที่จะถูกส่งเข้าไฟล์

ตรวจสอบข้อมูลข้างในพบว่ามีข้อมูล error ที่ได้ redirect ไป จะไม่พบ error อะไรเลย

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

*