Welcome to STXLUG Livestream

1-25-20

Topic: Bash Scripts and Cron Jobs

Meeting Notes

Intro and News

Bash Scripts

According to Wikibooks

Shell scripts are commonly used for many system administration tasks, such as performing disk backups, evaluating system logs, and so on. They are also commonly used as installation scripts for complex programs. They are particularly suited to all of these because they allow complexity without requiring it: if a script just needs to run two external programs, then it can be a two-line script, and if it needs all the power and decision-making ability of a Turing-complete imperative programming language, then it can have that as well.

Writing your first Bash Script

Writing your first Bash Script can be very easy. All you will need is a standard text editor and an idea.

  1. Open up a text editor
  2. At the top of the document write #!/bin/bash
  3. Add your commands
  4. Make script executable with chmod +x
  5. Run your script

Examples of Bash Scripts

  • Scenario 1:
    You want to install several programs.
  • Scenario 2:
    You want to want to backup your Documents directory

Cron Jobs

According to Wikipedia.

The software utility cron is a time-based job scheduler in Unix-like computer operating systems. Users that set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals. The origin of the name cron is from the Greek word for time, χρόνος (chronos).

See this wikipedia entry on Cron.

Creating a Cron job

Time Breakdown

Minutes / Hours / Day of Month / Month / Day of Week

Cron Helper https://cron.help

  • Scenario 1:
    Accounting wants to make sure they have a backup of their work. They request a monthly backup at the beginning of the month.
    • */60 23 1 * * This format gives you a cron job that will execute at 11pm on the first of the month
  • Scenario 2:
    Accounting does critical work on the 28th of every month. They want to make sure they have redundant backups on that day. They request multiple backups of their work on the 28 of every month. MINUTES HOURS DAY OF MONTH MONTHS DAY OF WEEK
    • */60 * 28 * * This will give you a cron job that executes at every hour on the 28th of the month
  • Scenario 3:
    Marketing needs a new folder created for the art team every Monday morning. MINUTES HOURS DAY OF MONTH MONTHS DAY OF WEEK
    • */60 7 * * 1 This cron job will execute at 7am on Monday

Final thoughts

Bash Scripts

Bash scripts are extremely useful to administer a Linux machine. You can do pretty much anything in an automated fashion with Bash scripts. As long as you can execute it in a terminal you can run a bash script.

Cron Jobs

If you combine Bash Scripts with the power of Cron Jobs, you will be a Linux power user. Not only can you do awesome things like automate backups but you can ensure that you save on resources like time. So start being more efficient and write some cron jobs.

Thanks

Thanks to everyone who joined in and I hope you have a great weekend!

Would you like to support my work?

default-green.png