17

I am trying to run a shell script using php

shell script ( /home/scripts/fix-perm.sh ) is in the same server

this is the code that i am trying

<?php
echo shell_exec('/home/scripts/fix-perm.sh');
?>

the above code is not working

am using linux server

can anybody please help me?

xdazz
  • 158,678
  • 38
  • 247
  • 274
NidhinRaj
  • 355
  • 2
  • 6
  • 17
  • 5
    Is it returning an error or just not doing anything? Is it throwing a permissions error? What user is the php script running under? More information please. You might also want to try executing 'bash /home/scripts/fix-perm.sh' assuming its a bash script. – IslandCow Sep 13 '11 at 06:30
  • its not showing anything – NidhinRaj Sep 13 '11 at 10:22
  • Use /bin/bash like --> shell_exec("/bin/bash /var/www/html/roshan.sh"); – Yo Yo Roshan Dec 01 '20 at 10:19

4 Answers4

27

Shell exec takes a string which needs to be an actual command. You are now passing it a filepath. This is not interpreted as "execute the file at this path". You could do several things.

What you need to do is call the file with a program. Call it with bash or sh as suggested in the comment:

echo shell_exec('sh /home/scripts/fix-perm.sh');

Another option could be:

$contents = file_get_contents('/home/scripts/fix-perm.sh');
echo shell_exec($contents);

I think the first option would be better however.

It is important to note that all commands for executing external programs expect actual commands and not a filepath or something else. This goes for shell_exec, exec, passthru and others.

hoppa
  • 3,011
  • 18
  • 21
  • I suggest not doing either. Making the file executable is much cleaner. – Jan Hudec Sep 13 '11 at 06:53
  • 1
    i tried both , but its not showing anything , it will return any result ? how to find whether its working or not? – NidhinRaj Sep 13 '11 at 10:29
  • shell_exec returns the output that a command generates. If your script doesn't output anything, there will be no output :) – hoppa Sep 13 '11 at 11:23
  • 1
    @Jan I disagree, invoking the interpreter is more explicit. – IslandCow Sep 13 '11 at 16:29
  • @IslandCow: Invoking interpreter is being explicit at the wrong place. The php script has no business caring whether the helper is written in POSIX shell, bash, zsh, csh, ksh, awk, sed, perl, python, ruby, saphire, chicken or egg. The appropriate place for that is header of that file. – Jan Hudec Sep 14 '11 at 07:50
  • @Jan point conceded. What on earth is "chicken" and "egg"? :P – IslandCow Sep 14 '11 at 15:14
  • 1
    @IslandCow: "chicken" is a version of scheme. I don't know any language called "egg". It's a random placeholder for any other language I never heard of that rhymes with "chicken" :-) – Jan Hudec Sep 15 '11 at 08:29
  • Make sure that the user running the script has access to run the command. For example: if you are running the script via a webpage you'll have to make sure www-data (or whichever user under which Apache runs) has permission to execute the command. – Matthew Jun 13 '18 at 19:10
1

I am not sure but you can try using chmod +x /home/scripts/fix-perm.sh on server at the first then try...

1

first you need to make sure the file has the right permissions

on your server chmod u+x /home/scripts/fix-perm.sh

then you run : echo shell_exec('sh /home/scripts/fix-perm.sh');

or if you want to output the results into a txt file:

shell_exec('sh /home/scripts/fix-perm.sh > /home/scripts/log.txt &');

Marwane Ezzaze
  • 1,032
  • 5
  • 11
0

I got a similar problem evn i read all the posts. I want to execute a file .sh with a path to an another folder, and keep the result in my php page ( I don't want to close the .sh as it is looking foe css change). Here what I try, but nothing append on php side :

    <?php
    echo '<html><head>';
    echo'</head><body>';
        shell_exec('/bin/sh ../include/makeCss/makeWatch.sh'); 
    echo '</body></html>';
?>

and for the makeWatch.sh

 #!/bin/bash
sudo compass watch
ricobolo
  • 9
  • 2