c语言重启命令linux c++重启程序

怎么用C语言实现linux的命令

命令是查询当前登录的每个用户,它的输出包括用户名、终端类型、登录日期及远程主机,在Linux系统中输入who命令输出如下:

创新互联是一家专注于网站设计、成都网站建设与策划设计,湖口网站建设哪家好?创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:湖口等地区。湖口做网站价格咨询:18982081108

我们先man一下who,在帮助文档里可以看到,who命令是读取/var/run/utmp文件来得到以上信息的。

我们再man一下utmp,知道utmp这个文件,是二进制文件,里面保存的是结构体数组,这些数组是struct utmp结构体的。

struct utmp {

short ut_type;

pid_t ut_pid;

char ut_line[UT_LINESIZE];

char ut_id[4];

char ut_user[UT_NAMESIZE];

char ut_host[UT_HOSTSIZE];

struct {

int32_t tv_sec;

int32_t tv_usec;

} ut_tv;

/***等等***/

};

要实现who只需要把utmp文件的所有结构体扫描过一遍,把需要的信息显示出来就可以了,我们需要的信息有ut_user、ut_line、ut_tv、ut_host。

老师给的初始代码:who1.c运行结果如下:

需要注意的是utmp中所保存的时间是以秒和微妙来计算的,所以我们需要把这个时间转换为我们能看懂的时间,利用命令man -k time | grep 3搜索C语言中和时间相关的函数:

经过搜索发现了一个ctime()函数,似乎可以满足我们的需求,于是对代码中关于时间的printf进行修改:

printf("%s",ctime(utbufp-ut_time));

编译运行发现出来的结果虽然已经转换成了我们能看懂的时间格式,但是很明显这个时间是错的:

搜索一下ut_time这个宏,发现它被定义为int32_t类型:

但是ctime()函数中要求参数的类型是time_t类型,所以重新定义一下类型,编译运行之后,发现时间已经改成了正确的,但是发现()中的内容被换行了,猜想ctime()函数的返回值可能自动在最后补了一个字符\n:

一开始想通过\r\b来实现“退行”,但实践后发现并不可取,最后考虑到直接修改字符串中最后一个字符为\0,让其字符串结束,使输出达到与系统who命令一样的效果,即在输出语句前添加如下代码:

cp[strlen(cp)-1] = '\0'

最后编译执行效果,发现解决了该问题:

虽然能看出基本上和who指令的执行结果一致,但是并非完全一样,主要在两点,第一是时间格式不一样,第二个是比who执行的结果多了几条,需要注意的是utmp中保存的用户,不仅仅是已经登陆的用户,还有系统的其他服务所需要的“用户”,所以在显出所有登陆用户的时候,应该过滤掉其他用户,只保留登陆用户。我们可以通过ut_type来区别,登陆用户的ut_type是USER_PROCESS。

先用if语句对执行结果进行过滤,效果如下:

接着解决时间格式问题,利用man命令收到了两个非常有用的函数:localtime()和strftime(),localtime()是把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,strftime()则是用来定义时间格式的,如:年-月-日,利用这两个函数对时间进行修改后,结果显示终于和系统中who命令一模一样:

最终完整的代码如下:

#include stdio.h

#include stdlib.h

#include utmp.h

#include fcntl.h

#include unistd.h

#include time.h

#define SHOWHOST

void show_time(long timeval){

char format_time[40];

struct tm *cp;

cp = localtime(timeval);

strftime(format_time,40,"%F %R",cp);

printf("%s",format_time);

}

int show_info( struct utmp *utbufp )

{

if(utbufp-ut_type == USER_PROCESS){

printf("%-8.8s", utbufp-ut_name);

printf(" ");

printf("%-8.8s", utbufp-ut_line);

printf(" ");

show_time(utbufp-ut_time);

printf(" ");

#ifdef SHOWHOST

printf("(%s)", utbufp-ut_host);

#endif

printf("\n");

}

return 0;

}

int main()

{

struct utmp current_record;

int utmpfd;

int reclen = sizeof(current_record);

if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){

perror( UTMP_FILE );

exit(1);

}

while ( read(utmpfd, current_record, reclen) == reclen )

show_info(current_record);

close(utmpfd);

return 0;

}

怎样用c语言使linux系统关机

把system里边的命令改成"shutdown -r now"看看....

没Linux系统,无法测试

写一个linux下写个关于c语言的双守护进程,就是监视一个进程,当其死掉,马上将其重启

可以分三步来做:

