0

We have a script that is executed by httpd as the default ec2-user. However when executed the script does not see any of the environmental variables for that user

the variable is set under user ec2-user

myUseVarHome=/home/ec2-user

myScript.sh

#!/bin/bash
myFolder="${myUseVarHome}/test/www"
USER=$(whoami)
echo "Content-type: text/html"
echo ""
echo "hello $USER"
echo "myFolder=$myFolder"

executing script as ec2-user outputs

hello ec2-user
myFolder=/home/ec2-user/test/www

We then set httpd 2.4 conf

<IfModule unixd_module>
    User ec2-user
    Group ec2-user
</IfModule>

now call the script with

wget 127.0.0.1/myScript.sh

outputs

hello ec2-user
myFolder=/test/www

The output validates the httpd user is ec2-user, same as manually executing the script, however the env variable ${myUseVarHome} is blank or does not exist.

Is this expected behaviour or do we need to call the env variable another way when executed as httpd user?

1
  • 2
    Yes, this is expected behavior. Environment variables are per process, not per user. Commented Jan 18, 2018 at 22:06

1 Answer 1

1

bash acts differently whether it is a shell or a normal progamming language (like perl or python).

By designed, those settings in ~/.bash_profile, ~/.bashrc, etc. are for users to set things when bash plays the roll of a shell (login shell, interractive shell). Think about environment you have in a xterm (interractive shell) or in ssh sessions (login shell) or in consoles (login shell).

In other hand, bash is also a powerfull progamming language --think about many scripts for managing services in systemd-- which requires a different style of working. Example, when a developer write a system script or a bash program, he/she will not likely to source user defined ~/.bash_profile automatically. It is a normal program, not a shell. A normal program (including bash programs) would naturally inherrit setting in a current working evironement (shell), but not set them.

If we write a program for cron in bash --it is just happenly it is written in bash; in fact, we can write it in python or perl or any other progamming language-- then, we can have an option to sources bash's ~/.bash_profile (read: setting of user's shell, which happenly to be the same language of your programming language):

[ -f /home/user/.bash_profile ] && . /home/user/.bash_profile

However, what if that particular user do not use bash as his/her shell? He/she may use zsh, 'ksh,fish`, etc. So, that's practice does not really work when writing program for public use.

So, you can source ~/.bash_profile if you think it work. But, here, it is not about whether we are able to source a file, it is about how things should works in the system: the design concept. In short: we should view bash as something having 2 rolls: shell and progamming language. Then everything will be clear, easier to understand.

See: How to change cron shell sh to bash

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.