Categories: Linux

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
admin

Share
Published by
admin

Recent Posts

Obtain global IP in bash

In current Internet environment, every PC is assigned a private IP. If global IP is…

4 years ago

Create log file in the directory of the script in Python

__file__ is the script file itself. From python3.9 above, it is an absolute path, otherwise…

4 years ago

RichTextBox’s font changes automatically in C#

You need to clear these two flags IMF_DUALFONT and IMF_AUTOFONT to keep font unchanged.

4 years ago

Test post to check image URL

This is an image inserted by wordpress 'Add Media'

4 years ago

msbuild says ‘target does not exist’

I don't know what is going wrong but... How to fix it Open the solution…

4 years ago

Creating a break point that hits on calling win32 api

Create a break point that hits when CreateProcess was called Enter a Function Breakpoint Enter…

5 years ago