³oùتº°Q½×¬O°w¹ï³æ½Õ¼Ò¦¡ªº UPS ±±¨î¦Ó¼g´Nªº¡CµM¦Ó¡A
¤j³¡¤Àªº¹Lµ{¹ï³æ½Õªº UPS »PÁo©úªº UPS ´X¥G¬O¤@¼Ëªº¡C³Ì¤jªº¤£¦P¦b©ó UPS
ºÊ±±µ{¦¡ (°ò¥»¤W¬O powerd
) ¦p¦ó»P UPS ·¾³q¡C
¦b¶}©l¤§«e¡A§Ú«Øij¥H¤Uªº¤u§@¬yµ{:
powerd
(©Î¨ä¥LÃþ¦üªº³nÅé) ¡Cinit
µ{¦¡¨Ï¥¦¯à¦b powerfail ¤Î powerok
¨Æ¥óµo¥Í®É°µ¾A·íªº³B²z (¨Ò¦p¤À§O±Ò°Ê shutdown
µ{§Ç¤Î°±¤î¥¿¦b°õ¦æªº
shutdown
µ{§Ç)¡C
·í¹q¤O®ø¥¢®É¡A UPS «ùÄò¨ÑÀ³¹q¸£©Ò»Ýªº¹q¤O¡A ¨Ã¥H°e¥X«¸m°T¸¹©Î¬O¦b¥¦ªº±±¨îªú¤W¥´¶}³s±µ°T¸¹¥H³qª¾¹q¸£¹q¤O®ø¥¢¤F¡C
°T¸¹Æl½u¬O³]p¬°·í UPS °e¥X«¸m°T¸¹®É¡A·|¨Ï±o¯S®íªº§Ç¦Cªú±±¨î±µ¸}
(°ò¥»¤W¬O DCD
) ¥X²{°ª¹q¦ì¡C
powerd
µ{¦¡t³dºÊµø§Ç¦Cªú¡C¥¦«O«ùµÛ UPS ©Ò»Ý¦Ó¤É°ª /
°§C§Ç¦Cªú±±¨î±µ¸}ªº°ª / §C¹q¦ì (°ò¥»¤W¡A DTR
±µ¸}¥²¶·«O«ù¦b°ª¹q¦ì¥BÃö³¬ UPS ªº±µ¸}¥²¶·«O«ù¦b§C¹q¦ì) ¡C·í powerd
µoı±±¨î±µ¸}Åܦ¨°ª¹q¦ì¡A¥¦·|¦b /etc/powerfail
Àɤ¤¼g¤J
FAIL
³oÓ¦r¨Ã°e SIGPWR
°T¸¹µ¹ init
¦æµ{¡C
·í±±¨î±µ¸}¦A¦¸Åܦ^§C¹q¦ì®É¡A¥¦·|¦b /etc/powerfail
Àɤ¤¼g¤J OK
³oÓ¦r¨Ã°e SIGPWR
°T¸¹µ¹ init
¡C
·í¥¦±µ¨ì¤F SIGPWR
°T¸¹¡A¥¦·|Àˬd /etc/powerfail
ÀÉ¡C
¦pªGùØÀY¬O FAIL
³oÓ¦r²´¥¦·|°õ¦æ /etc/inittab
Àɤ¤ powerfail
¨º¤@¦æªº«ü¥O¡C¦pªGùØÀY¬O OK
³oÓ¦r¥¦«h°õ¦æ inittab
Àɤ¤ powerokwait
¨º¤@¦æªº«ü¥O¡C
¥H¤U§ÚÌ°²³]±z¦³¤@±ø¥i¥H©M powerd
¥¿±`¤u§@ªº°T¸¹Æl½u¡C
¦pªG±z¤£½T©w±zªº°T¸¹Æl½u¬O§_¥i¥H¤u§@ (©Î¥¦¦p¦ó¤u§@)¡A½Ð°Ñ¦Ò
°T¸¹Æl½u¤Ï¦V¤ÀªR¤Îקï power.c
¤@¸`¡A¨º¨à¦³Ãö©ó¤Ö¨£ªº°T¸¹Æl½u³B²z¤Î«·s³]©w powerd.c
ªº¸ê°T¡C
°T¸¹Æl½u¤Ï¦V¤ÀªR¤Îקï power.c<@ref>serial-port§Ç¦Cªú±µ¸}ªº«ü©w ¤Î
°T¸¹Æl½u¤Ï¦V¤ÀªR¤Îקï power.c<@ref>serial-port§Ç¦Cªú±µ¸}ªº«ü©w<@ref>ioctl¦³ÃöŪ¼g RS232 ³o¨â¸`¤]±N¹ï±z¦³À°§U¡C
¦pªG±z»Ýn»s§@¤@±ø°T¸¹Æl½u¡A½Ð¨£ ¦p¦ó»s§@°T¸¹Æl½u ªº¸Ô²Ó»¡©ú¡A¥H¤Î ¦p¦ó»s§@°T¸¹Æl½u<@ref>ups-specificUPS ¸ê®Æ¸`¿ï ¤@¸`¤¤¦³Ãö±zªº UPS ªº¤p¸`¡C «áªÌ¥i¯à¤]¥]§t¤F»s³y°Ó©Ò´£¨Ñªº°T¸¹Æl½uªº¬ÛÃö¸ê°T¡C ©Î³\±zÀ³¦Ü¤Ö±N ¦p¦ó»s§@°T¸¹Æl½u<@ref>ups-specificUPS ¸ê®Æ¸`¿ï<@ref>ups-specificUPS ¸ê®Æ¸`¿ï ¤¤©Ò¦³ªº¤p¸`ÂsÄý¹L¤@¹M¡A¦]¬°¨C¤@¤p¸`³£·|¦³¤@¨ÇÃB¥~ªº¤@¯ë©Ê¸ê®Æ¯à¹ï±z¦³À°§U¡C
/etc/inittab
ÀÉ¡C¼g¤J¹³¥H¤Uªº¤º®e:
# What to do when power fails (Halt system & drain battery :): pf::powerfail:/etc/powerfailscript +5 # If power is back before shutdown, cancel the running shutdown. pg:0123456:powerokwait:/etc/powerokscript
/etc/powerfailscript
¤Î /etc/powerokscript
³o¨âÓ script ÀɤÀ§O¦b¤¤ÀÄÁ (©Î¨ä¥L±z³ßÅwªº®É¶¡)
¤§¤º¶i¦æÃö¾÷¤Î°±¤î¥ô¦ó¦s¦bªº shutdown
µ{§Ç¡C
¨Ì±z©Ò¥Îªº shutdown
µ{¦¡ª©¥»ªº¤£¦P¡A
¥¦¥i¯à·|¬Û·í³æ²¥H¦Ü©ó±z®Ú¥»¤£»Ý¨Ï¥Î script ÀÉ¡A©ÎªÌ¬O¤@¦æ bash
ªº script Àɹ³¬O¥H¤U³o¤@¦æ:
kill `ps -aux | grep "shutdown" | grep -v grep | awk '{print $2}'`(¬°¤FÁקK±z§Ë¿ù¡A¤W¤@¦æ¤¤²Ä¤@Ó³æ¤Þ¸¹¬O¤Ï¦V³æ¤Þ¸¹¡A²Ä¤G¤Î²Ä¤TÓ¬O³æ¤Þ¸¹¡A ¦Ó³Ì«á¤@Ó«h¬°¤Ï¦V³æ¤Þ¸¹¡C)
init
«·sŪ¨ú inittab
Àɤ¤ªº³]©w:
telinit q
powerd
¡C¨ä»yªk¦p¤U:
powerd <line>¨ä¤¤¡A½Ð±z¦Û¦æ±N
<line>
³oÓ¦r´À´«¦¨ modem
©Ò³s±µªº§Ç¦Cªú¦W¡A¹³¬O /dev/cua1
¡C
powerd
¡C
/etc/powerfailscript
¬O§_¤w¶}©l°õ¦æ¡Cshutdown
¬O§_¥¿¦b°õ¦æ¡C/etc/powerokscript
¬O§_¤w¶}©l°õ¦æ¡C/etc/powerfailscript
¬O§_¤w°±¤î°õ¦æ¡Cshutdown
¬O§_¤w°±¤î°õ¦æ¡Cshutdown
¬O§_¤w¦b¹B§@¡A¨Ãµ¥«Ý¤@¬q®É¶¡¡C
½T©w¹q¸£¦b UPS ªº¹q¤O¥Î§¹«e§¹¥þ¦aÃö¾÷¡C¦¹¤@¨BÆJ¬O«Ü¦MÀIªº¡A
¦]¬°¦pªG UPS ªº¹q¤O¦b¹q¸£§¹¦¨Ãö¾÷¤§«e¥ý¥Î§¹¤F¡A
±zªºÀɮרt²Î±N·|¨ü·l¡A¥B±z±N¥i¯à¥á¥¢¤@¨ÇÀɮסC
±z¥i¯à¦b¶i¦æ³o¶µ´ú¸Õ¤§«e·|·Qn³Æ¥÷±z©Ò¦³ªºÀɮסA
¨Ã±N¶}©lÃö¾÷ªº®É¶¡³]µu¤@ÂI¥H¶}©l³o¶µ´ú¸Õ¡C®¥³ß±z! ±zªº Linux ¨t²Î²{¦b¤w¦b UPS ªº«OÅ@¤U¡A ¥B¥¦±N·|¦b¹q¤O¯ÓºÉ¤§«e¦w¥þ¦aÃö¾÷¤F¡C
powerd.c
¨Ï¥¦¦bºÊµø UPS ªº°T¸¹½u®É¯à±oª¾¹q¦À¹q¤O¤w®z¡C
·í¹q¤O·L®z®É¡A°¨¤W¶i¦æºò«æÃö¾÷µ{§Ç¡Cpowerfail
±¡ªp¤UÃö¾÷®É¡A
¯à°÷¦b§¹¦¨©Ò¦³¤u§@¤§«á±N UPS Ãö±¼¡C