I want to start screen and put a couple of programs running inside on startup. I've seen many similar posts but none answered my question.
so I put in rc.local
following 2 lines:
screen -dmS scr bash -c 'ytop; exec bash'
screen -S scr -X screen 1 bash -c 'nvtop; exec bash'
which works fine, but when I enter this screen, once logged in to the server, and want to create new screen, it doesn't have environment variables in it. How can I start it in the way that would have all root's envars?
new window looks like this:
sh-5.1#
$PATH
in window is
/usr/bin:/bin
Yet in the rc.local
created windows $PATH
(/usr/bin:/bin
) is the same, but the prompt is normal, so it looks that it reads /etc/profile
Window initialized by rc.local has normal prompt:
[root@mysrv ~]#
PATH is also a bit more than fresh window, but not same as if I start screen manually from the terminal as root
echo $PATH
usr/lib64/qt-3.3/bin:/usr/bin:/bin
When I start screen by myself it's normal prompt.
[root@mysrv ~]#
and
echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Interestingly if I exit ytop
it shows me proper prompt. Only when creating new screen somehow looses this envars.
I tried also running:
su - root -c `screen -dmS scr bash -c 'ytop; exec bash'`
but I get the same result.
Regards
I think your problem here is the session-handling of bash. Bash - like any other shell interpreter - detects if it is started in the backgroud, or if it is attached to a terminal ( Interactive / Non-interactive mode ). Background is found here. My guess is, that your problems are solved by providing the
-l
switch to the first bash command:The
--login
switch makes bash read all startup files, like/etc/profile
,~/.bash_profile
,~/.bash_login
, and~/.profile
- which should contain the environmental variables you are missing.I finally found a solution. It's very simple. So to start screen with normal shell and variables you have to do it with
init
, so :So this will have normal environment like if you manually start screen and detach it.