LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux From Scratch
User Name
Password
Linux From Scratch This Forum is for the discussion of LFS.
LFS is a project that provides you with the steps necessary to build your own custom Linux system.

Notices

Reply
 
Search this Thread
Old 05-23-2012, 02:01 PM   #1
lich000king
Member
 
Registered: Jun 2011
Posts: 56

Rep: Reputation: Disabled
su - lfs inside script


Hi

While trying to further automate Johnburrell's scripts:-) I run into a little problem:

When the command
Code:
su - lfs
(LFS section 4.3) is executed from inside a shell script, the script stops.
I tried writing the commands that should be executed by the user lfs into script.sh and then:
Code:
su - lfs -c "script.sh"
but that does not work either. Again the script is locked inside the lfs shell.

On the other hand if I do
Code:
su lfs -c "script.sh"
that works fine (but is wrong according to the book).

I searched a lot, including jhalfs mailing lists etc. and this is being discussed, but I could not find a solution.

Is there a way to make the "su -" work?

Cheers
Oliver
 
Old 05-23-2012, 03:42 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
This seems to work:

script1.sh:
Code:
#!/bin/bash

echo "
from script1.sh: "
whoami

su - lfs -c "/tmp/script2.sh" # use full path to script

echo "
from script1.sh: "
whoami
script2.sh:
Code:
#!/bin/bash

echo "from script2.sh: "
whoami

echo "doing stuff in script2.sh"

echo "done ...."
Example run:
Code:
$ ./script1.sh

from script1.sh: 
root
from script2.sh: 
lfs
doing stuff in script2.sh
done ....

from script1.sh: 
root
Permissions (dir and script2.sh) can be an issue, make sure both are accessible by the lfs user.
 
Old 05-24-2012, 03:24 PM   #3
lich000king
Member
 
Registered: Jun 2011
Posts: 56

Original Poster
Rep: Reputation: Disabled
Hello druuna

Thanks for your reply.
Unfortunately it does not work for me:

Code:
root@debian:~# ./script1.sh 

from script1.sh: 
root
lfs@debian:~$
The execution stops there.
When I type "exit", I get:

Code:
exit

from script1.sh: 
root
root@debian:~#
I did "chmod 777 script2.sh" and "chown lfs script2.sh".
The second script is in lfs's home directory (and I added the full path in it).
Furthermore, the following does work:

Code:
root@debian:~# su - lfs
lfs@debian:~$ ./script2.sh 
from script2.sh: 
lfs
doing stuff in script2.sh
done ....
lfs@debian:~$

Do you have an idea what else I could be missing?

Oliver
 
Old 05-25-2012, 02:11 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
I just tried a few things and found out that the presence of .bash_profile in the lfs home directory is the reason for this behaviour:
Code:
# content of lfs' .bash_profile
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
The above starts a new shell. After executing su - lfs an interactive login shell is started and .bash_profile is parsed, which in turn starts bash as a none login shell and .bashrc is parsed. This needs to be done this way to create a good, clean environment.

This might work: Change the content of lfs' .bash_profile to:
Code:
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /home/lfs/script2.sh
and script2.sh to:
Code:
#!/bin/bash

. /home/lfs/.bashrc

echo "from script2.sh: "
whoami

echo "doing stuff in script2.sh"

echo "done ...."
The above does come with a possible down-side: script2.sh is always executed when becoming user lfs (su - lfs). This might not be a problem when everything is scripted, but could be a problem if you manually need to do stuff as lfs user. You could manually use su lfs instead of su - lfs, but then you need to be aware that the environment isn't properly set.
 
Old 05-26-2012, 07:24 AM   #5
lich000king
Member
 
Registered: Jun 2011
Posts: 56

Original Poster
Rep: Reputation: Disabled
Thanks, now it's working!

There is still a small problem:
When using a script, the $HOME environment variable is not correctly read. lfs' $HOME will be "/root" rather than "/home/lfs". So I think it's better to also change the "HOME" and "TERM" entries in the exec env command (see below).

