/    Sign up×
Community /Pin to ProfileBookmark

auto seach in folder every 10 minutes

I need to find a way to make a script that wil auto search file in a specified folder every 10 minutes in php.
i know that in windows i can use “scheduler task” and in Linux “Cron”. i would like to know how to create a cron job or scheduler task in php via a script.

any help out will be appreciated ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@stephan_gerlachMar 28.2008 — well the easiest way is to do it the following way.

  • 1. write a php script which will do the search. At the end of the script place a sleep command with the time of 10 minutes.


  • 2. start the script via a cron. ( make sure your cron will only run the page once )
  • Copy linkTweet thisAlerts:
    @mani-XauthorMar 28.2008 — hmm! ok very good proposition i will try. i have found out how to do it under Linux with a function, since i have to do it under windows too, i will download CRONw which is the CRON version for windows to try.
    Copy linkTweet thisAlerts:
    @ZnupiMar 28.2008 — I don't see why use cron at all. You can just run the script from the command line and make it sleep for 10 minutes when it finishes and then do everything again. If you need it to daemonize (run in the background), try this:
    [code=php]
    $pid = pcntl_fork();
    if ($pid < 0) {
    // pcntl_fork() failed, for some reason
    die("[ERROR] Could not fork process.n");
    }
    if ($pid) {
    // We are the parent, so we have to exit...
    exit;
    }
    // Only child reaches this point, so we have to detach from the terminal...
    if (posix_setsid() < 0) {
    die("[ERROR] Could not detach from terminal.n");
    }
    // Do an infinite loop...
    while (1) {

    // do your stuff here.. check for files or whatever

    sleep(600); // Sleep 10 minutes and then do the stuff again
    }
    [/code]
    Copy linkTweet thisAlerts:
    @mani-XauthorMar 29.2008 — Znupi,

    it's very interesting i do concur with you, even if it works i do not like how CRONw is doing it, i have tried your code but it prompts "Call to undefined function pcntl_fork()" i have read that "is It is not possible to use the function 'pcntl_fork' when PHP is used as Apache module. You can only use pcntl_fork in CGI mode or from command-line".

    how can i use this function in my script when PHP is used as an apache module in windows.

    any help out will be appreaciated
    Copy linkTweet thisAlerts:
    @ZnupiMar 30.2008 — how can i use this function in my script when PHP is used as an apache module in windows.[/QUOTE]
    Honestly, I don't know, I've been only running php scripts from the command line on linux. And even if I knew, I just noticed this:
    [b]Note:[/b] This extension is not available on Windows platforms.[/quote]
    So, I think your best bet is to do this:
    [code=php]
    set_time_limit(0);
    while (1) {
    // do stuff
    sleep(600);
    }
    [/code]

    Open that page with your browser, and obviously it won't completely load, so press the Stop button in your browser (the script continues to run even after the browser has closed the connection). But, Apache might stop your script after a while, but I'm not sure about this. You can do some checking and see if it does ? (for example you could make a script that just puts a dot "." in a file every hour and check that file after a few days to see if your script was stopped or not).

    Good luck ?
    Copy linkTweet thisAlerts:
    @mani-XauthorMar 31.2008 — hmm! ok i have done this so far, and as you said it won't completely load. very good advice i will try to leave for a day to see the result, otherwise if it does not resolve my problem, i will move to Linux to try to execute the process control script.

    Znupi,thanks a lot for your very helpful comments and advices.
    ×

    Success!

    Help @mani-X spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...