Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15309936
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: LINUX

2008-06-23 12:32:28

使用shutdown -P 10,不是很直观,
使用cront也划不来,干脆自制一个,很简单的一个几行的小程序,实现延时关机,这样晚上就可以放心的在benben上下东西le,程序如下:

#include <stdio.h>

#define PRINT_HELP \
"\n"\
"examples:\n"\
"scrott_timer 1:20:8\n"\
"scrott_timer 2:5\n"\
"scrott_timer 60\n"\

void print_help(void)
{
    printf(PRINT_HELP);
}


int main(int argc, char *argv[])
{
    int i,j;
    int seconds;
    if(argc != 2)
    {
        print_help();
        return -1;
    }

    char *s,*pos;
    char buffer[256];
    int esc_nums = 0;
    s = argv[1];
    pos = strchr(s, ':');
    //if(pos)
    {
        seconds = strtol(s, NULL, 0);
        if(pos)
        {
            seconds *= 60*60;
            seconds += 60*strtol(pos+1, NULL, 0);
            pos = strchr(pos+1, ':');
            if(pos)
            {
                seconds += strtol(pos+1, NULL, 0);
            }
        }

        //printf("seconds = %d\n", seconds);
        
        system("sudo date");
        for(i = seconds;i >= 0;i--)
        {
            for(j = 0;j < esc_nums;j++)
            {
                fprintf(stderr, "\b");
            }
            for(j = 0;j < esc_nums;j++)
            {
                fprintf(stderr, " ");
            }
            for(j = 0;j < esc_nums;j++)
            {
                fprintf(stderr, "\b");
            }
            
            esc_nums = sprintf(buffer, "%d:%d:%d", i/(60*60), (i/60)%60, i%60);
            printf(buffer);
            fflush(stdout);
            sleep(1);
        }
        system("sudo halt -p");
    }
    return 0;
}

文件:scrott_timer.tar.bz2
大小:2KB
下载:下载
阅读(3192) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~