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

Re: script timer



All very nice, but the first exec takes you out of the shell/C
program, and how the heck do you expect anything to run after that?

while : ; do program & ; sleep 5 ; done

may be a better way of doing it.

Regards,

- -- Raju (Shell God) Mathur

>>>>> "Binand" == Binand Raj S <binand@xxxxxxxxxxxxxxxxxxxxx> writes:

    Binand> 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

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

    Binand> Second, try this:

    Binand> while :; do exec myfile; sleep 5; done

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

    Binand> 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); } }

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

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

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

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

    Binand> Binand
- --------------------------------------------------------------------
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.

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