The officially official Devuan Forum!

You are not logged in.

#1 2020-03-01 03:03:40

Kiwi Rider
Member
Registered: 2020-02-01
Posts: 8  

Append date and time to .bash_history at start of session

Hi there.

Something I'm not up to figuring out ATM...

When I log into a machine I'd like to have the current date and time of the session added to ~/.bash_history.

I don't want it for every command line, just at the start of the session, eg

##
##Session begin 1 Mar 2020 16:00
##
cd /etc/apt
tail sources.list
....

Sometimes when I've been working on something across multiple accounts it'd be nice to be able to start to follow the order a bit easier, so having the date/time I start a session would be quite nice.

I'm sure someone else has done this but I cannot think of the right search terms to find the answer, nor am I familiar enough with the system to work it out myself at the moment. Everything points to sites explaining how to add a timestamp to every command but I don't want that, just session starts.

Session end would be fine as well, as it still separates each day/logon.

Much thanks.

Offline

#2 2020-03-01 10:47:30

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 976  
Website

Re: Append date and time to .bash_history at start of session

Create a file at ~/.bash_profile with this content:

. ~/.profile
echo "##
##Session begin $(date)
##" >> ~/.bash_history

Black Lives Matter

Offline

#3 2020-03-22 12:46:45

rolfie
Member
Registered: 2017-11-25
Posts: 400  

Re: Append date and time to .bash_history at start of session

Tried that and created the .bash_profile in the /root folder of a Beowulf installation in a VM based on the Beta DVD, does not work. I use Mate, the root terminal is started by pkexec mate-terminal.

What can be wrong? Rights?

rolfie

PS: seems to work when I start the Mate terminal as user and switch to root with su -. How can I make this work via pkexec?

Last edited by rolfie (2020-03-22 12:55:49)

Offline

#4 2020-03-22 13:04:09

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 976  
Website

Re: Append date and time to .bash_history at start of session

Don't use pkexec to start a root terminal, that's a silly idea. Why do the MATE devs do that? hmm

Use this to start a root terminal:

x-terminal-emulator -e su -

Create a file at ~/.local/share/applications/root-terminal.desktop to make the action available via the XDG menu sytem:

[Desktop Entry]
Version=1.0
Name=Root Terminal
Comment=Run terminal emulator as root
Type=Application
Exec=x-terminal-emulator -e su -
Categories=System;TerminalEmulator;

Black Lives Matter

Offline

#5 2020-03-22 13:56:54

fsmithred
Administrator
Registered: 2016-11-25
Posts: 1,619  

Re: Append date and time to .bash_history at start of session

Putting it in ~/.bash_profile didn't work for me. I suppose I have to log out of my desktop session and restart it. Putting it in ~/.bashrc works every time I open a new terminal on the desktop. If I put it in /root/.bashrc, it works every time I do 'su' or 'su -' in a terminal.

Offline

#6 2020-03-22 15:08:09

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 976  
Website

Re: Append date and time to .bash_history at start of session

fsmithred wrote:

Putting it in ~/.bash_profile didn't work for me. I suppose I have to log out of my desktop session and restart it.

Yes, that's right.

fsmithred wrote:

Putting it in ~/.bashrc works every time I open a new terminal on the desktop.

But then you'll have a new "##Session begin" section for every time a terminal is opened, which doesn't appear to be what the OP wants.


Black Lives Matter

Offline

#7 2020-03-22 15:21:34

rolfie
Member
Registered: 2017-11-25
Posts: 400  

Re: Append date and time to .bash_history at start of session

Head_on_a_Stick wrote:

Don't use pkexec to start a root terminal, that's a silly idea. Why do the MATE devs do that? hmm

Please can you explain why this is a silly idea?

And its not a MATE devs idea. I put something together about a year ago, related to dropping gksu: https://dev1galaxy.org/viewtopic.php?id=2717

Head_on_a_Stick wrote:

Use this to start a root terminal:

x-terminal-emulator -e su -

That line works fine as additional entry in the menu.

But: I don't like xterm. The font is ugly and much too small for my eyes, and the window is too small. Mate terminal gives me exactly what I want and need, and offers to increase the window to a width of 132 characters.

There should be a way to achieve this.

rolfie

Offline

#8 2020-03-22 15:29:05

rolfie
Member
Registered: 2017-11-25
Posts: 400  

Re: Append date and time to .bash_history at start of session

fsmithred wrote:

Putting it in ~/.bash_profile didn't work for me. I suppose I have to log out of my desktop session and restart it. Putting it in ~/.bashrc works every time I open a new terminal on the desktop. If I put it in /root/.bashrc, it works every time I do 'su' or 'su -' in a terminal.

Entering:

. ~/.profile
echo "##
##Session begin $(date)
##" >> ~/.bash_history

into /root/.bashrc causes a memory access error (Speicherzugriffsfehler in German), no matter if I use Mate with pkexec, su - or xterm. Whats wrong here? Guess I have to use the rescue mode again.

It does not matter if the separator is inserted every time I use the root terminal, I like the idea to have a better feeling for when I did something administering my installations.

BTW: the machine in question is Beowulf Beta from DVD in a VBox with UEFI encrypted with LVM. Rebooting the VM didn't make a difference.

rolfie

Edit: could fix it via opening the root folder under the Mate file manager with administrative privileges and using geany to remove the modifications from the .bashrc file.

Last edited by rolfie (2020-03-22 16:21:17)

Offline

#9 2020-03-22 16:02:04

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 976  
Website

Re: Append date and time to .bash_history at start of session

rolfie wrote:
Head_on_a_Stick wrote:

Don't use pkexec to start a root terminal, that's a silly idea. Why do the MATE devs do that? hmm

Please can you explain why this is a silly idea?

Because it runs the entire terminal emulator as root. Using my suggested command runs the emulator under the normal user and just runs the shell as root. See also https://en.wikipedia.org/wiki/Principle … _privilege

rolfie wrote:

But: I don't like xterm.

# update-alternatives --config x-terminal-emulator # then select your favourite emulator

Black Lives Matter

Offline

#10 2020-03-22 16:33:10

rolfie
Member
Registered: 2017-11-25
Posts: 400  

Re: Append date and time to .bash_history at start of session

I am always willing to learn, you got a point there. update-alternatives allows to switch to the mate-terminal-wrapper, I will change my default mode to that proposal. This also fixes the problems with the text in the bash_history.

Thanks, rolfie

Offline

#11 2020-06-06 10:21:56

Kiwi Rider
Member
Registered: 2020-02-01
Posts: 8  

Re: Append date and time to .bash_history at start of session

Head_on_a_Stick wrote:
fsmithred wrote:

Putting it in ~/.bash_profile didn't work for me. I suppose I have to log out of my desktop session and restart it.

Yes, that's right.

fsmithred wrote:

Putting it in ~/.bashrc works every time I open a new terminal on the desktop.

But then you'll have a new "##Session begin" section for every time a terminal is opened, which doesn't appear to be what the OP wants.

Well. actually that'd be quite good I think . Sometimes I'm switching between normal user and root, and if I come back to something after a while I might see a string of commands in root's history but there was a break where I dropped back to normal user first.

As I said, I wouldn't want it for every single command, but at the start of any shell session is fine and can help a lot to trace things.

Thanks again to for the suggestions guys! It has helped some with keeping on top of some maintenance for a project where I really must work out a script that runs under cron, at least the gruntwork.

Offline

Board footer