How to execute script on my Ubuntu via PHP from site












1















How can I use PHP to execute a script on my Ubuntu server? The site is hosted on the same machine obviously.



I read about exec_shell() and tried this



<?php
$output = shell_exec("ls /var/www");
echo "<h1>Output: " . $output . "</h1>";
?>


But it didn't echo anything on the site, have I misunderstood the function?



Plan is to make a script run on my Ubuntu PC, restarting a few game servers once I click a button on the website.










share|improve this question

























  • According to the documentation: "The output from the executed command or NULL if an error occurred or the command produces no output." Potentially an error have occurred, likely because the process runs with username that does not have access to /var/www. Check the permissions returned with ls -ld /var/www in shell, perhaps also try different command in shell_exec().

    – Sergiy Kolodyazhnyy
    Jan 18 at 23:41











  • Try $output = shell_exec('ls /var/www'); and echo "<h1>Output: $output</h1>";, that’s how the manual does it.

    – dessert
    Jan 18 at 23:41













  • @SergiyKolodyazhnyy What user is used for the shell_exec() commands?

    – Jonathan Öhrström
    Jan 18 at 23:50











  • @dessert Didn't work either, sadly

    – Jonathan Öhrström
    Jan 18 at 23:50






  • 1





    @JonathanÖhrström, you said "But it didn"t echo anything on the site", but if the discussed function fail you must see, at least, <h1>Output:</h1> in the browser - doesn't it?

    – pa4080
    Jan 19 at 8:47


















1















How can I use PHP to execute a script on my Ubuntu server? The site is hosted on the same machine obviously.



I read about exec_shell() and tried this



<?php
$output = shell_exec("ls /var/www");
echo "<h1>Output: " . $output . "</h1>";
?>


But it didn't echo anything on the site, have I misunderstood the function?



Plan is to make a script run on my Ubuntu PC, restarting a few game servers once I click a button on the website.










share|improve this question

























  • According to the documentation: "The output from the executed command or NULL if an error occurred or the command produces no output." Potentially an error have occurred, likely because the process runs with username that does not have access to /var/www. Check the permissions returned with ls -ld /var/www in shell, perhaps also try different command in shell_exec().

    – Sergiy Kolodyazhnyy
    Jan 18 at 23:41











  • Try $output = shell_exec('ls /var/www'); and echo "<h1>Output: $output</h1>";, that’s how the manual does it.

    – dessert
    Jan 18 at 23:41













  • @SergiyKolodyazhnyy What user is used for the shell_exec() commands?

    – Jonathan Öhrström
    Jan 18 at 23:50











  • @dessert Didn't work either, sadly

    – Jonathan Öhrström
    Jan 18 at 23:50






  • 1





    @JonathanÖhrström, you said "But it didn"t echo anything on the site", but if the discussed function fail you must see, at least, <h1>Output:</h1> in the browser - doesn't it?

    – pa4080
    Jan 19 at 8:47
















1












1








1


1






How can I use PHP to execute a script on my Ubuntu server? The site is hosted on the same machine obviously.



I read about exec_shell() and tried this



<?php
$output = shell_exec("ls /var/www");
echo "<h1>Output: " . $output . "</h1>";
?>


But it didn't echo anything on the site, have I misunderstood the function?



Plan is to make a script run on my Ubuntu PC, restarting a few game servers once I click a button on the website.










share|improve this question
















How can I use PHP to execute a script on my Ubuntu server? The site is hosted on the same machine obviously.



I read about exec_shell() and tried this



<?php
$output = shell_exec("ls /var/www");
echo "<h1>Output: " . $output . "</h1>";
?>


But it didn't echo anything on the site, have I misunderstood the function?



Plan is to make a script run on my Ubuntu PC, restarting a few game servers once I click a button on the website.







bash scripts apache2 php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 8:03









Codito ergo sum

1,5043825




1,5043825










asked Jan 18 at 23:36









Jonathan ÖhrströmJonathan Öhrström

12215




