1 |
On 01/02/2015 02:18, Adam Carter wrote: |
2 |
> |
3 |
> If you've su'd to root, try 'su -' instead. |
4 |
> |
5 |
> |
6 |
> Thank you, that was it? |
7 |
> What difference does it make and why on some boxes it has to be "su |
8 |
> -" and on others simple "su" works. |
9 |
> |
10 |
> |
11 |
> Read 'man su'. I dont really understand this stuff well enough, but a |
12 |
> 'login shell', that is, one started by /bin/login, is setup with a |
13 |
> different environment to a shell that's started by su (or by, say, |
14 |
> cron). This is why a shell command or script may work for you when |
15 |
> you're logged in, but not if you run it from cron. I'm sure other's can |
16 |
> explain it more correctly and fully. |
17 |
|
18 |
|
19 |
This stuff is complex the first time you run into it. |
20 |
|
21 |
There are three kinds of shell depending on how you start them: login |
22 |
shell, interactive shell, and non-interactive shell. |
23 |
|
24 |
When you get a login prompt on the console and type your |
25 |
username/password, you get a login shell. bash will have done it's full |
26 |
complete start-up routine, will have read all the .bashrc and.profile |
27 |
files, and will set up your default environment just the way you like it. |
28 |
|
29 |
If you then type say "vi" at the command line, bash will launch it. vi |
30 |
does not try and set up a complete new environment for you - what would |
31 |
be the point? the shell already did it for you. So if you are at a bash |
32 |
prompt, and type "bash", the running bash will start a new bash but |
33 |
won't do the whole startup routine for you (exactly the same way vi also |
34 |
doesn't try do it). The kind of bash shell is called an "interactive |
35 |
shell", and it's different from a login shell. It's called interactive |
36 |
because you get a prompt, you can type stuff at the keyboard and get a |
37 |
response on the screen. |
38 |
|
39 |
A non-interactive shell is what a daemon/service like cron gets when it |
40 |
starts bash to run some program in the background. Almost all |
41 |
programming languages have an exec() function where you can run shell |
42 |
commands - these shells are also non-interactive. Why non-interactive? |
43 |
Because a daemon can't type stuff the keyboard, doesn't have a screen, |
44 |
and doesn't need $PATH set. It's a program, it doesn't need cute shortcuts. |
45 |
|
46 |
When you run "su" you might want an interactive shell, or you might want |
47 |
a login shell. Maybe you want to quickly run a command as the effective |
48 |
ID of the apache user, but don't need to change your entire environment. |
49 |
You run that as "su" |
50 |
|
51 |
Maybe you want to run su and log in as root to do a whole lot of |
52 |
maintenance. You decide you need root's full setup with PATH and |
53 |
everything else for this, so you run "su -" (the "-" means "start a |
54 |
login shell") |
55 |
|
56 |
|
57 |
|
58 |
Well, OK, that's how it SUPPOSED to work. Usually it doesn't because |
59 |
distros decide to be clever. |
60 |
|
61 |
The files bash reads when it starts follow a convoluted path (see |
62 |
INVOCATION in man bash) and your average user gets confused. So distros |
63 |
"helpfully" make it easy for the user and rig it so that when you start |
64 |
an interactive shell, it goes and reads all the startup files anyway. So |
65 |
you don't get an interactive shell, you get a full-blown login shell! |
66 |
So how do you get an interactive shell in a distro like that? Well, |
67 |
errr, you don't. |
68 |
|
69 |
This explains the different behaviours of su vs su - between distros. |
70 |
Some, like SuSE have rigged it so there's no difference. Gentoo doesn't |
71 |
do that because a) Gentoo sticks closely to upstream and b) Gentoo users |
72 |
are not morons and don't need helpful distros dicking around with their |
73 |
shells. |
74 |
|
75 |
|
76 |
|
77 |
The topic of cron came up. A common issue is you run myscript.sh at the |
78 |
command line and it works. Run it in a cron as you and it doesn't work. |
79 |
|
80 |
Cron gets a non-interactive shell and $PATH is not set. So in cron, you |
81 |
must call it like this: |
82 |
|
83 |
/path/to/bin/myscript.sh |
84 |
|
85 |
In cron, it's always best to use fully-qualified paths to all files |
86 |
everywhere and always. Don't rely on startup profiles - those have |
87 |
always been to make life easier for human users, and were never intended |
88 |
to be used by system daemons. |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
-- |
96 |
Alan McKinnon |
97 |
alan.mckinnon@×××××.com |