I am trying to run a .sh script when loading my mainpage:
index.php:
<?php
shell_exec('./alert.sh');
?>
<script type='text/javascript'>
window.location = "phpshell.php";
</script>
My script alert.sh:
#! /bin/sh
echo Login: >> log.txt
date >> log.txt
mpg123 alert.mp3
It is working when manually started from nautilus but nothing happen when connecting to via webserver. I am using Ubuntu 11.10 and Apache2.
Default folder for webserver is /var/www
The location of the shellscript is important, and the path you're using are important. Even if
alert.sh
is located at/var/www
, the current working directory can be different. Useecho getcwd();
to get the current working directory. With an absolute path, you would use:The second possibility (most likely) is that the file mode (file permissions) is insufficient. The Apache webserver runs as user
www-data
. If thealert.sh
is owned by you, has your group and has execute permissions for the owner only, the apache server cannot execute it. Possible permissions (practicing the least privilege rule) are:The last option works only if you execute the script like
shell_exec('/bin/sh alert.sh')
. The shell program/bin/sh
only needs to be able to read the script after which the data is executed.