12215













  • According to the documentation: "The output from the executed command or NULL if an error occurred or the command produces no output." Potentially an error have occurred, likely because the process runs with username that does not have access to /var/www. Check the permissions returned with ls -ld /var/www in shell, perhaps also try different command in shell_exec().

    – Sergiy Kolodyazhnyy
    Jan 18 at 23:41











  • Try $output = shell_exec('ls /var/www'); and echo "<h1>Output: $output</h1>";, that’s how the manual does it.

    – dessert
    Jan 18 at 23:41













  • @SergiyKolodyazhnyy What user is used for the shell_exec() commands?

    – Jonathan Öhrström
    Jan 18 at 23:50











  • @dessert Didn't work either, sadly

    – Jonathan Öhrström
    Jan 18 at 23:50






  • 1





    @JonathanÖhrström, you said "But it didn"t echo anything on the site", but if the discussed function fail you must see, at least, <h1>Output:</h1> in the browser - doesn't it?

    – pa4080
    Jan 19 at 8:47





















  • According to the documentation: "The output from the executed command or NULL if an error occurred or the command produces no output." Potentially an error have occurred, likely because the process runs with username that does not have access to /var/www. Check the permissions returned with ls -ld /var/www in shell, perhaps also try different command in shell_exec().

    – Sergiy Kolodyazhnyy
    Jan 18 at 23:41











  • Try $output = shell_exec('ls /var/www'); and echo "<h1>Output: $output</h1>";, that’s how the manual does it.

    – dessert
    Jan 18 at 23:41













  • @SergiyKolodyazhnyy What user is used for the shell_exec() commands?

    – Jonathan Öhrström
    Jan 18 at 23:50











  • @dessert Didn't work either, sadly

    – Jonathan Öhrström
    Jan 18 at 23:50






  • 1





    @JonathanÖhrström, you said "But it didn"t echo anything on the site", but if the discussed function fail you must see, at least, <h1>Output:</h1> in the browser - doesn't it?

    – pa4080
    Jan 19 at 8:47



















According to the documentation: "The output from the executed command or NULL if an error occurred or the command produces no output." Potentially an error have occurred, likely because the process runs with username that does not have access to /var/www. Check the permissions returned with ls -ld /var/www in shell, perhaps also try different command in shell_exec().

– Sergiy Kolodyazhnyy
Jan 18 at 23:41





According to the documentation: "The output from the executed command or NULL if an error occurred or the command produces no output." Potentially an error have occurred, likely because the process runs with username that does not have access to /var/www. Check the permissions returned with ls -ld /var/www in shell, perhaps also try different command in shell_exec().

– Sergiy Kolodyazhnyy
Jan 18 at 23:41













Try $output = shell_exec('ls /var/www'); and echo "<h1>Output: $output</h1>";, that’s how the manual does it.

– dessert
Jan 18 at 23:41







Try $output = shell_exec('ls /var/www'); and echo "<h1>Output: $output</h1>";, that’s how the manual does it.

– dessert
Jan 18 at 23:41















@SergiyKolodyazhnyy What user is used for the shell_exec() commands?

– Jonathan Öhrström
Jan 18 at 23:50





@SergiyKolodyazhnyy What user is used for the shell_exec() commands?

– Jonathan Öhrström
Jan 18 at 23:50













@dessert Didn't work either, sadly

– Jonathan Öhrström
Jan 18 at 23:50





@dessert Didn't work either, sadly

– Jonathan Öhrström
Jan 18 at 23:50




1




1





@JonathanÖhrström, you said "But it didn"t echo anything on the site", but if the discussed function fail you must see, at least, <h1>Output:</h1> in the browser - doesn't it?

– pa4080
Jan 19 at 8:47







@JonathanÖhrström, you said "But it didn"t echo anything on the site", but if the discussed function fail you must see, at least, <h1>Output:</h1> in the browser - doesn't it?

– pa4080
Jan 19 at 8:47












1 Answer
1






active

oldest

votes


















2





+25









I've tested your code and in my server it works correctly. It gives the following output:



Output: apps awstats cgi-bin clients conf error html icons ispconfig manual mauco.org med-01.uc.cl php-fcgi-scripts usage


That are the folders in /var/www



I think you may have a privileges issue. What happens if you try with this command:



  $output = shell_exec("ls /tmp");


You can also check if what user is being used by apache/nginx



 $output = shell_exec("whoami");


Please include screenshots of your results.






share|improve this answer
























  • passthru(); seems to work fine, but still shell_exec doesn't. Using passhthru I found out that apache is using www-data as its user

    – Jonathan Öhrström
    Jan 26 at 23:16













  • This $output = shell_exec("ls /tmp"); gives you no output?

    – Philippe Delteil
    Jan 27 at 0:59











  • Correct. Doesn't work at all

    – Jonathan Öhrström
    Jan 27 at 16:22











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1111009%2fhow-to-execute-script-on-my-ubuntu-via-php-from-site%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2





+25









I've tested your code and in my server it works correctly. It gives the following output:



