openstack 实例无交互操作自动关闭

openstack 云桌面在学校中使用,因为不同的课程教学需要,会生成大量的实例,这会带来几个问题,其中一个是超配,紧跟着的是过量的虚拟机在运行而资源不足,或影响速度造成用户体验差,可能问题在于有的实例启动后,课上完了,实例不使用了但没有及时关闭,一直在运行占用资源,想到的办法是检测鼠标和键盘动作,当一个实例超过一定时间长度,如30分钟或一小时,没有鼠标和键盘动作,就自动关闭掉实例。

一、linux规定时间没有鼠标键盘操作自动关机

对于linux系统,找到如下程序,测试可用:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <time.h>
#include <limits.h>
#include <signal.h>

void daemonize();
void *listen_ms( void * );
void *listen_kb( void * );
volatile time_t stamp;
pthread_mutex_t stamp_mutex;

int main()
{
	daemonize();
	pthread_mutex_init( &stamp_mutex, NULL );
	stamp = time( NULL );
	pthread_t ms_tid, kb_tid;
	if ( pthread_create( &ms_tid, NULL, listen_ms, NULL ) != 0 )
	{
		perror( "pthread_create" );
		exit( 1 );
	}

	if ( pthread_create( &kb_tid, NULL, listen_kb, NULL ) != 0 )
	{
		perror( "pthread_create" );
		exit( 1 );
	}

	unsigned int interval = 60 * 30;
	while ( 1 )
	{
		sleep( 1 );
		pthread_mutex_lock( &stamp_mutex );
		if ( time( NULL ) - stamp > interval )
		{
			system( "init 0" );
		}
		pthread_mutex_unlock( &stamp_mutex );
	}
	pthread_join( ms_tid, NULL );
	pthread_join( kb_tid, NULL );
	return(0);
}


void * listen_ms( void * arg )
{
	int fd = open( 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值