This is an odd one, it only happens on one server (shared hosting, so I have limited access to configuration and logs), but I can't wrap my head around it.

I have a shell script which runs some app's deployment. The file has a +x flag, but I can't run it by simply calling its name from shell, I get an "access denied" error. Invoking bash with the filename as an arguments works like a charm. It looks something like that:

[someuser@someserver:~] $cat test.sh
echo "hooray!"
[someuser@someserver:~] $chmod +x test.sh
[someuser@someserver:~] $ls -l test.sh
-rwxrwxr-x 1 someuser someuser 14 Oct  8 11:27 test.sh
[someuser@someserver:~] $./test.sh
-bash: ./test.sh: Permission denied
[someuser@someserver:~] $bash test.sh

Adding a #!/bin/bash at the beginning doesn't help. I also tried from other shell (default is bash, tried in sh), same thing happened.

This is not that big of a problem, but it's baffling for me. What could be the cause?

  • 2
    The #! line in the beginning of a script is mandatory. If you leave it out, the kernel will refuse to run the script. Some shells have a workaround, which will allow them to run the script anyway. However those workarounds will cause the script to behave differently depending on how it was called and break completely if it wasn't called from a shell with a workaround. For those reasons you should never rely on those workarounds and instead include a correctly formatted #! line. – kasperd Oct 8 '14 at 10:04

I think it is because of the "noexec" mount option for the folder your script is in. You may try to check that by this command

mount | grep `df -P /path/to/folder/with/script | tail -1 | cut -d ' ' -f 1`
| improve this answer | |
  • How do I verify this? – jkondratowicz Oct 8 '14 at 9:51
  • Added command to my answer. You may have look at /etc/fstab. According to your example you are in a home directory, so you'll see a /home mountpoint. There should be noexec directive at the end of mountpoint's options. – Glueon Oct 8 '14 at 9:58
  • As I said this is a shared hosting, so for security reasons there's not much I can do. Mount command is not available to me, /etc/fstab doesn't exist (or that's what it looks like from my perspective). Some interesting config... – jkondratowicz Oct 8 '14 at 10:01
  • 1
    Sorry, missed the shared hosting bit. Then you have nothing to do as your shared hosting provider mounted home directories with the noexec option for security reasons. – Glueon Oct 8 '14 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy