Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to run a .sh script when loading my mainpage:



<script type='text/javascript'>
window.location = "phpshell.php";

My script

#! /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

share|improve this question
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
up vote 1 down vote accepted

The location of the shellscript is important, and the path you're using are important. Even if 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:


The second possibility (most likely) is that the file mode (file permissions) is insufficient. The Apache webserver runs as user www-data. If the 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'). The shell program /bin/sh only needs to be able to read the script after which the data is executed.

share|improve this answer
This is what I get when running from a php console I put on the website: – 01BTC10 Jan 22 '12 at 21:00
$ ./ ./ 3: cannot create log.txt: Permission denied ./ 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 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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.