0

I have a PHP script that will be run as cron on Ubuntu server.

I am trying to execute a bash script from PHP script like this :

    exec(escapeshellarg('/bin/bash ') . escapeshellarg("/home/monu/myBash.sh") . escapeshellarg("var1") . escapeshellarg("var2") .escapeshellarg("var3"));

When I run "php myPHP.php" in terminal as user (monu) then I get error like this :

    sh: 1: /bin/bash /home/monu/myBash.sh var1 var2 var3: not found

The contents of myBash.sh are something like :

    export CLASSPATH=./:./lib/xp.jar:./lib/ojdbc14.jar:./lib/log4j-1.2.8.jar:./lib/log4j.properties:./lib/log4j.xml

    cd someDir
    ./install.sh $A $B $C $D
    cd ..

When I manually execute the BASH script from command line it works as expected.

I've even tried system() and shell_exec() but still no luck.

How should I call this BASH script from PHP script to make it work, any hints ?

2
  • 2
    That's because your apache/php user doesn't have permission to access your home folder. Commented Jun 16, 2013 at 21:01
  • @Jon, I am running the PHP script in terminal for debugging purposes while logged in as user (monu). So I guess file permissions are not causing problem. Commented Jun 16, 2013 at 21:15

1 Answer 1

1

escapeshellarg() should be used on each argument, not the command as a whole.

exec(escapeshellarg('/bin/bash') . ' ' . escapeshellarg("/home/monu/myBash.sh") . ' ' . escapeshellarg(...));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the head's up, I've updated the code but still the error persists.

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.