Mentoring SysAdmin Session 3

Useful commands

Cron-job

Cron-job is a tool for triggering application based on a schedule, when you need to run an specific task on a time basis logic you can use cron-tab, currently there are a ton of tools for running programmed tasks but in scense all have the same behaviour and configuration as cron-tab.

You can recognize the same configuration configuration files for kubernetes, aws, etc.

1
2
# AWS example
aws events put-rule --schedule-expression "cron(*/5 * * * *)" --name MyRule1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Kubernetes example
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

How to write a cron job

To create a cron job you need to run crontab -e, it will create a cron job for the current user.

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday)
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>

You can also use reserved words to execute commands:

Entry Description Equivalent to
@yearly (or @annually) Run once a year at midnight of 1 January 0 0 1 1 *
@monthly Run once a month at midnight of the first day of the month 0 0 1 * *
@weekly Run once a week at midnight on Sunday morning 0 0 * * 0
@daily (or @midnight) Run once a day at midnight 0 0 * * *
@hourly Run once an hour at the beginning of the hour 0 * * * *
@reboot Run at startup N/A

Examples

1
2
3
0 * * * * bash /home/gabriela/run.sh
*/5 * * * * bash /home/gabriela/run.sh
* */2 * * * bash /home/gabriela/run.sh

Exit codes

Each script or program that is executed on a bash always (with certain exceptions) put a exit code to know if the program execution was successful or not.

  • Exit code 0 = Success
  • Exit code from 1 to 255 Failure

Some programs add specific exit codes depending on the failure type. But for convention we use:

  • 1 - Catchall for general errors
  • 2 - Misuse of shell builtins (according to Bash documentation)
  • 126 - Command invoked cannot execute
  • 127 - “command not found”
  • 128 - Invalid argument to exit
  • 128+n - Fatal error signal “n”
  • 130 - Script terminated by Control-C
  • 255\* - Exit status out of range

You can use echo $? to know the last command exit code.

This is useful to know since on the pipeline when you have complex tasks you can return an specific exit code for handling exceptions.

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

cat file.txt

if [ $? -eq 0 ]
then
  echo "The script ran ok"
  exit 0
else
  echo "The script failed" >&2
  exit 1
fi

Excercise time

Create a bash script to write to a file called status.txt and a cron job to run that script every 2 minutes:

  • Success when the file.txt exists.
  • Error when the file.txt doesn’t exists.