LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-22-2016, 11:52 AM   #1
tezarin
Member
 
Registered: Nov 2007
Posts: 133

Rep: Reputation: 0
The command after && doesn't run


Hi all,

I have created a Jenkins job today, what it does is the Jenkins user should log into another server and run two commands separated by &&:

ssh -i /creds/jenkins jenkins@servername.com "sh -c 'sudo su && lxc exec containername bash'"

The logging part works fine, then it runs the sudo su command and becomes root but it never runs the second command.

I even did this manually and from the Jenkins machine logged into the other server (servername). Then ran sh -c "sudo su && lxc exec containername bash" with no luck.

Can you please help?
 
Old 12-22-2016, 12:24 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
The && is an operator that runs the second command only when the first one has finished running and reports success. sudo su probably never finishes and thus never reports success or failure.

Code:
ssh -i /creds/jenkins jenkins@servername.com "sh -c 'sudo lxc exec containername bash'"
So if you want lxc to run as root, just use sudo alone.

Last edited by Turbocapitalist; 12-22-2016 at 12:25 PM.
 
Old 12-22-2016, 01:53 PM   #3
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,503

Rep: Reputation: Disabled
When you sudo su you are getting a new terminal, & the command doesn't get passed to it.
(As said above, if you just use sudo it should work.)
 
Old 12-22-2016, 09:15 PM   #4
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
As previously said: In order for the after && thing to run, the thing before && needs to complete with an exit code 0. If that's not a requirement, perhaps using ";" would better suit.

$ echo -e "echo 1\nexit 0" > test_good.sh
$ echo -e "echo 1\nexit 1" > test_bad.sh

$ sh test_good.sh && echo 2
1
2

$ sh test_bad.sh && echo 2
1

$ sh test_bad.sh; echo 2
1
2

Something like that.
 
Old 12-23-2016, 09:47 AM   #5
tezarin
Member
 
Registered: Nov 2007
Posts: 133

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
The && is an operator that runs the second command only when the first one has finished running and reports success. sudo su probably never finishes and thus never reports success or failure.

Code:
ssh -i /creds/jenkins jenkins@servername.com "sh -c 'sudo lxc exec containername bash'"
So if you want lxc to run as root, just use sudo alone.
Thanks everyone,
I've just tried running the command exactly as you stated and it doesn't work. The command runs and I don't get the prompt back. But If I manually become root, then type the lxc command, it works fine.
 
Old 12-23-2016, 10:15 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Quote:
Originally Posted by tezarin View Post
Thanks everyone,
I've just tried running the command exactly as you stated and it doesn't work. The command runs and I don't get the prompt back. But If I manually become root, then type the lxc command, it works fine.
Maybe you are missing some needed environment variable or other settings. See if sudo does it with the -i option.

Code:
ssh -i /creds/jenkins jenkins@servername.com "sh -c 'sudo -l lxc exec containername bash'"
That will run as a login shell.
 
Old 12-23-2016, 10:30 AM   #7
tezarin
Member
 
Registered: Nov 2007
Posts: 133

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
Maybe you are missing some needed environment variable or other settings. See if sudo does it with the -i option.

Code:
ssh -i /creds/jenkins jenkins@servername.com "sh -c 'sudo -l lxc exec containername bash'"
That will run as a login shell.
Thanks, I just tried it with the -l and this is what I got back on the Jenkins machine: /usr/bin/lxc exec docker bash

So it didn't even log into the servername.com, let alone becoming sudo and running the lxc container.
 
Old 12-23-2016, 10:32 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Sorry, that was a typo. I wrote -i in the text but put the wrong option in the example. Please try with -i and not -l.
 
Old 12-23-2016, 12:55 PM   #9
tezarin
Member
 
Registered: Nov 2007
Posts: 133

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
Sorry, that was a typo. I wrote -i in the text but put the wrong option in the example. Please try with -i and not -l.
No problem, I just tried it with -i and got this error: mesg: ttyname failed: Inappropriate ioctl for device
 
Old 12-23-2016, 01:18 PM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Ok. That's probably an improvement. I've not used lxc or lxd. However, looking around it might be possible that you need to use lxc-attach instead of straight up lxc, if the container is already running.
 
Old 12-23-2016, 01:30 PM   #11
tezarin
Member
 
Registered: Nov 2007
Posts: 133

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
Ok. That's probably an improvement. I've not used lxc or lxd. However, looking around it might be possible that you need to use lxc-attach instead of straight up lxc, if the container is already running.
If I run sudo su and become root, then run lxc exec containername bash, everything works fine. I'm puzzled...
 
Old 12-23-2016, 01:41 PM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Just guessing here, but what about adding a login option to su?

Code:
ssh -i /creds/jenkins jenkins@servername.com "sh -l -c 'sudo -i lxc exec containername bash'"
(This time it really is a -l for the one. sudo should try -i.)
 
Old 12-23-2016, 02:21 PM   #13
tezarin
Member
 
Registered: Nov 2007
Posts: 133

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
Just guessing here, but what about adding a login option to su?

Code:
ssh -i /creds/jenkins jenkins@servername.com "sh -l -c 'sudo -i lxc exec containername bash'"
(This time it really is a -l for the one. sudo should try -i.)
Thanks again, I tried it and got the same result back: mesg: ttyname failed: Inappropriate ioctl for device
 
Old 12-23-2016, 02:29 PM   #14
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Ok. One last guess and then I'll have to leave it to others. Add to the above a -t option for ssh to force TTY allocation:

Code:
ssh -t -i /creds/jenkins jenkins@servername.com "sh -l -c 'sudo -i lxc exec containername bash'"
Edit: you might try -tt if -t does not do it.

Last edited by Turbocapitalist; 12-23-2016 at 02:32 PM.
 
Old 12-23-2016, 02:55 PM   #15
tezarin
Member
 
Registered: Nov 2007
Posts: 133

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
Ok. One last guess and then I'll have to leave it to others. Add to the above a -t option for ssh to force TTY allocation:

Code:
ssh -t -i /creds/jenkins jenkins@servername.com "sh -l -c 'sudo -i lxc exec containername bash'"
Edit: you might try -tt if -t does not do it.
WOW! -t worked like a charm, thank you so much for your help, I really appreciate it!

Happy Holidays!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to run a program in background and also using && to execute another command P.G.Krish Linux - Newbie 6 06-09-2016 06:22 AM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Japanese canna won't work : Warning: かな漢字変&am OrganicOrange84 Debian 3 06-30-2005 02:28 PM
Phục hồi dữ liệu bị mất???, cứ pollsite General 1 06-27-2005 12:39 PM
An equivelant DOS command for & or && rootyard General 3 09-30-2003 04:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration