php script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paranoid-android

    #1

    php script

    I have a personal webserver installed and wish to run a script daily on
    my database. How would I achieve this??

    Does anyone have any sample code??

    regards
  • Lord Hasenpfeffer

    #2
    Re: php script

    paranoid-android wrote:[color=blue]
    > I have a personal webserver installed and wish to run a script daily on
    > my database. How would I achieve this??
    >
    > Does anyone have any sample code??
    >
    > regards[/color]

    I don't have any sample code handy but if you're running Linux or
    anything similar to that, you most likely could create a 'cron' job that
    runs a shell script once a day. The shell script could invoke your
    browser (Lynx or whatever) to browse|execute the PHP script and then
    kill it a few seconds later.

    This is something I did about 4 1/2 years ago when I was first learning
    PHP and MySQL. It wasn't a very elegant solution but it did work. A
    site I was building needed an automatic update to the database every
    night at midnight - and armed with only Linux, PHP & MySQL that's how I
    did it.

    If anyone has a better way to accomplish this, I'm eager to learn how
    too! :)

    Myke

    Comment

    • WebRod

      #3
      Re: php script

      Hi,

      Linux/unix:
      you just need to add this line as the FIRST LINE in your script:
      #!/usr/local/bin/php

      Of course, you need to use the path of your php executable file.

      You need to change the permission of you file, and add "executable " rights.
      (you can rename your php file=> cgi, i don't think it's required)
      Now you only need to create a cron job.
      It will call your script
      And that's it!

      Windows:
      Create first a .bat file with:
      c:\windows\php\ php.exe c:\inetpub\www\ yoursite\yoursc ript.php
      You can now use the "task scheduler" to call the batch file when you want

      Rod


      "paranoid-android" <paranoid-android@ntlworl d.com> a écrit dans le message
      de news: FppKd.70$nQ1.27 @newsfe4-gui.ntli.net...[color=blue]
      >I have a personal webserver installed and wish to run a script daily on my
      >database. How would I achieve this??
      >
      > Does anyone have any sample code??
      >
      > regards[/color]


      Comment

      • Lord Hasenpfeffer

        #4
        Re: php script

        WebRod wrote:[color=blue]
        > Hi,
        >
        > Linux/unix:
        > you just need to add this line as the FIRST LINE in your script:
        > #!/usr/local/bin/php
        >
        > Of course, you need to use the path of your php executable file.
        >
        > You need to change the permission of you file, and add "executable " rights.
        > (you can rename your php file=> cgi, i don't think it's required)
        > Now you only need to create a cron job.
        > It will call your script
        > And that's it![/color]

        Wow! That kicks ass! :)

        I've never seen anything in print which even hints at using:

        #!/usr/local/bin/php

        ....to execute a PHP script without actually browsing it.

        Where is this approach documented in relation to executing PHP?

        That surely would have shaved hours off of my development time 4 1/2
        years ago if I'd only known it was an option.

        I've only been subscibed to this newsgroup for a few hours and already
        I've learned things I've needed to know for years but could never find
        answers for elsewhere.

        Thanks!

        Myke

        Comment

        • WebRod

          #5
          Re: php script

          Hi,

          the real documentation is on php.net


          On my computer it opens in french, i can't manage to find it in english.
          But i'm guessing it will open in english on your computer (it think it
          redirects on a different page according to your location)

          i 've found a great tutorial here in english:
          http://www.phpfreaks.com/tutorials/86/0.php

          PhP can be run just as a Perl script, cgi script.
          I use this functionality everyday to notify users and clean my db.
          One of the main difference is to provide parameters, you need to use
          $_server["argv"] (not $_request)
          So you can run your script like this:
          php.exe yourscript.php param1 param2


          Best Regards
          "Lord Hasenpfeffer" <mykec-nospam@spamsuck s-ionet.net> a écrit dans le
          message de news: ZXuKd.791$S3.25 @newsread2.news .atl.earthlink. net...[color=blue]
          > WebRod wrote:[color=green]
          >> Hi,
          >>
          >> Linux/unix:
          >> you just need to add this line as the FIRST LINE in your script:
          >> #!/usr/local/bin/php
          >>
          >> Of course, you need to use the path of your php executable file.
          >>
          >> You need to change the permission of you file, and add "executable "
          >> rights. (you can rename your php file=> cgi, i don't think it's required)
          >> Now you only need to create a cron job.
          >> It will call your script
          >> And that's it![/color]
          >
          > Wow! That kicks ass! :)
          >
          > I've never seen anything in print which even hints at using:
          >
          > #!/usr/local/bin/php
          >
          > ...to execute a PHP script without actually browsing it.
          >
          > Where is this approach documented in relation to executing PHP?
          >
          > That surely would have shaved hours off of my development time 4 1/2 years
          > ago if I'd only known it was an option.
          >
          > I've only been subscibed to this newsgroup for a few hours and already
          > I've learned things I've needed to know for years but could never find
          > answers for elsewhere.
          >
          > Thanks!
          >
          > Myke
          >[/color]


          Comment

          • Lord Hasenpfeffer

            #6
            Re: php script

            WebRod wrote:[color=blue]
            > Hi,
            >
            > the real documentation is on php.net
            > www.php.net/features.commandline
            >
            > On my computer it opens in french, i can't manage to find it in english.
            > But i'm guessing it will open in english on your computer (it think it
            > redirects on a different page according to your location)
            >
            > i 've found a great tutorial here in english:
            > http://www.phpfreaks.com/tutorials/86/0.php
            >
            > PhP can be run just as a Perl script, cgi script.
            > I use this functionality everyday to notify users and clean my db.
            > One of the main difference is to provide parameters, you need to use
            > $_server["argv"] (not $_request)
            > So you can run your script like this:
            > php.exe yourscript.php param1 param2[/color]

            Wow, that's very cool. I've been coding with PHP since 2000 and this is
            the first I've heard of a way to run code without browsing it.

            I actually have PHP scripts which I've written for mass renaming of
            MP3s, etc. (i.e. totally un-web-related stuff) but I always thought I
            had to browse it to execute it.

            Thank you so much for the clue! :)

            Myke

            Comment

            • Andrew @ Rockface

              #7
              Re: php script

              WebRod wrote:[color=blue]
              > Hi,
              >
              > Linux/unix:
              > you just need to add this line as the FIRST LINE in your script:
              > #!/usr/local/bin/php
              >
              > Of course, you need to use the path of your php executable file.
              >
              > You need to change the permission of you file, and add "executable "
              > rights. (you can rename your php file=> cgi, i don't think it's
              > required)
              > Now you only need to create a cron job.
              > It will call your script
              > And that's it![/color]

              You can also cron-call a non-cgi php script by using wget. Unfortunately I
              can't give you an example as I don't have access to my server at the moment
              but the following link should help:

              http://www.gnu.org/software/wget/wget.html

              --
              Andrew @ Rockface



              Comment

              • Ashmodai

                #8
                Re: php script

                Andrew @ Rockface scribbled something along the lines of:[color=blue]
                > WebRod wrote:
                >[color=green]
                >>Hi,
                >>
                >>Linux/unix:
                >>you just need to add this line as the FIRST LINE in your script:
                >>#!/usr/local/bin/php
                >>
                >>Of course, you need to use the path of your php executable file.
                >>
                >>You need to change the permission of you file, and add "executable "
                >>rights. (you can rename your php file=> cgi, i don't think it's
                >>required)
                >>Now you only need to create a cron job.
                >>It will call your script
                >>And that's it![/color]
                >
                >
                > You can also cron-call a non-cgi php script by using wget. Unfortunately I
                > can't give you an example as I don't have access to my server at the moment
                > but the following link should help:
                >
                > http://www.gnu.org/software/wget/wget.html
                >[/color]

                Actually as far as I remember you don't need the initial shebang if you
                run it by passing it to PHP as a parameter.

                Under Linux the man-page of php will tell you the specifics about
                running PHP code via the CLI.

                That way you can run non-CGI PHP scripts via the command line too.

                IIRC a typical call (from within the directory the php file resides in)
                would be:

                php -f ./myphpfile.php

                If you try it with one of your website's PHP files you should see a lot
                of HTML on your screen now.

                Have fun.

                --
                Ashmo

                Comment

                Working...