0

I need to run a script that will execute a php file but I can't run the PHP file from Bash Script

Test.sh:

#!/bin/sh
php /home/username/public_html/Test3/Test.php

Where is the error?

/var/log/cron :

Sep  7 22:56:01 srv CROND[4344]: (root) CMD (sh /home/Username/public_html/Test3/test.sh)

Test.php

<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $date = date('h:i:s');
    $sql = "UPDATE deneme SET CRONTEST='".$date."' WHERE id=1";

    $stmt = $conn->prepare($sql);

    $stmt->execute();

    echo $stmt->rowCount() . " records UPDATED successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?> 
11
  • What is the output if you simply type php -v in bash? Commented Sep 7, 2016 at 19:39
  • 2
    Can you print the error you have in console? Commented Sep 7, 2016 at 19:39
  • Have you added x to file? Commented Sep 7, 2016 at 19:40
  • @MarcoSantarossa There are no errors Commented Sep 7, 2016 at 19:50
  • 1
    @DuaneLortie pastebin.com/sFzSbF8T (?) Commented Sep 7, 2016 at 20:04

3 Answers 3

4

Multiple ways to get this working. My first choice, call the PHP script directly from cron. To do that, make sure your php script is executable (chmod 755) Make sure the CLI php script starts with..

#!/usr/bin/php
<?php

From a shell, type 'which php' and change the '/usr/bin...' line accordingly. Once those steps are done, your cron entry might look like . . .

*/5 * * * * /usr/bin/php -f /var/www/script.php

and again, make sure you point to php's actual location.
Tried explaining in comments, ran out of room :/

Sign up to request clarification or add additional context in comments.

Comments

1

I know it's old, but I just ran into this today. Whenever a Bash script does not execute a PHP script (despite having correct permissions and the "php" command working on its own), I found using the full path to PHP resolves it. i.e.

#!/bin/bash

other code
...
/usr/bin/php /home/username/public_html/Test3/Test.php

You can use which php and whereis php to determine the correct path.

Comments

0

I solved my question this method:

#!/bin/bash

data=$(/usr/bin/php -q /home/username/public_html/Test3/Test.php);

But I can't variables, for example:

#!/bin/bash

data=$(/usr/bin/php -q /home/username/public_html/Test3/Test.php name=Saracoglu);

Its not working. I'm searching..

2 Comments

Isn't this the same thing i have written in my answer?
@heemayl You don't have in answer data=$( binPath -q path );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.