|
# 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開
以下代碼
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/ioctl.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <signal.h>
- #include <syslog.h>
- #include <string.h>
- #include <paths.h>
- #ifndef SIGPWR
- # define SIGPWR SIGUSR1
- #endif
- #define PWRSTAT "/etc/powerstatus"
- /* Tell init that the power has gone (1), is back (0),
- or the UPS batteries are low (2). */
- void powerfail(int event)
- {
- int fd;
- /* Create an info file for init. */
- unlink (PWRSTAT);
- if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
- switch (event)
- {
- case 0 :
- write(fd, "OK\n", 3);
- break;
- case 1:
- write(fd, "WAIT\n", 5);
- break;
- case 2:
- default:
- write(fd, "FAIL\n", 4);
- break;
- }
- close(fd);
- }
- kill(1, SIGPWR);
- }
- /* Main program. */
- int main(int argc, char *argv[])
- {
- int DCD;
- if (argc < 2) {
- fprintf(stderr, "Usage: turn [0|1]\n");
- exit(1);
- }/* Start syslog. */
- openlog("turn", LOG_CONS, LOG_DAEMON);
- DCD = atoi(argv[1]);
- if(DCD != 0) powerfail(0); else powerfail(1);
- }
复制代码
轉貼請注明出處..?碜詌inuxsir ...........EXCELL-Kevin |
|