how to intercept the command that launches a bash script
If I run a bash script from the command line (with root permissions), is it possible to intercept the boot command within the script (while the script is still running) in order to log it?
Thanks in advance
linux bash shell-script
add a comment |
If I run a bash script from the command line (with root permissions), is it possible to intercept the boot command within the script (while the script is still running) in order to log it?
Thanks in advance
linux bash shell-script
1
What do you mean by 'the boot command' and 'while the script is still running'? Please tell us more details, and please give feedback to the answers and comments. Otherwise we can only guess, and the answer(s) might or might not be useful for your particular case.
– sudodus
Jan 8 at 16:55
add a comment |
If I run a bash script from the command line (with root permissions), is it possible to intercept the boot command within the script (while the script is still running) in order to log it?
Thanks in advance
linux bash shell-script
If I run a bash script from the command line (with root permissions), is it possible to intercept the boot command within the script (while the script is still running) in order to log it?
Thanks in advance
linux bash shell-script
linux bash shell-script
edited Jan 8 at 16:20
Daniel
31
31
asked Jan 8 at 15:51
DanielDaniel
61
61
1
What do you mean by 'the boot command' and 'while the script is still running'? Please tell us more details, and please give feedback to the answers and comments. Otherwise we can only guess, and the answer(s) might or might not be useful for your particular case.
– sudodus
Jan 8 at 16:55
add a comment |
1
What do you mean by 'the boot command' and 'while the script is still running'? Please tell us more details, and please give feedback to the answers and comments. Otherwise we can only guess, and the answer(s) might or might not be useful for your particular case.
– sudodus
Jan 8 at 16:55
1
1
What do you mean by 'the boot command' and 'while the script is still running'? Please tell us more details, and please give feedback to the answers and comments. Otherwise we can only guess, and the answer(s) might or might not be useful for your particular case.
– sudodus
Jan 8 at 16:55
What do you mean by 'the boot command' and 'while the script is still running'? Please tell us more details, and please give feedback to the answers and comments. Otherwise we can only guess, and the answer(s) might or might not be useful for your particular case.
– sudodus
Jan 8 at 16:55
add a comment |
1 Answer
1
active
oldest
votes
If editing the script is an option, you can add "logger" commands to log whatever you'd like to the system's logs.
on my Fedora system,
/usr/bin/logger $USER just ran $0
run from a bash prompt, added a log entry with the current timestamp, a source of my login ID[PID of logger command] just ran bash
you could add whatever information you wanted into that logger command's message. You can even pipe standard out of another command to the logger command.
$0 is a special variable that holds the name of the command currently running (bash when sitting at the bash prompt, but in a script, it would be the script's filename)
If you have access to the .bashrc or other startup files for the user who will be running processes you want to log, you could also create an alias.
I just tried:
alias logls='/usr/bin/logger listing a dir ; ls'
then logls
and got the listing of the current directory, and a timestamped log entry in the system logs.
if the alias was set to the same name as the original script it would be pretty transparent to the user(s). You would likely have to specify the full path to the original script in the definition of the alias of course.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493262%2fhow-to-intercept-the-command-that-launches-a-bash-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If editing the script is an option, you can add "logger" commands to log whatever you'd like to the system's logs.
on my Fedora system,
/usr/bin/logger $USER just ran $0
run from a bash prompt, added a log entry with the current timestamp, a source of my login ID[PID of logger command] just ran bash
you could add whatever information you wanted into that logger command's message. You can even pipe standard out of another command to the logger command.
$0 is a special variable that holds the name of the command currently running (bash when sitting at the bash prompt, but in a script, it would be the script's filename)
If you have access to the .bashrc or other startup files for the user who will be running processes you want to log, you could also create an alias.
I just tried:
alias logls='/usr/bin/logger listing a dir ; ls'
then logls
and got the listing of the current directory, and a timestamped log entry in the system logs.
if the alias was set to the same name as the original script it would be pretty transparent to the user(s). You would likely have to specify the full path to the original script in the definition of the alias of course.
add a comment |
If editing the script is an option, you can add "logger" commands to log whatever you'd like to the system's logs.
on my Fedora system,
/usr/bin/logger $USER just ran $0
run from a bash prompt, added a log entry with the current timestamp, a source of my login ID[PID of logger command] just ran bash
you could add whatever information you wanted into that logger command's message. You can even pipe standard out of another command to the logger command.
$0 is a special variable that holds the name of the command currently running (bash when sitting at the bash prompt, but in a script, it would be the script's filename)
If you have access to the .bashrc or other startup files for the user who will be running processes you want to log, you could also create an alias.
I just tried:
alias logls='/usr/bin/logger listing a dir ; ls'
then logls
and got the listing of the current directory, and a timestamped log entry in the system logs.
if the alias was set to the same name as the original script it would be pretty transparent to the user(s). You would likely have to specify the full path to the original script in the definition of the alias of course.
add a comment |
If editing the script is an option, you can add "logger" commands to log whatever you'd like to the system's logs.
on my Fedora system,
/usr/bin/logger $USER just ran $0
run from a bash prompt, added a log entry with the current timestamp, a source of my login ID[PID of logger command] just ran bash
you could add whatever information you wanted into that logger command's message. You can even pipe standard out of another command to the logger command.
$0 is a special variable that holds the name of the command currently running (bash when sitting at the bash prompt, but in a script, it would be the script's filename)
If you have access to the .bashrc or other startup files for the user who will be running processes you want to log, you could also create an alias.
I just tried:
alias logls='/usr/bin/logger listing a dir ; ls'
then logls
and got the listing of the current directory, and a timestamped log entry in the system logs.
if the alias was set to the same name as the original script it would be pretty transparent to the user(s). You would likely have to specify the full path to the original script in the definition of the alias of course.
If editing the script is an option, you can add "logger" commands to log whatever you'd like to the system's logs.
on my Fedora system,
/usr/bin/logger $USER just ran $0
run from a bash prompt, added a log entry with the current timestamp, a source of my login ID[PID of logger command] just ran bash
you could add whatever information you wanted into that logger command's message. You can even pipe standard out of another command to the logger command.
$0 is a special variable that holds the name of the command currently running (bash when sitting at the bash prompt, but in a script, it would be the script's filename)
If you have access to the .bashrc or other startup files for the user who will be running processes you want to log, you could also create an alias.
I just tried:
alias logls='/usr/bin/logger listing a dir ; ls'
then logls
and got the listing of the current directory, and a timestamped log entry in the system logs.
if the alias was set to the same name as the original script it would be pretty transparent to the user(s). You would likely have to specify the full path to the original script in the definition of the alias of course.
answered Jan 8 at 16:19
Adam JohnsonAdam Johnson
266
266
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493262%2fhow-to-intercept-the-command-that-launches-a-bash-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What do you mean by 'the boot command' and 'while the script is still running'? Please tell us more details, and please give feedback to the answers and comments. Otherwise we can only guess, and the answer(s) might or might not be useful for your particular case.
– sudodus
Jan 8 at 16:55