LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 566|回复: 0

用程式去控制/etc/inittab電源..,關機和取消.(不知是否可加精)

[复制链接]
发表于 2005-4-4 10:19:23 | 显示全部楼层 |阅读模式
# UPS connected and working correctly.
pf::powerfail:/sbin/shutdown -f -h +30 "ower Failure; System Shutting Down"

# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "ower Restored; Shutdown Cancelled"


軟體讓系統去執行/etc/inittab的如上語句...
gcc turn.c -o turn
./turn 0關
./trun 1開

以下代碼

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/ioctl.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <signal.h>
  10. #include <syslog.h>                                                            
  11. #include <string.h>
  12. #include <paths.h>

  13. #ifndef SIGPWR

  14. #  define SIGPWR SIGUSR1
  15. #endif

  16. #define PWRSTAT         "/etc/powerstatus"

  17. /* Tell init that the power has gone (1), is back (0),
  18.    or the UPS batteries are low (2). */
  19. void powerfail(int event)
  20. {
  21.   int fd;

  22. /* Create an info file for init. */
  23. unlink (PWRSTAT);
  24. if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
  25. switch (event)
  26.       {
  27.       case 0 :
  28.               write(fd, "OK\n", 3);
  29.               break;

  30.       case 1:
  31.              write(fd, "WAIT\n", 5);
  32.              break;
  33.       case 2:
  34.       default:
  35.              write(fd, "FAIL\n", 4);
  36.              break;
  37.       }
  38.   close(fd);
  39.   }
  40.   kill(1, SIGPWR);
  41. }

  42. /* Main program. */
  43. int main(int argc, char *argv[])
  44. {
  45. int DCD;

  46. if (argc < 2) {
  47.        fprintf(stderr, "Usage: turn [0|1]\n");
  48.        exit(1);
  49. }/* Start syslog. */
  50. openlog("turn", LOG_CONS, LOG_DAEMON);

  51. DCD = atoi(argv[1]);
  52. if(DCD != 0) powerfail(0); else powerfail(1);
  53. }

复制代码

轉貼請注明出處..?碜詌inuxsir ...........EXCELL-Kevin
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表