做两个简单的守护进程,并能正常运行

监控进程是否在运行

启动进程

综合起来就可以了,代码如下:

被监控进程thisisatest.c(来自):

#includeunistd.h

#includesignal.h

#includestdio.h

#includestdlib.h

#includesys/param.h

#includesys/types.h

#includesys/stat.h

#includetime.h

void init_daemon()

{

int pid;

int i;

pid=fork();

if(pid0)  

exit(1);  //创建错误,退出

else if(pid0) //父进程退出

exit(0);

setsid(); //使子进程成为组长

pid=fork();

if(pid0)

exit(0); //再次退出,使进程不是组长,这样进程就不会打开控制终端

else if(pid0)  

exit(1);

//关闭进程打开的文件句柄

for(i=0;iNOFILE;i++)

close(i);

chdir("/root/test");  //改变目录

umask(0);//重设文件创建的掩码

return;

}

void main()

{

FILE *fp;

time_t t;

init_daemon();

while(1)

{

  sleep(60); //等待一分钟再写入

  fp=fopen("testfork2.log","a");

  if(fp=0)

  {

      time(t);

      fprintf(fp,"current time is:%s\n",asctime(localtime(t)));  //转换为本地时间输出

      fclose(fp);

  }

}

return;

}

监控进程monitor.c:

#includeunistd.h

#includesignal.h

#includestdio.h

#includestdlib.h

#includesys/param.h

#includesys/types.h

#includesys/stat.h

#includetime.h

#includesys/wait.h

#includefcntl.h

#includelimits.h

#define BUFSZ 150

void init_daemon()

{

int pid;

int i;

pid=fork();

if(pid0)

exit(1);  //创建错误,退出

else if(pid0) //父进程退出

exit(0);

setsid(); //使子进程成为组长

pid=fork();

if(pid0)

exit(0); //再次退出,使进程不是组长,这样进程就不会打开控制终端

else if(pid0)

exit(1);

//关闭进程打开的文件句柄

for(i=0;iNOFILE;i++)

close(i);

chdir("/root/test");  //改变目录

umask(0);//重设文件创建的掩码

return;

}

void err_quit(char *msg)

{

perror(msg);

exit(EXIT_FAILURE);

}

// 判断程序是否在运行

int does_service_work()

{

FILE* fp;

int count;

char buf[BUFSZ];

char command[150];

sprintf(command, "ps -ef | grep thisisatest | grep -v grep | wc -l" );

if((fp = popen(command,"r")) == NULL)

err_quit("popen");

if( (fgets(buf,BUFSZ,fp))!= NULL )

{

count = atoi(buf);

}

pclose(fp);

return count;

// exit(EXIT_SUCCESS);

}

void main()

{

FILE *fp;

time_t t;

int count;

init_daemon();

while(1)

{

  sleep(10); //等待一分钟再写入

  fp=fopen("testfork3.log","a");

  if(fp=0)

  {

      count = does_service_work();

      time(t);

      if(count0)

          fprintf(fp,"current time is:%s and the process exists, the count is %d\n",asctime(localtime(t)), count);  //转换为本地时间输出

      else

      {

          fprintf(fp,"current time is:%s and the process does not exist, restart it!\n",asctime(localtime(t)));  //转换为本地时间输出

          system("/home/user/daemon/thisisatest"); //启动服务

      }

      fclose(fp);

  }

}

return;

}

具体CMD命令:

cc thisisatest.c -o thisisatest

./thisisatest

cc monitor.c -o monitor

./monitor

tail -f testfork3.log   -- 查看日志

c语言中 重启电脑的命令是什么

重启:shutdown -r

关机:shutdown -h

其他命令参数如下。

Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "c

omment"] [-d up:xx:yy]

No args Display this message (same as -?)

-i Display GUI interface, must be the first option

-l Log off (cannot be used with -m option)

-s Shutdown the computer

-r Shutdown and restart the computer

-a Abort a system shutdown

-m \\computername Remote computer to shutdown/restart/abort

-t xx Set timeout for shutdown to xx seconds

-c "comment" Shutdown comment (maximum of 127 characters)

-f Forces running applications to close without war

ning

-d [u][p]:xx:yy The reason code for the shutdown

u is the user code

p is a planned shutdown code

xx is the major reason code (positive integer le

ss than 256)

yy is the minor reason code (positive integer le

ss than 65536)


网站题目:c语言重启命令linux c++重启程序
文章位置:http://ybzwz.com/article/hhsdch.html