SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast jazzle's Avatar
    Join Date
    Feb 2005
    Location
    Liverpool, UK
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Give vars to Cron'd PHP script?

    Hi,
    I can successfully run a php script as a cron job, but have been unable to give it variables (unless I resort to wget).

    I currently have one script which uses a switch based on a variable to decide what to do.

    It seems I can't do
    php /home/xxx/public_html/xxx.php?action=...

    Must I create a new .php script for each of the 6 actions I wish to perform?

    Any thoughts welcome.
    Good judgement comes from experience
    Experience comes from bad judgement

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make some .txt file - each line should contain parameters for each script execution.

    To read this file just use file() function.
    OOP Addict - sorry for my english

  3. #3
    SitePoint Enthusiast jazzle's Avatar
    Join Date
    Feb 2005
    Location
    Liverpool, UK
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't really understand.
    What would I need in the file, and as the cron job?

    edit: even if I get lines from the file, how does that help me pass a var to the php?
    Last edited by jazzle; Nov 22, 2005 at 06:05. Reason: okay, I get that bit...
    Good judgement comes from experience
    Experience comes from bad judgement

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm
    This is file - data.txt
    Code:
    param1=4&param2=5
    param1=6&param2=11
    At beginning of your script
    PHP Code:
    $i=1;//which script execution

    $data=file('data.txt');
    $parameters=$data[$i-1];

    $params=explode("&",$parameters);

    $script_vars=array();

    foreach(
    $params as $param)
    {
    list(
    $name,$value)=explode("=",$param);
    $script_vars[$name]=$value;
    }

    print_r($script_vars); 
    OOP Addict - sorry for my english

  5. #5
    SitePoint Enthusiast jazzle's Avatar
    Join Date
    Feb 2005
    Location
    Liverpool, UK
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    huh?
    how does that help me?

    what command should I put in the cron job?
    Good judgement comes from experience
    Experience comes from bad judgement

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have thought that you want to execute the same script but with diffrent parameters. If you need to run 6 diffrent scripts you need to define 6 cron jobs.
    OOP Addict - sorry for my english

  7. #7
    SitePoint Enthusiast jazzle's Avatar
    Join Date
    Feb 2005
    Location
    Liverpool, UK
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do want to execute the same script with different params (dependant on the time), but my question is how do I pass that param?

    I expect to have 6 different cron jobs, all callling the same script.
    that script has 6 different actions, dependant on the param.
    Good judgement comes from experience
    Experience comes from bad judgement

  8. #8
    SitePoint Enthusiast jazzle's Avatar
    Join Date
    Feb 2005
    Location
    Liverpool, UK
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone other ideas anyone?
    Good judgement comes from experience
    Experience comes from bad judgement

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Define cron job for everyday
    PHP Code:
    $day=(int)date("w");//day of the week

    switch($day)
    {
    case 
    0:
    //script for sunday
    break;
    case 
    1:
    //script for monday
    break;
    //..
    case 5:
    //script for friday
    break;

    OOP Addict - sorry for my english

  10. #10
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jazzle
    Hi,
    I can successfully run a php script as a cron job, but have been unable to give it variables (unless I resort to wget).
    When you call a php script from command line like this
    Code:
    php -f yourscript.php arg1 arg2
    arguments are stored in $_SERVER['argv'] array. argv[0] is the name of script itself, argv[1] is the first argument etc. See also http://www.php.net/features.commandline

  11. #11
    SitePoint Enthusiast jazzle's Avatar
    Join Date
    Feb 2005
    Location
    Liverpool, UK
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @NuLL[PL]: thanks, but the schedule is more complex than that.

    @stereofrog: ah ha! perfect - many thanks.
    Good judgement comes from experience
    Experience comes from bad judgement


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •