I admit I am terrible with scripting, so when I was asked to store users' command history lines and zip them on monthly basis what I did was to create a file "user_history_Feb" with the following contents:
Part A
[root@H99XXX bin]# more user_history_Feb
cp -p /var/log/user_history/*history /var/log/user_history/old_logs/
gzip -9 /var/log/user_history/old_logs/*history
cd /var/log/user_history/old_logs
mv *gz /var/log/user_history/old_logs/02
[root@H99XXX bin]#
and a cronjob for it to run end of Feb:
Part B
[root@H99 bin]# crontab -l | grep user_history_Feb
01 22 29 2 * /usr/local/bin/user_history_Feb
[root@H99 bin]# uname -a
Linux H99 2.6.18-274.17.1.el5 #1 SMP Wed Jan 4 22:45:44 EST 2012 x86_64 x86_64 x86_64 GNU/Linux
[root@H99 bin]#
I am using RHEL 5.7
I know my "script" is no script, and would be very thankful if someone could help me into scriptifying part A so I can adjust the cron :o
PART A:
Script user_whtever.sh
#!/bin/bash
mkdir -p /var/log/user_history/old_logs # If the folder is not present, it will be created automatically
cp -p /var/log/user_history/*history /var/log/user_history/old_logs/
gzip -9 /var/log/user_history/old_logs/*history
mv /var/log/user_history/old_logs/*.gz /var/log/user_history/old_logs/02
After you have created the above script, change its permission:
[root@H99XXX bin]# chmod 750 user_whtever.sh
[root@H99 bin]# crontab -l | grep user_
01 22 29 2 * /usr/local/bin/user_whtever.sh
[PS: /var/log/user_history/old_logs/02 is file or folder?]
It's a directory
[root@H99 ~]# ls -ld /var/log/user_history/old_logs/02
drwxr-xr-x 2 root root 4096 Feb 1 14:52 /var/log/user_history/old_logs/02
[root@H99 ~]# date
Thu Feb 2 16:48:38 JST 2012
I've created all the folders in the script I provided in the post above, but how do I create a subsequent directory according to the next month
e.g 03 for March, 04 for April...
So the end results would be like this
- all the *history files from /var/log/user_history/ dumped into something like this:
/var/log/user_history/old_logs/02/*gz
/var/log/user_history/old_logs/03/*gz
/var/log/user_history/old_logs/04/*gz
You can do like this for the Destination Directory:
#!/bin/bash
OLD_HIST_DIR="/var/log/user_history/old_logs"
HIST_DIR="/var/log/user_history"
MNTH_DEST_DIR="/var/log/user_history/old_logs/$(date +'%m')"
# date +'%m' gives number corresponding to the current month in 01,02,...11,12 fashion
mkdir -p "${MNTH_DEST_DIR}" "${OLD_HIST_DIR}" # This will create the directory if not present
cp -p ${HIST_DIR}/*history ${OLD_HIST_DIR}
gzip -9 ${OLD_HIST_DIR}/*history
mv ${OLD_HIST_DIR}/*.gz ${MNTH_DEST_DIR}
1 Like
hang on, let me try this out on my DEV
---------- Post updated at 01:14 AM ---------- Previous update was at 01:02 AM ----------
For some reason I am getting error and the zipped files are being stored in /var/log/user_history/old_logs/ instead of /var/log/user_history/old_logs/02
The error:
[root@H99 02]# /usr/local/bin/testing
/usr/local/bin/testing: line 7: fg: no job control
mv: `/var/log/user_history/old_logs/d12adm_history.gz' and `/var/log/user_history/old_logs/d12adm_history.gz' are the same file
the script:
[root@H99 old_logs]# more /usr/local/bin/testing
#!/bin/bash
OLD_HIST_DIR="/var/log/user_history/old_logs"
HIST_DIR="/var/log/user_history"
MNTH_DEST_DIR="/var/log/user_history/old_logs/$(date +`%m`)"
cp -p ${HIST_DIR}/*history ${OLD_HIST_DIR}
gzip -9 ${OLD_HIST_DIR}/*history
mv ${OLD_HIST_DIR}/*.gz ${MNTH_DEST_DIR}
[root@H99 old_logs]#
---------- Post updated at 01:18 AM ---------- Previous update was at 01:14 AM ----------
OK, I figured out what went wrong. I used the ` ` instead of ' ' for the month
Thanks alot, works like charm!
how can you fetech the cmd history log of the all login,where is the path to store the users performed command.:Dtake me know!thank you!
It would be nice if you can sentence your request properly.
I have C Shell and Bash Shell users, whose .cshrc and .bashrc are modified as such
C Shell
set histfile=/var/log/user_history/db2s12_history
Bash Shell
export HISTFILE=/var/log/user_history/db2ash_history