วันจันทร์ที่ 17 กุมภาพันธ์ พ.ศ. 2557

เขียน Windows Services ด้วย Golang

ช่วงปีหลัง หนีไม่พ้นต้องเขียนอะไรที่เกี่ยวกับ Windows อย่างหลีกเลี่ยงไม่ได้ พอจะตัดสินใจใช้ Golang คำถามที่ตามมาคือแล้วถ้าต้องเขียน services ล่ะ ใช้ golang ได้รึเปล่า

คำตอบวันนี้คือ "ได้"

ผมเอา code จาก RESTful ของ Blog ที่แล้วมาใช้ทำ service โดยการ compile code บน windows ให้เป็น .exe ก่อน

> go build restmain.go

จะได้ restmain.exe จากนั้นผมใช้ NSSM - the Non-Sucking Service Manager (มาพร้อม source code และ execute binary) unzip แล้ว

> nssm McDuckService [path to]restmain.exe

เรียบร้อยลองเปิด service management อ่า... McDuckService มาแล้ว Double Click แล้ว Start เลย... 


[เพิ่มเติม]

  • เวลาจะลบ service ออกใช้
    • > sc delete McDuckService
  • command line ที่เกียวกับ service ต้องเป็น administrator right 
  • อ้างอิง http://sanatgersappa.blogspot.com/2013/07/windows-service-with-go-easy-way.html
    • เขียนก่อนผมเกือบปี ใช้ nssm เหมือนกันเลย





ไม่มีความคิดเห็น:

แสดงความคิดเห็น