SHTOOL-ROTATE.TMP(1)

πŸ“¦
version 2.0.8 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble

✨ SHTOOL-ROTATE.TMP β€” GNU shtool log file rotation ✨

okay bestie, listen up πŸ’– this one is from the GNU Portable Shell Tool manual family, it's called shtool-rotate, and it's literally the girl who does log rotation for you. let's get into it πŸŽ€


🌸 SYNOPSIS 🌸

shtool rotate [-v|--verbose] [-t|--trace] [-f|--force] [-n|--num-files
count] [-s|--size size] [-c|--copy] [-r|--remove] [-a|--archive-dir dir]
[-z|--compress [tool:]level] [-b|--background] [-d|--delay] [-p|--pad
len] [-m|--mode mode] [-o|--owner owner] [-g|--group group]
[-M|--migrate cmd] [-P|--prolog cmd] [-E|--epilog cmd] file [file ...]

like. that's a lot of flags. but she's worth it πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

this command rotates a logfile file by subsequently creating up to count (optionally compressed) archive files of it, no cap. archive files are named:

file.number[compress-suffix]

where number is the version number β€” 0 being the newest and count-1 the oldest. oldest queen stays in the back, periodt.

a rotation step consists of the following steps:

  1. remove last archive file
  2. shift archive file numbers
  3. create archive file 0 from file
  4. truncate/recreate file

iconic four-step routine honestly πŸ”₯


πŸ’… OPTIONS πŸ’…

the following command line options are available. (yes, all of them. we're doing this 🫢)

  • -v, --verbose β€” display some processing information. she's chatty, we love her.
  • -t, --trace β€” enable the output of the essential shell commands which are executed. it's giving transparency ✨
  • -f, --force β€” force silent creation of archive directory if it does not exists. also skip missing intermediate logfiles in the rotation step. default is to exit with error in both cases. FIXME
  • -n, --num-files count β€” create count archive files. default is 10.
  • -s, --size size β€” only rotate if logfile exceeds size. the argument size can be specified also with the trailing units "K" (kilo), "M" (mega) or "G" (giga). the "prolog" and "epilog" are only executed if rotation actually takes place. no unnecessary drama, only real rotation πŸŽ€
  • -c, --copy β€” copy file to archive then truncate original. the default is to move file to archive.

unless an application reopens its logfile it will continue to write to the same file. in the default move case the application will write to the archive which it had previously opened as file. in the copy case the application will write to the original file. the drawback of the copy approach is that logfile entries are lost when they are written to file between the execution of the copy and the truncation operation. the drawback of the move approach is that the application needs to detect the move or must be triggered to reopen its log (i.e. through epilog).

so basically: pick your struggle, bestie 😭 both have a catch, and the manpage is NOT hiding it.
  • -r, --remove β€” removes file after rotation instead of providing a new empty file. bye girl πŸ’€
  • -a, --archive-dir dir β€” specify the archive directory. default is to create archives in the same directory as file is located.
  • -z, --compress [tool:]level β€” enables compression of archive files with compression level level. by default, the tools bzip2(1), gzip(1) and compress(1) are searched for in $PATH, but one also can override this by prefixing the compression level with one of the three particular tool names.
  • -b, --background β€” enable background compression. multitasking queen πŸ’…
  • -d, --delay β€” delays the compression of archive file number 0. this is useful if `-c` is not used, because an application might continue to write to archive file 0 through an already open file handle.
  • -p, --pad len β€” enables padding with leading zeros in the number part of the filename file.numbercompress-suffix. the default padding len is 1. this is interesting if more than 10 archive files are used, because it leads to still sorted directory listings. aesthetic AND functional 🌸
  • -m, --mode mode β€” the file mode applied to the created files, see chmod(1). setting mode to "-" skips this step and leaves the operating system default which is usually based on umask(1). some file modes require superuser privileges to be set. default is 0755.
  • -o, --owner owner β€” the file owner name or id applied to the created files, see chown(1). this option requires superuser privileges to execute. default is to skip this step and leave the operating system default which is usually based on the executing uid or the parent setuid directory.
  • -g, --group group β€” the file group name or id applied to the created files, see chgrp(1). this option requires superuser privileges to execute to the fullest extend, otherwise the choice of group is limited on most operating systems. default is to skip this step and leave the operating system default which is usually based on the executing gid or the parent setgid directory.
  • -M, --migrate cmd β€” execute a "migration" command just before the archive file number count-1 is removed from the filesystem. the specified cmd receives the archive filename as command line argument. one last goodbye ritual πŸ•―οΈ
  • -P, --prolog cmd β€” execute a "prolog" command before the rotation step. useful in conjunction with -s.
  • -E, --epilog cmd β€” execute a "epilog" command after the rotation step. useful in conjunction with -s.

🌈 EXAMPLE 🌈

 #   shell script
 shtool rotate -n10 -s1M -zbzip2:9 -d -r /var/log/ap.access.log
 shtool rotate -n5 -s128K -zbzip2:9 -d -r /var/log/ap.error.log
 apachectl graceful

look at her go 😭 apache graceful at the end is the main character moment fr fr.


πŸ’œ HISTORY πŸ’œ

the GNU shtool rotate command was originally written by Ralf S. Engelschall <rse@engelschall.com> in 2001 for GNU shtool. its development was prompted by the need to have a flexible logfile rotation facility in the OpenPKG project. a true origin story πŸ‘‘


πŸ‘‘ SEE ALSO πŸ‘‘


okay that's the whole thing bestie, you're officially a log rotation girlie now πŸ¦‹

this was shtool 2.0.8 from 18-Jul-2008 β€” the source/version line and date, tucked in right here at the end like a little signature πŸ’– mwah, go rotate something ✨

πŸ“¦ where this page comes from