Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am using a command to start a screen session and run a bash script for a user.

/bin/su username -c "/usr/bin/screen -dmS test bash -c '/home/username/test.sh; exec bash'"

When I boot, I am seeing the screen session appear for that user, but when I open it, I see a permission denied error.

I checked permissions on the file and it is: -rw-rw-r-- 1 username www-data 816 Feb 18 00:59 test.sh

Anyone have a theory why I am seeing this error? I can run the script no problem when I am logged in as username.

share|improve this question
add comment

1 Answer 1

test.sh is not executable. So you can run it by passing it as an argument to a shell, as in bash /home/username/test.sh, but you can't run it directly as a program like in your example command. Run chmod +x /home/username/test.sh to make it executable.

If test.sh is doing things like setting environment variables, they only survive for the duration of the process. So if you run /home/username/test.sh; exec bash then the final interactive shell won't have the environment variables from that process. To keep the environment variables, set them in the parent bash process: source the script, instead of running it as a separate program.

/bin/su username -c "/usr/bin/screen -dmS test bash -c '. ~/test.sh; exec bash'"
share|improve this answer
add comment

Your Answer

 
discard

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.