Receive Linux command history to mail automatically



Basically i am a tech-enthusiastic.

When we seek for any support, Linux admins used to connect and work remotely on our servers which are hosted in a remote location.

I was so interested to know how they fix the issues , So i came up with this idea of getting the history of terminal commands they typed in to terminal into my mailbox automatically.

I tried this small script and it worked well.

I first created a script which will send a mail with the history contents.

#cd /tmp


and typed the below lines.

mail -s “Log $HOSTNAME @ $NOW” <$HOME/.bash_history

entered !wq to save and quit from vi editor and to make it executable

#chmod -x

Now the script is ready to send email with the bash history.

We can test it by executing the script #./

Now We can add the script to crontab and schedule it to send email periodically .

I wanted make it to send the command history automatically whenever  the .history file is modified whichmeans ,

whenever someone connected to terminal ,I should receive an mail with a copy of the terminal commands history.

To achive this I used incrontab . Incrontab is just like cron scheduler , but with additional capabilities

-which allows us to configure like file ,directory watch etc.,.


I downloaded incron rpm from and



After adding root user to /etc/incron.allow , I typed incrontab -l  which returnded “no table for root’  ,because I dont have any jobs configured yet.

I typed #incrontab -e to create a user table and added this line ~/.bash_history IN_MODIFY /tmp/./

( assuming we have the script file in temp directory) .


Voila thats all ! I just received an email








Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top