What are the pro's and con's in using the â-lâ in a script shebang
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I recently came up to an easy fix for a crontab logging issue and I am wondering what are the pro's and con's of using this specific fix (running a script with a "login shell flag"), as:
#!/bin/bash -l
bash shell-script cron login shebang
add a comment |Â
up vote
0
down vote
favorite
I recently came up to an easy fix for a crontab logging issue and I am wondering what are the pro's and con's of using this specific fix (running a script with a "login shell flag"), as:
#!/bin/bash -l
bash shell-script cron login shebang
1
I do wonder how being a login shell was even related to your issue there..
â ilkkachu
Feb 7 at 10:16
Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
â Pier
Feb 18 at 17:26
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I recently came up to an easy fix for a crontab logging issue and I am wondering what are the pro's and con's of using this specific fix (running a script with a "login shell flag"), as:
#!/bin/bash -l
bash shell-script cron login shebang
I recently came up to an easy fix for a crontab logging issue and I am wondering what are the pro's and con's of using this specific fix (running a script with a "login shell flag"), as:
#!/bin/bash -l
bash shell-script cron login shebang
edited Feb 7 at 23:49
Jeff Schaller
31.3k846105
31.3k846105
asked Feb 7 at 10:02
Pier
65112
65112
1
I do wonder how being a login shell was even related to your issue there..
â ilkkachu
Feb 7 at 10:16
Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
â Pier
Feb 18 at 17:26
add a comment |Â
1
I do wonder how being a login shell was even related to your issue there..
â ilkkachu
Feb 7 at 10:16
Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
â Pier
Feb 18 at 17:26
1
1
I do wonder how being a login shell was even related to your issue there..
â ilkkachu
Feb 7 at 10:16
I do wonder how being a login shell was even related to your issue there..
â ilkkachu
Feb 7 at 10:16
Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
â Pier
Feb 18 at 17:26
Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
â Pier
Feb 18 at 17:26
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
6
down vote
[The following assumes that your unspecified "logging issue" was related to missing environment setup, normally inherited from your profile.]
The -l
option tells bash to read all the various "profile" scripts, from /etc
and from your home directory. Bash normally only does this for interactive sessions (in which bash is run without any command line parameters).
Normal scripts have no business reading the profile; they're supposed to run in the environment they were given. That said, you might want to do this for personal scripts, maybe, if they're tightly bound to your environment and you plan to run them outside of a normal session.
A crontab is one example of running a script outside your session, so yes, go do it!
If the script is purely for the use of the crontab then adding -l
to the shebang is fine. If you might use the script other ways then consider fixing the environment problem in the crontab itself:
0 * * * * bash -l hourly.sh
Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
â Kusalananda
Feb 7 at 10:49
Thanks ams for your insight, that was some solution I did not think about.
â Pier
Feb 7 at 11:41
Also, if the user is doing something like this, there will be trouble.
â Kusalananda
Feb 8 at 20:25
add a comment |Â
up vote
-1
down vote
I honestly don't see any benefit of running a non-interactive script in a login shell.
The login shell will parse the relevant login shell initialization files (bash
uses ~/.bash_profile
) to set up the shell session's environment etc.
It is not unreasonable to believe that a user may do all sorts of interesting things in this file, such as starting tmux
(like in this question) or even running exec
on another shell altogether (as in this question, and this).
Instead, the environment that the script needs to run in should be set up in the file pointed to by $BASH_ENV
. This file will be sourced by any non-interactive bash
shell.
A script being run from a cron job would not be run in an interactive shell nor in a login shell, and it would then need to be started as
@daily BASH_ENV="$HOME/script.env" "$HOME/script.sh"
(for a daily job triggered at midnight) here $HOME/script.env
may be $HOME/.bashrc
if that's where the environment is set up.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
[The following assumes that your unspecified "logging issue" was related to missing environment setup, normally inherited from your profile.]
The -l
option tells bash to read all the various "profile" scripts, from /etc
and from your home directory. Bash normally only does this for interactive sessions (in which bash is run without any command line parameters).
Normal scripts have no business reading the profile; they're supposed to run in the environment they were given. That said, you might want to do this for personal scripts, maybe, if they're tightly bound to your environment and you plan to run them outside of a normal session.
A crontab is one example of running a script outside your session, so yes, go do it!
If the script is purely for the use of the crontab then adding -l
to the shebang is fine. If you might use the script other ways then consider fixing the environment problem in the crontab itself:
0 * * * * bash -l hourly.sh
Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
â Kusalananda
Feb 7 at 10:49
Thanks ams for your insight, that was some solution I did not think about.
â Pier
Feb 7 at 11:41
Also, if the user is doing something like this, there will be trouble.
â Kusalananda
Feb 8 at 20:25
add a comment |Â
up vote
6
down vote
[The following assumes that your unspecified "logging issue" was related to missing environment setup, normally inherited from your profile.]
The -l
option tells bash to read all the various "profile" scripts, from /etc
and from your home directory. Bash normally only does this for interactive sessions (in which bash is run without any command line parameters).
Normal scripts have no business reading the profile; they're supposed to run in the environment they were given. That said, you might want to do this for personal scripts, maybe, if they're tightly bound to your environment and you plan to run them outside of a normal session.
A crontab is one example of running a script outside your session, so yes, go do it!
If the script is purely for the use of the crontab then adding -l
to the shebang is fine. If you might use the script other ways then consider fixing the environment problem in the crontab itself:
0 * * * * bash -l hourly.sh
Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
â Kusalananda
Feb 7 at 10:49
Thanks ams for your insight, that was some solution I did not think about.
â Pier
Feb 7 at 11:41
Also, if the user is doing something like this, there will be trouble.
â Kusalananda
Feb 8 at 20:25
add a comment |Â
up vote
6
down vote
up vote
6
down vote
[The following assumes that your unspecified "logging issue" was related to missing environment setup, normally inherited from your profile.]
The -l
option tells bash to read all the various "profile" scripts, from /etc
and from your home directory. Bash normally only does this for interactive sessions (in which bash is run without any command line parameters).
Normal scripts have no business reading the profile; they're supposed to run in the environment they were given. That said, you might want to do this for personal scripts, maybe, if they're tightly bound to your environment and you plan to run them outside of a normal session.
A crontab is one example of running a script outside your session, so yes, go do it!
If the script is purely for the use of the crontab then adding -l
to the shebang is fine. If you might use the script other ways then consider fixing the environment problem in the crontab itself:
0 * * * * bash -l hourly.sh
[The following assumes that your unspecified "logging issue" was related to missing environment setup, normally inherited from your profile.]
The -l
option tells bash to read all the various "profile" scripts, from /etc
and from your home directory. Bash normally only does this for interactive sessions (in which bash is run without any command line parameters).
Normal scripts have no business reading the profile; they're supposed to run in the environment they were given. That said, you might want to do this for personal scripts, maybe, if they're tightly bound to your environment and you plan to run them outside of a normal session.
A crontab is one example of running a script outside your session, so yes, go do it!
If the script is purely for the use of the crontab then adding -l
to the shebang is fine. If you might use the script other ways then consider fixing the environment problem in the crontab itself:
0 * * * * bash -l hourly.sh
answered Feb 7 at 10:27
ams
4,1821123
4,1821123
Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
â Kusalananda
Feb 7 at 10:49
Thanks ams for your insight, that was some solution I did not think about.
â Pier
Feb 7 at 11:41
Also, if the user is doing something like this, there will be trouble.
â Kusalananda
Feb 8 at 20:25
add a comment |Â
Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
â Kusalananda
Feb 7 at 10:49
Thanks ams for your insight, that was some solution I did not think about.
â Pier
Feb 7 at 11:41
Also, if the user is doing something like this, there will be trouble.
â Kusalananda
Feb 8 at 20:25
Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
â Kusalananda
Feb 7 at 10:49
Rather than relying on the way the script is invoked, one could also explicitly source the relevant shell files from within the script. This would additionally serve to (to a degree) document the fact that the script needs profile-specific setup.
â Kusalananda
Feb 7 at 10:49
Thanks ams for your insight, that was some solution I did not think about.
â Pier
Feb 7 at 11:41
Thanks ams for your insight, that was some solution I did not think about.
â Pier
Feb 7 at 11:41
Also, if the user is doing something like this, there will be trouble.
â Kusalananda
Feb 8 at 20:25
Also, if the user is doing something like this, there will be trouble.
â Kusalananda
Feb 8 at 20:25
add a comment |Â
up vote
-1
down vote
I honestly don't see any benefit of running a non-interactive script in a login shell.
The login shell will parse the relevant login shell initialization files (bash
uses ~/.bash_profile
) to set up the shell session's environment etc.
It is not unreasonable to believe that a user may do all sorts of interesting things in this file, such as starting tmux
(like in this question) or even running exec
on another shell altogether (as in this question, and this).
Instead, the environment that the script needs to run in should be set up in the file pointed to by $BASH_ENV
. This file will be sourced by any non-interactive bash
shell.
A script being run from a cron job would not be run in an interactive shell nor in a login shell, and it would then need to be started as
@daily BASH_ENV="$HOME/script.env" "$HOME/script.sh"
(for a daily job triggered at midnight) here $HOME/script.env
may be $HOME/.bashrc
if that's where the environment is set up.
add a comment |Â
up vote
-1
down vote
I honestly don't see any benefit of running a non-interactive script in a login shell.
The login shell will parse the relevant login shell initialization files (bash
uses ~/.bash_profile
) to set up the shell session's environment etc.
It is not unreasonable to believe that a user may do all sorts of interesting things in this file, such as starting tmux
(like in this question) or even running exec
on another shell altogether (as in this question, and this).
Instead, the environment that the script needs to run in should be set up in the file pointed to by $BASH_ENV
. This file will be sourced by any non-interactive bash
shell.
A script being run from a cron job would not be run in an interactive shell nor in a login shell, and it would then need to be started as
@daily BASH_ENV="$HOME/script.env" "$HOME/script.sh"
(for a daily job triggered at midnight) here $HOME/script.env
may be $HOME/.bashrc
if that's where the environment is set up.
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
I honestly don't see any benefit of running a non-interactive script in a login shell.
The login shell will parse the relevant login shell initialization files (bash
uses ~/.bash_profile
) to set up the shell session's environment etc.
It is not unreasonable to believe that a user may do all sorts of interesting things in this file, such as starting tmux
(like in this question) or even running exec
on another shell altogether (as in this question, and this).
Instead, the environment that the script needs to run in should be set up in the file pointed to by $BASH_ENV
. This file will be sourced by any non-interactive bash
shell.
A script being run from a cron job would not be run in an interactive shell nor in a login shell, and it would then need to be started as
@daily BASH_ENV="$HOME/script.env" "$HOME/script.sh"
(for a daily job triggered at midnight) here $HOME/script.env
may be $HOME/.bashrc
if that's where the environment is set up.
I honestly don't see any benefit of running a non-interactive script in a login shell.
The login shell will parse the relevant login shell initialization files (bash
uses ~/.bash_profile
) to set up the shell session's environment etc.
It is not unreasonable to believe that a user may do all sorts of interesting things in this file, such as starting tmux
(like in this question) or even running exec
on another shell altogether (as in this question, and this).
Instead, the environment that the script needs to run in should be set up in the file pointed to by $BASH_ENV
. This file will be sourced by any non-interactive bash
shell.
A script being run from a cron job would not be run in an interactive shell nor in a login shell, and it would then need to be started as
@daily BASH_ENV="$HOME/script.env" "$HOME/script.sh"
(for a daily job triggered at midnight) here $HOME/script.env
may be $HOME/.bashrc
if that's where the environment is set up.
edited Feb 8 at 21:06
answered Feb 8 at 20:43
Kusalananda
103k13202318
103k13202318
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422499%2fwhat-are-the-pros-and-cons-in-using-the-l-in-a-script-shebang%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
I do wonder how being a login shell was even related to your issue there..
â ilkkachu
Feb 7 at 10:16
Sorry ikkachu, I cannot remember what this was about precisely ! I found a better way for fixing my issue anyway.
â Pier
Feb 18 at 17:26