Is it possible to run a command within a login shell using sudo or su?

Clash Royale CLAN TAG#URR8PPP up vote
0
down vote
favorite
I'm trying to run a non-interactive command within a login shell (using su - or sudo -i) but couldn't make it work so far.
Within ~/.bashrc I have sourced a script which sets all variables my application needs. When I do an interactive login su - username I can see all my variables there and if I run my application it works fine. However, when I try to execute my application by using su - username -c 'command' the necessary variables are not being set. In other words, it seems su - is incompatible with -c option. The same happens with sudo -i -u user command.
I have searched tons of questions but couldn't find an answer to this specific behavior.
According to bash man pages:
When bash is invoked as an interactive login shell, or as a non-interactive
shell with the `--login` option, it first reads and executes commands from the
file `/etc/profile`, if that file exists. After reading that file, it looks for
`~/.bash_profile`, `~/.bash_login`, and `~/.profile`, in that order, and reads and
executes commands from the first one that exists and is readable. The
`--noprofile` option may be used when the shell is started to inhibit this
behavior.
The above behavior is not working as ~/.profile executes ~/.bashrc and it in turn sources the script which sets all variables.
How can I execute a non-interactive login shell?
Thank you
bash login sudo su
add a comment |Â
up vote
0
down vote
favorite
I'm trying to run a non-interactive command within a login shell (using su - or sudo -i) but couldn't make it work so far.
Within ~/.bashrc I have sourced a script which sets all variables my application needs. When I do an interactive login su - username I can see all my variables there and if I run my application it works fine. However, when I try to execute my application by using su - username -c 'command' the necessary variables are not being set. In other words, it seems su - is incompatible with -c option. The same happens with sudo -i -u user command.
I have searched tons of questions but couldn't find an answer to this specific behavior.
According to bash man pages:
When bash is invoked as an interactive login shell, or as a non-interactive
shell with the `--login` option, it first reads and executes commands from the
file `/etc/profile`, if that file exists. After reading that file, it looks for
`~/.bash_profile`, `~/.bash_login`, and `~/.profile`, in that order, and reads and
executes commands from the first one that exists and is readable. The
`--noprofile` option may be used when the shell is started to inhibit this
behavior.
The above behavior is not working as ~/.profile executes ~/.bashrc and it in turn sources the script which sets all variables.
How can I execute a non-interactive login shell?
Thank you
bash login sudo su
Might be helpful: askubuntu.com/questions/879364/â¦
â d a i s y
Feb 28 at 8:53
Thank you for the reference @daisy, very good explanation. My understanding about the behavior of a login shell (bash) is that it to goes through /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile and a non-interactive shell is when a script os executable is run directly by the shell (no prompt) as "su -c" does. So I was wondering that "su - (with -c) option would execute a non-interactive login shell, but it seems it doesn't. Is my understanding incorrect?
â felima
Feb 28 at 9:37
Doessu - username -c 'bash -l -c command'work? With a login-shell (bash -l) in-between?
â PerlDuck
Feb 28 at 10:14
Thank you @PerlDuck it worked indeed. Got focused on understanding about su/sudo behavior and this great workaround never came to mind.
â felima
Mar 1 at 21:37
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to run a non-interactive command within a login shell (using su - or sudo -i) but couldn't make it work so far.
Within ~/.bashrc I have sourced a script which sets all variables my application needs. When I do an interactive login su - username I can see all my variables there and if I run my application it works fine. However, when I try to execute my application by using su - username -c 'command' the necessary variables are not being set. In other words, it seems su - is incompatible with -c option. The same happens with sudo -i -u user command.
I have searched tons of questions but couldn't find an answer to this specific behavior.
According to bash man pages:
When bash is invoked as an interactive login shell, or as a non-interactive
shell with the `--login` option, it first reads and executes commands from the
file `/etc/profile`, if that file exists. After reading that file, it looks for
`~/.bash_profile`, `~/.bash_login`, and `~/.profile`, in that order, and reads and
executes commands from the first one that exists and is readable. The
`--noprofile` option may be used when the shell is started to inhibit this
behavior.
The above behavior is not working as ~/.profile executes ~/.bashrc and it in turn sources the script which sets all variables.
How can I execute a non-interactive login shell?
Thank you
bash login sudo su
I'm trying to run a non-interactive command within a login shell (using su - or sudo -i) but couldn't make it work so far.
Within ~/.bashrc I have sourced a script which sets all variables my application needs. When I do an interactive login su - username I can see all my variables there and if I run my application it works fine. However, when I try to execute my application by using su - username -c 'command' the necessary variables are not being set. In other words, it seems su - is incompatible with -c option. The same happens with sudo -i -u user command.
I have searched tons of questions but couldn't find an answer to this specific behavior.
According to bash man pages:
When bash is invoked as an interactive login shell, or as a non-interactive
shell with the `--login` option, it first reads and executes commands from the
file `/etc/profile`, if that file exists. After reading that file, it looks for
`~/.bash_profile`, `~/.bash_login`, and `~/.profile`, in that order, and reads and
executes commands from the first one that exists and is readable. The
`--noprofile` option may be used when the shell is started to inhibit this
behavior.
The above behavior is not working as ~/.profile executes ~/.bashrc and it in turn sources the script which sets all variables.
How can I execute a non-interactive login shell?
Thank you
bash login sudo su
bash login sudo su
edited Feb 28 at 10:04
Félicien
8471516
8471516
asked Feb 28 at 8:22
felima
11
11
Might be helpful: askubuntu.com/questions/879364/â¦
â d a i s y
Feb 28 at 8:53
Thank you for the reference @daisy, very good explanation. My understanding about the behavior of a login shell (bash) is that it to goes through /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile and a non-interactive shell is when a script os executable is run directly by the shell (no prompt) as "su -c" does. So I was wondering that "su - (with -c) option would execute a non-interactive login shell, but it seems it doesn't. Is my understanding incorrect?
â felima
Feb 28 at 9:37
Doessu - username -c 'bash -l -c command'work? With a login-shell (bash -l) in-between?
â PerlDuck
Feb 28 at 10:14
Thank you @PerlDuck it worked indeed. Got focused on understanding about su/sudo behavior and this great workaround never came to mind.
â felima
Mar 1 at 21:37
add a comment |Â
Might be helpful: askubuntu.com/questions/879364/â¦
â d a i s y
Feb 28 at 8:53
Thank you for the reference @daisy, very good explanation. My understanding about the behavior of a login shell (bash) is that it to goes through /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile and a non-interactive shell is when a script os executable is run directly by the shell (no prompt) as "su -c" does. So I was wondering that "su - (with -c) option would execute a non-interactive login shell, but it seems it doesn't. Is my understanding incorrect?
â felima
Feb 28 at 9:37
Doessu - username -c 'bash -l -c command'work? With a login-shell (bash -l) in-between?
â PerlDuck
Feb 28 at 10:14
Thank you @PerlDuck it worked indeed. Got focused on understanding about su/sudo behavior and this great workaround never came to mind.
â felima
Mar 1 at 21:37
Might be helpful: askubuntu.com/questions/879364/â¦
â d a i s y
Feb 28 at 8:53
Might be helpful: askubuntu.com/questions/879364/â¦
â d a i s y
Feb 28 at 8:53
Thank you for the reference @daisy, very good explanation. My understanding about the behavior of a login shell (bash) is that it to goes through /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile and a non-interactive shell is when a script os executable is run directly by the shell (no prompt) as "su -c" does. So I was wondering that "su - (with -c) option would execute a non-interactive login shell, but it seems it doesn't. Is my understanding incorrect?
â felima
Feb 28 at 9:37
Thank you for the reference @daisy, very good explanation. My understanding about the behavior of a login shell (bash) is that it to goes through /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile and a non-interactive shell is when a script os executable is run directly by the shell (no prompt) as "su -c" does. So I was wondering that "su - (with -c) option would execute a non-interactive login shell, but it seems it doesn't. Is my understanding incorrect?
â felima
Feb 28 at 9:37
Does
su - username -c 'bash -l -c command' work? With a login-shell (bash -l) in-between?â PerlDuck
Feb 28 at 10:14
Does
su - username -c 'bash -l -c command' work? With a login-shell (bash -l) in-between?â PerlDuck
Feb 28 at 10:14
Thank you @PerlDuck it worked indeed. Got focused on understanding about su/sudo behavior and this great workaround never came to mind.
â felima
Mar 1 at 21:37
Thank you @PerlDuck it worked indeed. Got focused on understanding about su/sudo behavior and this great workaround never came to mind.
â felima
Mar 1 at 21:37
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can source the variable file in your script. In your script, include the following line before referencing any of the variables:
source full_path_to_variable_file
This will read your variables into the script's session. You must use the full literal path (no aliases), starting with the root directory'/', so for example if your home directory's .bash_aliases defines your variables:
source /home/felima/.bash_aliases
On the command line '.' has the same effect
. ~/.bash_aliases
But in scripts using 'source' is more clear.
Hi @StephenBoston I failed to mention that application executable is started by a script in /etc/init.d (which cannot be changed due to support requirements) and it expects to have all variables set by the user environment. So the script which sets all variables was added to the last line of ~/.bashrc as you mentioned "source full_path_of_script_which-sets_all_vars", so I understand ~/.bashrc is not being run on non-interactive login. So the question is how we make the non-interactive shell with the --login option work (as described by the bash man page)?
â felima
Feb 28 at 8:57
Ah! What a nuisance. I am sorry. So you do not have write access to the script. It is a shared file? Then does the script work for other users, in other contexts?
â Stephen Boston
Feb 28 at 10:23
Yes, I can't change them. Anyway, @PerlDuck suggestion worked like a charm. Thank you for your comments.
â felima
Mar 1 at 21:40
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can source the variable file in your script. In your script, include the following line before referencing any of the variables:
source full_path_to_variable_file
This will read your variables into the script's session. You must use the full literal path (no aliases), starting with the root directory'/', so for example if your home directory's .bash_aliases defines your variables:
source /home/felima/.bash_aliases
On the command line '.' has the same effect
. ~/.bash_aliases
But in scripts using 'source' is more clear.
Hi @StephenBoston I failed to mention that application executable is started by a script in /etc/init.d (which cannot be changed due to support requirements) and it expects to have all variables set by the user environment. So the script which sets all variables was added to the last line of ~/.bashrc as you mentioned "source full_path_of_script_which-sets_all_vars", so I understand ~/.bashrc is not being run on non-interactive login. So the question is how we make the non-interactive shell with the --login option work (as described by the bash man page)?
â felima
Feb 28 at 8:57
Ah! What a nuisance. I am sorry. So you do not have write access to the script. It is a shared file? Then does the script work for other users, in other contexts?
â Stephen Boston
Feb 28 at 10:23
Yes, I can't change them. Anyway, @PerlDuck suggestion worked like a charm. Thank you for your comments.
â felima
Mar 1 at 21:40
add a comment |Â
up vote
0
down vote
You can source the variable file in your script. In your script, include the following line before referencing any of the variables:
source full_path_to_variable_file
This will read your variables into the script's session. You must use the full literal path (no aliases), starting with the root directory'/', so for example if your home directory's .bash_aliases defines your variables:
source /home/felima/.bash_aliases
On the command line '.' has the same effect
. ~/.bash_aliases
But in scripts using 'source' is more clear.
Hi @StephenBoston I failed to mention that application executable is started by a script in /etc/init.d (which cannot be changed due to support requirements) and it expects to have all variables set by the user environment. So the script which sets all variables was added to the last line of ~/.bashrc as you mentioned "source full_path_of_script_which-sets_all_vars", so I understand ~/.bashrc is not being run on non-interactive login. So the question is how we make the non-interactive shell with the --login option work (as described by the bash man page)?
â felima
Feb 28 at 8:57
Ah! What a nuisance. I am sorry. So you do not have write access to the script. It is a shared file? Then does the script work for other users, in other contexts?
â Stephen Boston
Feb 28 at 10:23
Yes, I can't change them. Anyway, @PerlDuck suggestion worked like a charm. Thank you for your comments.
â felima
Mar 1 at 21:40
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can source the variable file in your script. In your script, include the following line before referencing any of the variables:
source full_path_to_variable_file
This will read your variables into the script's session. You must use the full literal path (no aliases), starting with the root directory'/', so for example if your home directory's .bash_aliases defines your variables:
source /home/felima/.bash_aliases
On the command line '.' has the same effect
. ~/.bash_aliases
But in scripts using 'source' is more clear.
You can source the variable file in your script. In your script, include the following line before referencing any of the variables:
source full_path_to_variable_file
This will read your variables into the script's session. You must use the full literal path (no aliases), starting with the root directory'/', so for example if your home directory's .bash_aliases defines your variables:
source /home/felima/.bash_aliases
On the command line '.' has the same effect
. ~/.bash_aliases
But in scripts using 'source' is more clear.
edited Feb 28 at 8:41
answered Feb 28 at 8:30
Stephen Boston
6081516
6081516
Hi @StephenBoston I failed to mention that application executable is started by a script in /etc/init.d (which cannot be changed due to support requirements) and it expects to have all variables set by the user environment. So the script which sets all variables was added to the last line of ~/.bashrc as you mentioned "source full_path_of_script_which-sets_all_vars", so I understand ~/.bashrc is not being run on non-interactive login. So the question is how we make the non-interactive shell with the --login option work (as described by the bash man page)?
â felima
Feb 28 at 8:57
Ah! What a nuisance. I am sorry. So you do not have write access to the script. It is a shared file? Then does the script work for other users, in other contexts?
â Stephen Boston
Feb 28 at 10:23
Yes, I can't change them. Anyway, @PerlDuck suggestion worked like a charm. Thank you for your comments.
â felima
Mar 1 at 21:40
add a comment |Â
Hi @StephenBoston I failed to mention that application executable is started by a script in /etc/init.d (which cannot be changed due to support requirements) and it expects to have all variables set by the user environment. So the script which sets all variables was added to the last line of ~/.bashrc as you mentioned "source full_path_of_script_which-sets_all_vars", so I understand ~/.bashrc is not being run on non-interactive login. So the question is how we make the non-interactive shell with the --login option work (as described by the bash man page)?
â felima
Feb 28 at 8:57
Ah! What a nuisance. I am sorry. So you do not have write access to the script. It is a shared file? Then does the script work for other users, in other contexts?
â Stephen Boston
Feb 28 at 10:23
Yes, I can't change them. Anyway, @PerlDuck suggestion worked like a charm. Thank you for your comments.
â felima
Mar 1 at 21:40
Hi @StephenBoston I failed to mention that application executable is started by a script in /etc/init.d (which cannot be changed due to support requirements) and it expects to have all variables set by the user environment. So the script which sets all variables was added to the last line of ~/.bashrc as you mentioned "source full_path_of_script_which-sets_all_vars", so I understand ~/.bashrc is not being run on non-interactive login. So the question is how we make the non-interactive shell with the --login option work (as described by the bash man page)?
â felima
Feb 28 at 8:57
Hi @StephenBoston I failed to mention that application executable is started by a script in /etc/init.d (which cannot be changed due to support requirements) and it expects to have all variables set by the user environment. So the script which sets all variables was added to the last line of ~/.bashrc as you mentioned "source full_path_of_script_which-sets_all_vars", so I understand ~/.bashrc is not being run on non-interactive login. So the question is how we make the non-interactive shell with the --login option work (as described by the bash man page)?
â felima
Feb 28 at 8:57
Ah! What a nuisance. I am sorry. So you do not have write access to the script. It is a shared file? Then does the script work for other users, in other contexts?
â Stephen Boston
Feb 28 at 10:23
Ah! What a nuisance. I am sorry. So you do not have write access to the script. It is a shared file? Then does the script work for other users, in other contexts?
â Stephen Boston
Feb 28 at 10:23
Yes, I can't change them. Anyway, @PerlDuck suggestion worked like a charm. Thank you for your comments.
â felima
Mar 1 at 21:40
Yes, I can't change them. Anyway, @PerlDuck suggestion worked like a charm. Thank you for your comments.
â felima
Mar 1 at 21:40
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1010556%2fis-it-possible-to-run-a-command-within-a-login-shell-using-sudo-or-su%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Might be helpful: askubuntu.com/questions/879364/â¦
â d a i s y
Feb 28 at 8:53
Thank you for the reference @daisy, very good explanation. My understanding about the behavior of a login shell (bash) is that it to goes through /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile and a non-interactive shell is when a script os executable is run directly by the shell (no prompt) as "su -c" does. So I was wondering that "su - (with -c) option would execute a non-interactive login shell, but it seems it doesn't. Is my understanding incorrect?
â felima
Feb 28 at 9:37
Does
su - username -c 'bash -l -c command'work? With a login-shell (bash -l) in-between?â PerlDuck
Feb 28 at 10:14
Thank you @PerlDuck it worked indeed. Got focused on understanding about su/sudo behavior and this great workaround never came to mind.
â felima
Mar 1 at 21:37