命令行解析与程序开启后台模式运行
2015-05-29 11:09
363 查看
一个命令行解析与程序开启后台模式运行的示例程序。
闲言少述,直接上代码:
/* 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
闲言少述,直接上代码:
/* 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
相关文章推荐
- 无约束最优化方法
- sublime text快捷键
- 偶遇spfa()判断负环
- Lua Require函数
- 中国IP段查找地址
- Leetcode 运行输出与本地结果不一样的解决方法(用例子说明)
- 十年程序员
- SegNet: a deep convolutional encoder-decoder architecture for robust semantic pixel-wise labelling
- LeetCode的medium题集合(C++实现)十六
- 优化Android应用内存的若干方法
- PL\SQL基础
- Handsontable通用方法
- 如何在安卓应用程序中保存数据
- poi拷贝sheet
- 径向基函数(RBF)神经网络
- 网页端和手机端自动适应网页设计
- 数据库中的左连接(left join)和右连接(right join)区别
- 学员经验:SAT阅读从680提高至730分
- 有趣的布局
- Heap corruption detected.