Quote:
The above does come with a possible down-side: script2.sh is always executed when becoming user lfs (su - lfs). This might not be a problem when everything is scripted, but could be a problem if you manually need to do stuff as lfs user. You could manually use su lfs instead of su - lfs, but then you need to be aware that the environment isn't properly set.
This can be avoided by modifying script1 as follows:

Code:
#!/bin/bash

echo "
from script1.sh: "
whoami

su lfs -c "cat > ~/.bash_profile << "EOF"
exec env -i HOME=/home/lfs TERM=xterm PS1='\u:\w\$ ' /home/lfs/script2.sh
EOF"

su - lfs -c "/home/lfs/script2.sh" # use full path to script

su lfs -c "cat > ~/.bash_profile << "EOF"
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF"

echo "
from script1.sh: "
whoami
This way, lfs' .bash_profile is changed to what we need to run script2.sh, then the script is run and finally .bash_profile is changed back to normal.
Of course I can't write lfs' .bash_profile logged in as "su - lfs". But I guess doing it as "su lfs" is fine. The important thing is that the installation of the packages happens from a clean environment.

Now I'm wondering: Couldn't I just simplify script1 like this:

Code:
#!/bin/bash

echo "
from script1.sh: "
whoami

su lfs -c "exec env -i HOME=/home/lfs TERM=xterm PS1='\u:\w\$ ' /home/lfs/script2.sh"

echo "
from script1.sh: "
whoami
Wouldn't this also give the desired clean environment, withouth the need to do any changes in .bash_profile?
 
Old 05-26-2012, 08:09 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
You shouldn't need to change the HOME and TERM variables, they are set correctly as-is. Are you sure you aren't mixing up su lfs and su - lfs?

Here are the scripts/files I tested with:

script1.sh
Code:
$ ls -l script1.sh 
-rwxr-xr-x 1 root root 142 May 26 14:53 script1.sh

$ cat script1.sh
#!/bin/bash

echo "
from script1.sh: "
whoami

su - lfs -c "/home/lfs/script2.sh"

echo "
from script1.sh: "
whoami
script2.sh
Code:
$ ls -l /home/lfs/script2.sh 
-rwxr-xr-x 1 lfs lfs 160 May 26 14:47 /home/lfs/script2.sh

$ cat /home/lfs/script2.sh
#!/bin/bash

. /home/lfs/.bashrc

echo "from script2.sh: "
whoami

echo "doing stuff in script2.sh"
echo "HOME -> $HOME"
echo "TERM -> $TERM"

echo "done ...."
.bash_profile
Code:
$ ls -l /home/lfs/.bash_profile 
-rw-r--r-- 1 lfs lfs 70 May 26 14:55 /home/lfs/.bash_profile

$ cat /home/lfs/.bash_profile
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /home/lfs/script2.sh
Example run, started as root:
Code:
$ ./script1.sh 

from script1.sh: 
root
from script2.sh: 
lfs
doing stuff in script2.sh
HOME -> /home/lfs
TERM -> xterm
done ....

from script1.sh: 
root
A quick test shows that your alternative should work also (I did use: su - lfs -c "exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /home/lfs/script2.sh")

It is up to you to decide what works best in your situation. The lfs user itself is temporary and all that really matters is the clean environment once you become the lfs user (and using a login shell to kick things off would be best in my opinion).
 
Old 05-30-2012, 01:30 PM   #7
lich000king
Member
 
Registered: Jun 2011
Posts: 56

Original Poster
Rep: Reputation: Disabled
Right, I tested again using exactly what you wrote and it's working.
It must have been a permission problem or some stupid typo that caused the environment variables to behave bad.
I will post my script to the "Automate LFS 7.1" thread when I'm done.

druuna, thanks a lot for your help!

Cheers
Oliver
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Runs Multiple shell script inside a main script using crontab srimal Linux - Newbie 4 10-22-2009 06:19 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
script executed inside another script dont make changes potraike Linux - Newbie 4 03-05-2008 05:27 AM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM


All times are GMT -5. The time now is 06:34 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration