Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I banged this out one evening just to sort it out for myself. Could use a bit of cleanup, and adding the command to be limited and the max allowed time as command line arguments would be a good idea.
Code:
#!/bin/bash
#sample script to start a program, permit it to run for a predefined amount of CPU time, then kill it.
#
progID="gmplayer"
$progID &
mypid=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $1}'`
echo "mypid $mypid"
mins=0
killmins=5
while [ $mins -lt $killmins ]; do
usePID=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $4}'`
hours=`eval echo "$usePID" | awk -F\: '{print $1}'`
mins=`eval echo "$usePID" | awk -F\: '{print $2}'`
sleep 1
echo "$usePID"
echo "$hours"
echo "$mins"
done
kill "$mypid"
IIRC, although the variable is named "mins", it is actually watching seconds.
Also, if you have multiple instances of the program you are monitoring running, this won't necessarily pick up the instance you want to monitor. You have to make it smarter to do that.
Since there isn't already, I thought it would be fun to write a C program to do it.
This probably isn't very good, because I'm not familiar with process control with libc.
Code:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#define SHELL "/bin/sh"
pid_t pid;
int status = 123;
void sigalrm_catch(int sig)
{
fprintf(stderr, "lim: child process timeout: sending SIGKILL");
kill(pid, SIGKILL);
exit(-4);
}
void exec_limit(int max_time, char *cmd)
{
struct itimerval tm;
pid = fork();
if (pid == 0){
// child process: execute the shell command
execl(SHELL, SHELL, "-c", cmd, NULL);
fprintf(stderr, "lim: could not exec the shell\n");
status = -3;
} else if (pid < 0){
// fork failed
status = -3;
fprintf(stderr, "lim: could not fork\n");
} else {
tm.it_interval.tv_sec = 0;
tm.it_interval.tv_usec = 0;
tm.it_value.tv_sec = max_time / 1000;
tm.it_value.tv_usec = max_time % 1000;
signal(SIGALRM, &sigalrm_catch);
setitimer(ITIMER_REAL, &tm, NULL);
waitpid(pid, &status, 0);
}
}
int main(int argc, char **argv)
{
int i, l, max_time, cmd_len;
char *cmd, *p;
if (argc <= 2){
printf("executes a command and limits its execution time\n"
"syntax: lim timeout command\n"
"\ttimeout is specified in milliseconds\n\n"
);
return 0;
}
max_time = atoi(argv[1]);
if (!max_time){
fprintf(stderr, "lim: invalid timeout\n");
return -1;
}
cmd_len = 0;
for (i=2; i<argc; i++){
cmd_len += strlen(argv[i]) + 1;
}
p = cmd = malloc(cmd_len);
if (!cmd)
return -2;
for (i=2; i<argc; i++){
l = strlen(argv[i]);
memcpy(p, argv[i], l);
p += l;
*p++ = ' ';
}
*p = '\0';
exec_limit(max_time, cmd);
free(cmd);
return status;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.