Output: apps awstats cgi-bin clients conf error html icons ispconfig manual mauco.org med-01.uc.cl php-fcgi-scripts usage


That are the folders in /var/www



I think you may have a privileges issue. What happens if you try with this command:



  $output = shell_exec("ls /tmp");


You can also check if what user is being used by apache/nginx



 $output = shell_exec("whoami");


Please include screenshots of your results.






share|improve this answer
























  • passthru(); seems to work fine, but still shell_exec doesn't. Using passhthru I found out that apache is using www-data as its user

    – Jonathan Öhrström
    Jan 26 at 23:16













  • This $output = shell_exec("ls /tmp"); gives you no output?

    – Philippe Delteil
    Jan 27 at 0:59











  • Correct. Doesn't work at all

    – Jonathan Öhrström
    Jan 27 at 16:22
















2





+25









I've tested your code and in my server it works correctly. It gives the following output:



Output: apps awstats cgi-bin clients conf error html icons ispconfig manual mauco.org med-01.uc.cl php-fcgi-scripts usage


That are the folders in /var/www



I think you may have a privileges issue. What happens if you try with this command:



  $output = shell_exec("ls /tmp");


You can also check if what user is being used by apache/nginx



 $output = shell_exec("whoami");


Please include screenshots of your results.






share|improve this answer
























  • passthru(); seems to work fine, but still shell_exec doesn't. Using passhthru I found out that apache is using www-data as its user

    – Jonathan Öhrström
    Jan 26 at 23:16













  • This $output = shell_exec("ls /tmp"); gives you no output?

    – Philippe Delteil
    Jan 27 at 0:59











  • Correct. Doesn't work at all

    – Jonathan Öhrström
    Jan 27 at 16:22














2





+25







2





+25



2




+25





I've tested your code and in my server it works correctly. It gives the following output:



Output: apps awstats cgi-bin clients conf error html icons ispconfig manual mauco.org med-01.uc.cl php-fcgi-scripts usage


That are the folders in /var/www



I think you may have a privileges issue. What happens if you try with this command:



  $output = shell_exec("ls /tmp");


You can also check if what user is being used by apache/nginx



 $output = shell_exec("whoami");


Please include screenshots of your results.






share|improve this answer













I've tested your code and in my server it works correctly. It gives the following output:



Output: apps awstats cgi-bin clients conf error html icons ispconfig manual mauco.org med-01.uc.cl php-fcgi-scripts usage


That are the folders in /var/www



I think you may have a privileges issue. What happens if you try with this command:



  $output = shell_exec("ls /tmp");


You can also check if what user is being used by apache/nginx



 $output = shell_exec("whoami");


Please include screenshots of your results.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 25 at 15:40









Philippe DelteilPhilippe Delteil

4841418




4841418













  • passthru(); seems to work fine, but still shell_exec doesn't. Using passhthru I found out that apache is using www-data as its user

    – Jonathan Öhrström
    Jan 26 at 23:16













  • This $output = shell_exec("ls /tmp"); gives you no output?

    – Philippe Delteil
    Jan 27 at 0:59











  • Correct. Doesn't work at all

    – Jonathan Öhrström
    Jan 27 at 16:22



















  • passthru(); seems to work fine, but still shell_exec doesn't. Using passhthru I found out that apache is using www-data as its user

    – Jonathan Öhrström
    Jan 26 at 23:16













  • This $output = shell_exec("ls /tmp"); gives you no output?

    – Philippe Delteil
    Jan 27 at 0:59











  • Correct. Doesn't work at all

    – Jonathan Öhrström
    Jan 27 at 16:22

















passthru(); seems to work fine, but still shell_exec doesn't. Using passhthru I found out that apache is using www-data as its user

– Jonathan Öhrström
Jan 26 at 23:16







passthru(); seems to work fine, but still shell_exec doesn't. Using passhthru I found out that apache is using www-data as its user

– Jonathan Öhrström
Jan 26 at 23:16















This $output = shell_exec("ls /tmp"); gives you no output?

– Philippe Delteil
Jan 27 at 0:59





This $output = shell_exec("ls /tmp"); gives you no output?

– Philippe Delteil
Jan 27 at 0:59













Correct. Doesn't work at all

– Jonathan Öhrström
Jan 27 at 16:22





Correct. Doesn't work at all

– Jonathan Öhrström
Jan 27 at 16:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1111009%2fhow-to-execute-script-on-my-ubuntu-via-php-from-site%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?