[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: script timer



Chien-Lung Wu forced the electrons to say:
> 
> Hi, 
> 
> Can any one help me out? I have a small program, which should be execute
> every 5 sec. I have a simple script as following:
> 
> 
> #!/bin/sh
> 
> while [ "forever" ]
> do
> 
> 	exec myfile
> 
> 	sleep 5s
> done

First of all, there is no "forever" in /bin/sh.

Second, try this:

while :; do exec myfile; sleep 5; done

> #include <stdio.h>
> #include <unistd.h>
> 
> void man (void) {

Bad... it should be int main (void)
> 
> 	while (1)
> 	{
> 
> 		execl ("/home/myfile", "myfile", NULL);
> 		/* myfile have been compiled to be executable and put on
> 		   /home/ */
> 
> 		sleep(5);
> 	}
> }

This won't work because execl() (in fact, none of the exec*() functions) never
returns. The correct code will be:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main (void)
{
	while (1)
	{
		switch (fork())
		{
			case -1: perror ("fork"); exit (EXIT_FAILURE);
			case 0: execl ("/home/myfile", "myfile", NULL); exit (EXIT_FAILURE);
			default: sleep(5);
		}
	}
	exit (EXIT_SUCCESS);
}

There are a few other issues, like avoiding zombies, which I leave as an
exercise to you. ;-)

Binand

- -- 
#include <stdio.h>                                   | Binand Raj S.
char *p = "#include <stdio.h>%cchar *p = %c%s%c;     | This is a self-
int main(){printf(p,10,34,p,34,10);return 0;}%c";    | printing program.
int main(){printf(p,10,34,p,34,10);return 0;}        | Try it!!
- --------------------------------------------------------------------
For more information on Linux in India visit http://www.linux-india.org/
The Linux India mailing list does not accept postings in HTML format.

------------------------------