I'm trying to run a PHP file as a shell script in linux (using /usr/bin/php interpreter). It looks like this:

#!/usr/bin/php -q
<?php
echo "hi";
?>

when i tyr to run, it show the error :

Error in argument 1, char 3: option not found
Error in argument 1, char 3: option not found

whats wrong with my script??

    If you remove the first line from the file, the '#!/usr/bin/php -q'

    and type in the shell 'php -q yourfilename.php'

    Does it work then?

    If it doesn't check if the php interpreter is really in that location. (Mine is in '/usr/local/bin/php')

    Succes,

    Marvin.

    PS, if you edit the file in an window file editor there are usually wrong linebreak characters in your file. This could prevent the file from running. If this is the case try to use the dos2unix command to fix it.

      Your script works for me:

      [etully@localhost ~]$ ./test8.php
      hi [etully@localhost ~]$

      Are you sure you have PHP installed correctly? From the command line, try typing:

      php filename.php

      I get:

      [etully@localhost ~]$ php test8.php
      hi [etully@localhost ~]$

      I'm not sure I can help much more than that.

        /usr/bin/php does not appear to be the path to php. What does...

        ls -l /usr/bin | grep "php"
        

        produce?

        You might also try...

        whereis php
        

          I also want to run a php script from bash, I don't want to side track the topic but I have a little question that falls directly under this category...

          I have a php script, that's a bot. In other words, it runs forever.
          I'm not too good at shell so... After I get it on my shell how can I have it run as a background process? (Doesn't need to be background process of course) In other words, I'm asking how to run/stop it.

          Thanks--

            Tp put something in the background, use &.
            eg;

            php myscript.php &
            

            To kill it however will take a little more work. You might need to use top to get the process id, then kill it that way.

            Its probably easier to write a bash script that will take care of both starting and stoping your daemon though.

              Yeh I know that much, however thanks for the 'php myscript.php &' code 😉

              Didn't know that!

                Hi frikikip, I can run the script by removing the first line '#!/usr/bin/php -q'

                Sorry to say that i'm not very familiar with linux system, one question, without the first line, this script is still consider as shell script?

                Now i use two files to execute my script, one is the php script file and the other one is the script to call "php -q yourfilename.php".

                Its fine for me...i think is the php configuration problem..i'm not the server admin, so i cant do anything on it.. thanks to all for the help...THANKS!!

                  thorpe wrote:

                  Tp put something in the background, use &.
                  eg;

                  php myscript.php &
                  

                  To kill it however will take a little more work. You might need to use top to get the process id, then kill it that way.

                  Its probably easier to write a bash script that will take care of both starting and stoping your daemon though.

                  I have the php script, and I write just what you say but because my script prints stuff, after I write the command I see what it's printing.

                  Plus, when I logged out the script stopped working.

                  So I'm getting the impression that I wasn't able to run it in the backgorund?
                  The host where I bought the shell from says that I can run 1 bg process...

                  But maybe I did something wrong? 🆒

                    Why would your script print stuff if its intended to run in the background? A simple example...

                    test.php

                    #!/usr/bin/php
                    <?php
                    
                    $a = TRUE; 
                    while ($a) {
                        sleep(1);
                    }
                    
                    ?>
                    

                    Now... in the terminal.

                    thorpe@oblivion ~ # test.php &
                    [1] 25358
                    thorpe@oblivion ~ # jobs
                    [1]+  Running            test.php &
                    thorpe@oblivion ~ # kill 25358
                    [1]+  Terminated       test.php
                    

                      Well, someone suggested something so that it writes the output to a file...

                      'php dk.php add > script.log &'

                      Now it works 😃 (I'm pretty sure that was the command)

                        Write a Reply...