• Skip to primary navigation
  • Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

log

Logging Commands

by Ulysses · Feb 22, 2021

I decided to log my commands for good record keeping. Here’s my script.

#!/bin/bash
if [[ $# -eq 0 ]] ; then echo 'No arguments. Format: ./go.sh script.sh'; exit 0; fi
log=log.txt
echo '-------------------------------' >> $log
date >> $log
echo '---' >> $log
cat $1 >> $log
echo '---' >> $log
/bin/bash $1 2>&1 | tee -a $log

#!/bin/bash if [[ $# -eq 0 ]] ; then echo 'No arguments. Format: ./go.sh script.sh'; exit 0; fi log=log.txt echo '-------------------------------' >> $log date >> $log echo '---' >> $log cat $1 >> $log echo '---' >> $log /bin/bash $1 2>&1 | tee -a $log

To run.

./go.sh script.sh

./go.sh script.sh

Line by line explanation.

  1. Checks for script to run. Exits if there’s no argument.
  2. Sets the log file.
  3. Prints dashes.
  4. Prints the date.
  5. Print 3 dashes.
  6. Prints the commands to the log file.
  7. Print 3 dashes.
  8. Run the command. Send error and standard out to log file.

Filed Under: Linux Tagged With: bash, commands, log, tee

Setup Log Rotation

by Ulysses · Jan 16, 2021

Logrotation is available in most Linux distros. If they are missing, you can easily install them. The logrotation main configuration file is located in /etc/logrotate.conf which is the default setting. The config file contains an include statement that pulls in other log configurations located in the /etc/logrotate.d/ directory. When setting up a log rotation, it’s best to add your log rotation configuration in this directory.

Here’s a simple configuration in a file called webmon.

/home/ubuntu/webmon.log {
        daily
        create 0640 ubuntu ubuntu
        dateext
        rotate 7
	nocompress
}

/home/ubuntu/webmon.log { daily create 0640 ubuntu ubuntu dateext rotate 7 nocompress }

The options above does the following:

  • The logs are rotated daily.
  • The logs are rotated 7 times with no compression.
  • The rotate log files will have dates appended to them.
  • The log files are owned user ubuntu with permission of 0640.

There are more options from the logrotate man pages which can be added.

Filed Under: Linux Tagged With: configuration, log, rotation

Audit Log Files

by Ulysses · Nov 10, 2020

The /var/log/audit/audit.log files were not being rotated. The files in the directory grew so large that it created disk space issues on /. Here’s the fix. In the /etc/audit/auditd.conf file, change max_log_file_action from “ignore” to “rotate.” Instead of hundreds of files being kept, it will be rotated up to 5 files.

max_log_file_action = rotate
num_logs = 5

max_log_file_action = rotate num_logs = 5

Restart service to take effect. The old files will be deleted.

service auditd stop
service auditd start
service auditd status

service auditd stop service auditd start service auditd status

Disk space went from 82% down to 25%.

Filed Under: Linux Tagged With: audit, files, log, out of disk space, rotate

Internet Test

by Ulysses · Jun 26, 2020

My internet has been flaky lately. I wrote a script that logs my internet connection.

I’m running it in cron every minute.

#!/bin/bash
log='/home/ulysses/Code/spectrum.log'
if nc -zw1 google.com 443;
then 
  echo 'Spectrum is up' $(date) >> $log
else 
  echo 'Spectrum is down' $(date) >> $log
fi

#!/bin/bash log='/home/ulysses/Code/spectrum.log' if nc -zw1 google.com 443; then echo 'Spectrum is up' $(date) >> $log else echo 'Spectrum is down' $(date) >> $log fi

To view, just cat the log.

cat spectrum.log 
Spectrum is up Fri Jun 26 15:14:01 EDT 2020
Spectrum is up Fri Jun 26 15:15:01 EDT 2020
Spectrum is up Fri Jun 26 15:16:01 EDT 2020
Spectrum is up Fri Jun 26 15:17:01 EDT 2020
Spectrum is up Fri Jun 26 15:18:01 EDT 2020

cat spectrum.log Spectrum is up Fri Jun 26 15:14:01 EDT 2020 Spectrum is up Fri Jun 26 15:15:01 EDT 2020 Spectrum is up Fri Jun 26 15:16:01 EDT 2020 Spectrum is up Fri Jun 26 15:17:01 EDT 2020 Spectrum is up Fri Jun 26 15:18:01 EDT 2020

So, I now have a record when it was up or down.

Filed Under: Linux Tagged With: connection, internet, log, spectrum

Tail A File in Windows Server

by Ulysses · Nov 19, 2019

Linux has tail command. What about Windows? You can use Powershell to tail a file.

Get-Content myLog.log –Wait

Get-Content myLog.log –Wait

Filed Under: Windows Tagged With: file, log, powershell, tail, windows

Copyright © 2012–2021