命令行解析与程序开启后台模式运行

一个命令行解析与程序开启后台模式运行的示例程序。
闲言少述,直接上代码:

/* Parse command parameters
 *
 * by Hank
 * 2015/5/27
 */
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>


#include <signal.h>     // to catch Ctrl-C
#include <getopt.h>
#include <sys/select.h>
#include <sys/types.h>
#include <unistd.h>




int
main(int argc, char **argv)
{
    char const *short_options = "d:p:h:e:w:i:f:";
    struct option const long_options[] = {
        { "daemon"  , 0, NULL, 'd' },
        { "port"    , 1, NULL, 'p' },
        { "key"     , 1, NULL, 'i' },
        { "dir"     , 1, NULL, 'e' },
        { "listen"  , 1, NULL, 'w' },
        { "filename", 1, NULL, 'f' },
        { "help"    , 0, NULL, 'h' },
        { NULL },
    };

    int  daemon = 0;
    char *filename = NULL, *dir = NULL;
    int  port = 0, listen=0;
    int key=0;

    for (;;)
    {
        int next_option = getopt_long(argc, argv, short_options, long_options, NULL);
        if (next_option == -1)
          break;
        switch (next_option)
        {
            case 'd': daemon  = 1;            break;
            case 'p': port    = atoi(optarg); break;
            case 'i': key      = atoi(optarg); break;
            case 'f': filename= optarg;       break;
            case 'e': dir     = optarg;       break;
            case 'w': listen   = atoi(optarg); break;
            case 'h': printf("TEST [-d] [-h] <-p port> <-i key> <-w listen-port> [-e log-dir]\n");  break;
        }
    }

    if (0 == port || NULL == filename || 0 == listen || 0 == key)
    {
        printf("TEST [-d] [-h]<-i key>  <-p port> <-f file> <-w listen-port> [-e log-dir]\n");
        goto MAINQUIT;
    }

    if (daemon)
    {
        pid_t pid = fork();
        if (pid < 0)
          return 0;
        if (pid > 0) exit(0);


        if (setsid() < 0)
        {
            printf ("%d \n",key);
            goto MAINQUIT;
        }

        close(0);
        close(1);
        close(2);
        open("/dev/null", O_RDONLY);
        pid = getpid();
        char buf[1024], str[1024];
        struct tm tm;
        time_t t = time(0);
        localtime_r(&t, &tm );
        strftime(str,1024,"%Y%m%d%H%M%S",&tm);

        if (!dir)
          dir = ".";
        snprintf(buf, 1024, "%s/%d_%s_status.log", dir, pid, str);
        open(buf, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
        snprintf(buf, 1024, "%s/%d_%s_trace.log", dir, pid, str);
        open(buf, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
    }

MAINQUIT:
    return 0;
}

编译命令行:
gcc -g -Wall  -o test main.c 

命令行:
./test -i 22 -e /opt -p 8080 -w 8088 -f ./test.conf -d 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北雨南萍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值