Why putting a script in /etc/cron.hourly is not working?

As you’ve probably seen in the comments to your question, the cronjobs in /etc/cron.hourly (and the other, similar directories) are executed by run-parts. run-parts is a little picky about filenames. By default it doesn’t execute files whose filenames contain anything other than (all of those from ASCII)

  • uppercase letters
  • lowercase letters
  • digits
  • underscores
  • dashes (“minus signs”)

So if your script has a filename of for example “myscript.sh”, it just is ignored, because run-parts does not like the dot.

Source: Why putting a script in /etc/cron.hourly is not working? – Ask Ubuntu

LOL!

What’s the best way to check if a volume is mounted in a Bash script?

What’s the best way to check if a volume is mounted in a Bash script? What I’d really like is a method that I can use like this: if <something is mounted at /mnt/foo> then <Do some

Source: What’s the best way to check if a volume is mounted in a Bash script?

Avoid using /etc/mtab because it may be inconsistent.

Avoid piping mount because it needn’t be that complicated.

Simply:

if grep -qs '/mnt/foo' /proc/mounts; then
    echo "It's mounted."
else
    echo "It's not mounted."
fi

ffmpeg – encode video in reverse?

Dump all video frames

$ ffmpeg -i input.mkv -an -qscale 1 %06d.jpg

Cat video frames in reverse order to FFmpeg as input
$ cat $(ls -t *jpg) | ffmpeg -f image2pipe -vcodec mjpeg -r 25 -i - -i backwards.wav
-vcodec libx264 -vpre slow -crf 20 -threads 0 -acodec flac output.mkv

via ffmpeg – encode video in reverse? – Stack Overflow.

The above ffmpeg command examples turned out to be very useful.  Previously I had to do this manually in Avidemux.

The output of my IP camera is VGA 640×480 and I needed to slice that up into a  10×10 array of little areas  (100 jpeg files) using the following:

convert input.jpg -crop 64x48  +repage  +adjoin  myoutputfile_%02d.jpg

Motion detect reveals a lot of false positives which must be filtered out manually.  In order to automate this I  compare time n to time n+1 in only a couple of the 100 little jpegs separated in the above command.   So far I’m using this:

compare -metric MAE time_n_number.jpg time_n+1_number.jpg  null: 2>&1

A changed portion of the jpeg will generate a high number which can be compared to a threshold in a script allowing me to eliminate most all false positive motion detects.

More detailed explanation for the Image compare commands in the ImageMagick package can be found here:

ImageMagick v6 Examples –Image Comparing

Therefore my entire process consists of separating the video into jpegs, finding changes in areas where there shouldn’t be changes and if not reassembling the jpegs back into a video file again all done automatically via bash scripting.

There probably are more elegant solutions but this works for now.

Archiving command history in Linux

#!/bin/bash
 umask 077
 max_lines=10000
 linecount=$(wc -l < ~/.bash_history)
 if (($linecount > $max_lines)); then
         prune_lines=$(($linecount - $max_lines))
         head -$prune_lines ~/.bash_history >> ~/.bash_history.archive \
                && sed -e "1,${prune_lines}d"  ~/.bash_history > ~/.bash_history.tmp$$ \
                && mv ~/.bash_history.tmp$$ ~/.bash_history
 fi

via BashFAQ/088 – Greg’s Wiki.

I needed to manage shell command history in a formal fashion in order to turn repeated sequences into scripts without having to type them in again.  I also wanted a  record of packages installed and in what order.   The history of commands is contained in .bash_history file which is read once when a terminal opens.   Running set -o vi allows for history commands to be recalled using standard vi commands.  The above script can be run as a user level cron job to periodically prune the top so many commands and place them into an archive.

The bash statements below set history size and make it so a command will be written to the history file immediately and not simply when a terminal closes.   These should be placed in .bashrc or whatever file executes when a new terminal opens.

HISTFILESIZE=400000000
HISTSIZE=10000
PROMPT_COMMAND="history -a"
export HISTSIZE PROMPT_COMMAND

shopt -s histappend