LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 04-26-2007, 08:13 AM   #1
xlq
Member
 
Registered: Feb 2007
Distribution: Slackware 12.0
Posts: 58

Rep: Reputation: 15
How to limit program's execution time?


Is there a program I can use to execute a command and limit its execution time?
 
Old 04-26-2007, 09:46 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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.

Last edited by jiml8; 04-26-2007 at 09:55 AM.
 
Old 04-27-2007, 02:47 PM   #3
xlq
Member
 
Registered: Feb 2007
Distribution: Slackware 12.0
Posts: 58

Original Poster
Rep: Reputation: 15
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;
}
 
Old 05-02-2007, 09:40 AM   #4
ronmuzzi
LQ Newbie
 
Registered: May 2007
Location: Michigan
Distribution: Debian
Posts: 5

Rep: Reputation: 0
The "ulimit" command can also do this. Type "man bash" and search for "ulimit".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command execution time minil Programming 10 08-20-2010 04:59 PM
I want to know the program execution time daggulasreedhar Programming 6 03-15-2007 04:07 AM
commnad time execution caprianking2002 Linux - General 2 06-27-2006 03:01 AM
how to [time] the execution hq4ever Linux - General 8 07-03-2004 04:57 AM
limit program's time and memory nibbler Programming 3 03-18-2004 08:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration