• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

cron

Crontab on macOS Monterey

November 29, 2022

A recent OS upgrade rendered the crontab to malfunction on macOS Monterey. It turned out the system just needed a reset of System Preferences > Security & Privacy > Privacy tab, and to make sure cron has full access to disks. Once you flipped that, your crontab should start working. Hope that helps.

Filed Under: Linux Tagged With: cron, mac os, monterey, reset

AWS LightSail Delete Snapshots

August 8, 2019

Here’s my script to delete AWS LightSail snapshots. It can delete daily or weekly backups and scheduled via crontab. If you want longer or shorter retention, adjust the expired value. It’s in seconds. 604800 is 1 week. 2592000 is 1 month.

#!/bin/bash
 
current=$(date +%s)
 
if [ $# -eq 0 ]; then
  exit 1
fi
 
if [[ $1 = 'daily' ]]; then
  prefix='daily'
  expired=$(($current-604800))
elif [[ $1 = 'weekly' ]]; then
  prefix='weekly'
  expired=$(($current-2592000))
else
  exit 1
fi
 
snaps='/root/snapshots/snapshots.json'
names='/root/snapshots/names.txt'
parse='/root/snapshots/parse.txt'
logfile='/root/snapshots/snapshots.log'
 
/usr/local/bin/aws lightsail get-instance-snapshots > $snaps
cat $snaps | jq -r '.instanceSnapshots[] | .name' > $names
cat $names | grep $prefix > $parse
 
while read -r line; do
 
  snapshot=$(echo $line | cut -d_ -f3)
  snapshotname=$line
 
  if [ `expr $snapshot + 1 2> /dev/null` ] ; then
 
    if [ $snapshot -le $expired ]; then
      echo 'Deleted: '$snapshotname >> $logfile
      /usr/local/bin/aws lightsail delete-instance-snapshot \
      --instance-snapshot-name $snapshotname
    else
      echo 'Nothing: '$snapshotname >> $logfile
    fi
 
  else
    echo $snapshot is not numeric > /dev/null
  fi
 
done < $parse
 
echo 'Current time: '$current >> $logfile
echo 'Expired time: '$expired >> $logfile
echo '-----------------------------------' >> $logfile

#!/bin/bash current=$(date +%s) if [ $# -eq 0 ]; then exit 1 fi if [[ $1 = 'daily' ]]; then prefix='daily' expired=$(($current-604800)) elif [[ $1 = 'weekly' ]]; then prefix='weekly' expired=$(($current-2592000)) else exit 1 fi snaps='/root/snapshots/snapshots.json' names='/root/snapshots/names.txt' parse='/root/snapshots/parse.txt' logfile='/root/snapshots/snapshots.log' /usr/local/bin/aws lightsail get-instance-snapshots > $snaps cat $snaps | jq -r '.instanceSnapshots[] | .name' > $names cat $names | grep $prefix > $parse while read -r line; do snapshot=$(echo $line | cut -d_ -f3) snapshotname=$line if [ `expr $snapshot + 1 2> /dev/null` ] ; then if [ $snapshot -le $expired ]; then echo 'Deleted: '$snapshotname >> $logfile /usr/local/bin/aws lightsail delete-instance-snapshot \ --instance-snapshot-name $snapshotname else echo 'Nothing: '$snapshotname >> $logfile fi else echo $snapshot is not numeric > /dev/null fi done < $parse echo 'Current time: '$current >> $logfile echo 'Expired time: '$expired >> $logfile echo '-----------------------------------' >> $logfile

Schedule deletes via crontab.

# run daily at 5am
0 5 * * * /bin/bash /root/snapshots/delete-snapshot.sh daily 2>&1
# run weekly every sunday at 6am
0 6 * * 0 /bin/bash /root/snapshots/delete-snapshot.sh weekly 2>&1

# run daily at 5am 0 5 * * * /bin/bash /root/snapshots/delete-snapshot.sh daily 2>&1 # run weekly every sunday at 6am 0 6 * * 0 /bin/bash /root/snapshots/delete-snapshot.sh weekly 2>&1

Filed Under: Cloud, Linux Tagged With: aws, bash, cli, cron, delete, lightsail, snapshots

AWS LightSail Create Snapshots

August 8, 2019

Here’s my script to create snapshots of a LightSail instance. Create daily or weekly snapshots.

#!/bin/bash
if [ $1 != '' ]; then 
  prefix=$1
  timestamp=$(date +%s)
  snapshotname=$prefix'_web-server_'$timestamp
  /usr/local/bin/aws lightsail create-instance-snapshot \
  --instance-snapshot-name $snapshotname \
  --instance-name web-server
else
  echo 'Need one argument. e.g. daily or weekly'
fi

#!/bin/bash if [ $1 != '' ]; then prefix=$1 timestamp=$(date +%s) snapshotname=$prefix'_web-server_'$timestamp /usr/local/bin/aws lightsail create-instance-snapshot \ --instance-snapshot-name $snapshotname \ --instance-name web-server else echo 'Need one argument. e.g. daily or weekly' fi

Crontab. Job can be scheduled daily or weekly.

# run daily at 4am
0 4 * * * /bin/bash /root/snapshots/create-snapshot.sh daily 2>&1
# run weekly every sunday at 5am
0 5 * * 0 /bin/bash /root/snapshots/create-snapshot.sh weekly 2>&1

# run daily at 4am 0 4 * * * /bin/bash /root/snapshots/create-snapshot.sh daily 2>&1 # run weekly every sunday at 5am 0 5 * * 0 /bin/bash /root/snapshots/create-snapshot.sh weekly 2>&1

Filed Under: Cloud Tagged With: aws, cli, create, cron, lightsail, snapshots

Adding to Crontab in Bash

January 10, 2019

Here’s a neat command to append a job to a crontab within a Bash script.

crontab -l | { cat; echo "0 1 * * * /dir/script.sh > /logs/script.cronlog 2>&1"; } | crontab -

crontab -l | { cat; echo "0 1 * * * /dir/script.sh > /logs/script.cronlog 2>&1"; } | crontab -

To validate, perform crontab -l to see if your changes are there.

crontab -l

crontab -l

Filed Under: Linux Tagged With: bash, cron, crontab

Create a Crontab via Script

December 30, 2018

How do you create a crontab entry via a Bash script? That’s a good question. Here is one solution:

(crontab -l 2>/dev/null; echo "* 11 * * * /path/to/job -with args") | crontab -

(crontab -l 2>/dev/null; echo "* 11 * * * /path/to/job -with args") | crontab -

If you want to run it as a specific user, use the following:

(crontab -l -u user 2>/dev/null; echo "* 11 * * * /path/to/job -with args") | crontab -

(crontab -l -u user 2>/dev/null; echo "* 11 * * * /path/to/job -with args") | crontab -

Filed Under: Linux Tagged With: bash, cron, user

Cronjob Not Running

April 16, 2018

I had issues with cron. The jobs were not running, so it seemed. It turned out that they were in fact running. The jobs were firing off based on schedule. The problem was, cron didn’t have the correct path to the commands. I realized this when I looked at a couple of log files, in /var/mail and /var/log/syslog to look for clues. The fix was to include the path in cron.

Edit cron.

crontab -e

crontab -e

Add the shell and path at the top of the file.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

Filed Under: Linux Tagged With: cron

Cron Allow

December 21, 2017

Some Linux distributions don’t turn on cron automatically for users. To give certain users access to cron, add them to cron.allow.

Edit cron.allow.

$ sudo nano /etc/cron.allow

$ sudo nano /etc/cron.allow

Add one user per line.

johndoe

johndoe

Save and close.

Filed Under: Linux Tagged With: cron

Implementing Surfcali.com

November 6, 2016

I have a website called Surfcali.com. It’s a website that provides tide table information for select California beaches. The tide information on the website is auto-generated by the xtide program which is available on most Linux distributions.

You can install xtide on Ubuntu by typing this command from the Terminal.

sudo apt-get install xtide

sudo apt-get install xtide

Instead of explaining how the website is implemented using lots of words, it’s probably much easier to explain it via video.

So, here’s a short video of how Surfcali.com was put together.

Video

Filed Under: CSS, HTML, PHP Tagged With: bash, cron, surfcali.com, tide table, xtide

  • Home
  • About
  • Archives

Copyright © 2023