1

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

01BTC10
  • 681
  • 2
  • 12
  • 22
  • 1
    Can you try to explain what you are trying to do? Are you trying to play an alert on the server when someone connects or on the client? What is it you need to do that PHP can't do? – sakjur Jan 22 '12 at 20:50
  • I want to play an alert when the website page load and create a simple txt log. – 01BTC10 Jan 22 '12 at 21:07
  • The sound should play server side. – 01BTC10 Jan 22 '12 at 21:27

1 Answers1

1

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. Use echo getcwd(); to get the current working directory. With an absolute path, you would use:

shell_exec('/var/www/alert.sh');

The second possibility (most likely) is that the file mode (file permissions) is insufficient. The Apache webserver runs as user www-data. If the alert.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:

  • 755 - owner: you - group: you
  • 750 - owner: you - group: www-data
  • 644 or 640 - owner: you - group: you or www-data

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.

Lekensteyn
  • 174,277
  • This is what I get when running alert.sh from a php console I put on the website: – 01BTC10 Jan 22 '12 at 21:00
  • $ ./alert.sh ./alert.sh: 3: cannot create log.txt: Permission denied ./alert.sh: 4: cannot create log.txt: Permission denied Home directory /var/www not ours. – 01BTC10 Jan 22 '12 at 21:03
  • log.txt must be writable by www-data. Create it with: sudo touch /var/www && sudo chgrp www-data log.txt && sudo chmod log.txt. See also http://askubuntu.com/q/46331/6969 for avoiding sudo – Lekensteyn Jan 22 '12 at 22:27
  • Thank you! Now the log.txt is updating but I dont have sound access with mpg123 player. Any hint how to get sound working? – 01BTC10 Jan 22 '12 at 22:48
  • I've no idea how to run audio as a non-desktop user. If the question has not been asked before, start a new question. Aside, do you really want to allow a webserver to write indefinitely to a file and play audio? – Lekensteyn Jan 22 '12 at 23:01
  • Yes it's a honeypot so I want the sound as an alert. Thank's! – 01BTC10 Jan 22 '12 at 23:08