run shell script as other user, but directory stay the same
I have to run a bash script as root. But in this script, there are commands that I have to run as my normal user (he's named "ubuntu"), but when doing it I don't want the current working directory to change.
Things that I tried, but doesn't perfectly work :
Let's say that I am in a folder : /var/www
and that I am in super user mode.
-u
sudo -u ubuntu pwd
gives me/var/www
, which is nice.- But
sudo -u ubuntu which composer
gives me nothing, which is bad.
-i -u
sudo -i -u ubuntu which composer
gives me/home/ubuntu/bin/composer
which is nice.- But
sudo -i -u ubuntu pwd
gives me/home/ubuntu
which is bad.
I also tried to add a -H, or -s but it doesn't help in any way.
What kind of options or tricks could I use to achieve this ?
command-line bash sudo users
add a comment |
I have to run a bash script as root. But in this script, there are commands that I have to run as my normal user (he's named "ubuntu"), but when doing it I don't want the current working directory to change.
Things that I tried, but doesn't perfectly work :
Let's say that I am in a folder : /var/www
and that I am in super user mode.
-u
sudo -u ubuntu pwd
gives me/var/www
, which is nice.- But
sudo -u ubuntu which composer
gives me nothing, which is bad.
-i -u
sudo -i -u ubuntu which composer
gives me/home/ubuntu/bin/composer
which is nice.- But
sudo -i -u ubuntu pwd
gives me/home/ubuntu
which is bad.
I also tried to add a -H, or -s but it doesn't help in any way.
What kind of options or tricks could I use to achieve this ?
command-line bash sudo users
1
What are you trying to achieve exactly? Like if you're trying to call/home/ubuntu/bin/composer
, you could call it by the full path,/home/ubuntu/bin/composer
.
– wjandrea
Jan 21 at 17:18
Not exactly, cause I have to execute this script on several machines. Andcomposer
won't be stored at the same place every time. But on all machine, I have a user calledubuntu
that have a command calledcomposer
.
– Motiss
2 days ago
add a comment |
I have to run a bash script as root. But in this script, there are commands that I have to run as my normal user (he's named "ubuntu"), but when doing it I don't want the current working directory to change.
Things that I tried, but doesn't perfectly work :
Let's say that I am in a folder : /var/www
and that I am in super user mode.
-u
sudo -u ubuntu pwd
gives me/var/www
, which is nice.- But
sudo -u ubuntu which composer
gives me nothing, which is bad.
-i -u
sudo -i -u ubuntu which composer
gives me/home/ubuntu/bin/composer
which is nice.- But
sudo -i -u ubuntu pwd
gives me/home/ubuntu
which is bad.
I also tried to add a -H, or -s but it doesn't help in any way.
What kind of options or tricks could I use to achieve this ?
command-line bash sudo users
I have to run a bash script as root. But in this script, there are commands that I have to run as my normal user (he's named "ubuntu"), but when doing it I don't want the current working directory to change.
Things that I tried, but doesn't perfectly work :
Let's say that I am in a folder : /var/www
and that I am in super user mode.
-u
sudo -u ubuntu pwd
gives me/var/www
, which is nice.- But
sudo -u ubuntu which composer
gives me nothing, which is bad.
-i -u
sudo -i -u ubuntu which composer
gives me/home/ubuntu/bin/composer
which is nice.- But
sudo -i -u ubuntu pwd
gives me/home/ubuntu
which is bad.
I also tried to add a -H, or -s but it doesn't help in any way.
What kind of options or tricks could I use to achieve this ?
command-line bash sudo users
command-line bash sudo users
edited 2 days ago
Motiss
asked Jan 21 at 16:57
MotissMotiss
367
367
1
What are you trying to achieve exactly? Like if you're trying to call/home/ubuntu/bin/composer
, you could call it by the full path,/home/ubuntu/bin/composer
.
– wjandrea
Jan 21 at 17:18
Not exactly, cause I have to execute this script on several machines. Andcomposer
won't be stored at the same place every time. But on all machine, I have a user calledubuntu
that have a command calledcomposer
.
– Motiss
2 days ago
add a comment |
1
What are you trying to achieve exactly? Like if you're trying to call/home/ubuntu/bin/composer
, you could call it by the full path,/home/ubuntu/bin/composer
.
– wjandrea
Jan 21 at 17:18
Not exactly, cause I have to execute this script on several machines. Andcomposer
won't be stored at the same place every time. But on all machine, I have a user calledubuntu
that have a command calledcomposer
.
– Motiss
2 days ago
1
1
What are you trying to achieve exactly? Like if you're trying to call
/home/ubuntu/bin/composer
, you could call it by the full path, /home/ubuntu/bin/composer
.– wjandrea
Jan 21 at 17:18
What are you trying to achieve exactly? Like if you're trying to call
/home/ubuntu/bin/composer
, you could call it by the full path, /home/ubuntu/bin/composer
.– wjandrea
Jan 21 at 17:18
Not exactly, cause I have to execute this script on several machines. And
composer
won't be stored at the same place every time. But on all machine, I have a user called ubuntu
that have a command called composer
.– Motiss
2 days ago
Not exactly, cause I have to execute this script on several machines. And
composer
won't be stored at the same place every time. But on all machine, I have a user called ubuntu
that have a command called composer
.– Motiss
2 days ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1111711%2frun-shell-script-as-other-user-but-directory-stay-the-same%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1111711%2frun-shell-script-as-other-user-but-directory-stay-the-same%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What are you trying to achieve exactly? Like if you're trying to call
/home/ubuntu/bin/composer
, you could call it by the full path,/home/ubuntu/bin/composer
.– wjandrea
Jan 21 at 17:18
Not exactly, cause I have to execute this script on several machines. And
composer
won't be stored at the same place every time. But on all machine, I have a user calledubuntu
that have a command calledcomposer
.– Motiss
2 days ago