busyrebooter

I wanted to reboot my ubuntu server when it got very busy state. After trying monit which I think this software has such function, I realize this is a monitoring tool and does not have a such function. That why I created very simple app ‘busyrebooter’.

/proc/loadavg shows the busy state.

$ cat /proc/loadavg
0.36 0.25 0.25 1/182 7658
$

In fact, this proc file is used by tools such as uptime or top. The first 3 columns show the load average of 1, 5, and 15 minutes. Checking 2nd column to see the system is busy or not, and if it was busy, reboot the system. I wrote a C code.

#include <stdio.h>
#include <unistd.h>
 
int main(int argc, char* argv[])
{
	float avr = 0;
	float tmp = 0;
	int fret;
 
	FILE* f = fopen("/proc/loadavg", "r");
	fret = fscanf(f, "%f %f",&tmp, &avr);
	if(fret != 2) {
		perror("fscanf unmatched");
	}
	fclose(f);
 
	if(avr > 20)
	{
		char command[] = "/sbin/reboot";
		char    *exargv[] = {"/sbin/reboot", NULL};
		char    *exenvp[] = {NULL};
		execve(command, exargv, exenvp);
		perror("Could not execute command");
	}
 
	return 0;
}

Compiling.

$ gcc -o busyrebooter main.c

This program executes /sbin/reboot which needs the root privilege.

Add an entry to /etc/crontab to run this program by every 10 minutes.

*/10 *  * * *   root    [ -x /root/bin/busyrebooter ] && /root/bin/busyrebooter

Leave a comment

Your email address will not be published. Required fields are marked *