1

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?

I try to be clearer. From the command line I launch a script with this statement: ./reconfig.sh -host localhost ... and other parameters. Inside the script, while it is running, a log file is written and I would also like to log the launch command inside it.

closed as unclear what you're asking by G-Man, Thomas, Archemar, Kusalananda, msp9011 Jan 21 at 13:39

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    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
  • I still can't really understand, what you really want. If you want to log something from the command line, the logger command can do it ( man7.org/linux/man-pages/man1/logger.1.html ). More luck on the site! – peterh Jan 22 at 7:08
2

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.