Automating admin tasks using shell scripts and cron Vijay Kumar Adhikari. vijay@kcm



Similar documents
Shell Scripts (1) For example: #!/bin/sh If they do not, the user's current shell will be used. Any Unix command can go in a shell script

Unix Scripts and Job Scheduling

CS Unix Tools & Scripting Lecture 9 Shell Scripting

Hands-On UNIX Exercise:

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Linux Shell Script To Monitor Ftp Server Connection

BASH Scripting. A bash script may consist of nothing but a series of command lines, e.g. The following helloworld.sh script simply does an echo.

Linux command line. An introduction to the Linux command line for genomics. Susan Fairley

Introduction to Shell Scripting

Unix Sampler. PEOPLE whoami id who

Beginners Shell Scripting for Batch Jobs

HP-UX Essentials and Shell Programming Course Summary

Outline. Unix shells Bourne-again Shell (bash) Interacting with bash Basic scripting References

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

SFTP SHELL SCRIPT USER GUIDE

Chapter 1. Backup service

Hadoop Hands-On Exercises

List of FTP commands for the Microsoft command-line FTP client

Syntax: cd <Path> Or cd $<Custom/Standard Top Name>_TOP (In CAPS)

A Tiny Queuing System for Blast Servers

Introduction to Shell Programming

Backing Up TestTrack Native Project Databases

ICS 351: Today's plan

UNIX, Shell Scripting and Perl Introduction

Answers to Even-numbered Exercises

Unix the Bare Minimum

Tor Exit Node Block Scripts

Linux Syslog Messages in IBM Director

Automating FTP with the CP IT

Systems Programming & Scripting

Command Line Crash Course For Unix

Linux Crontab: 15 Awesome Cron Job Examples

An Introduction to the Linux Command Shell For Beginners

Command Line - Part 1

BioSense 2.0. User Community Extension Project. Getting Started With The Data Lockers. Information Contributed by:

4PSA Total Backup User's Guide. for Plesk and newer versions

Extending Remote Desktop for Large Installations. Distributed Package Installs

MySQL Backups: From strategy to Implementation

A Crash Course on UNIX

Table of Contents Introduction Supporting Arguments of Sysaxftp File Transfer Commands File System Commands PGP Commands Other Using Commands

Cisco Networking Academy Program Curriculum Scope & Sequence. Fundamentals of UNIX version 2.0 (July, 2002)

UNIX / Linux commands Basic level. Magali COTTEVIEILLE - September 2009

Thirty Useful Unix Commands

AUTOMATED BACKUPS. There are few things more satisfying than a good backup

Introduction to Running Hadoop on the High Performance Clusters at the Center for Computational Research

netflow-indexer Documentation

New Lab Intro to KDE Terminal Konsole

The Linux Operating System and Linux-Related Issues

Job Scheduler Daemon Configuration Guide

This solution generally applies to v7.0 and higher of the Lawson Environment, running on Microsoft Windows, IBM AIX, Sun Solaris or HP-UX platforms.

HARFORD COMMUNITY COLLEGE 401 Thomas Run Road Bel Air, MD Course Outline CIS INTRODUCTION TO UNIX

Using SVN to Manage Source RTL

Welcome and thank you for considering enstratus as your cloud management platform.

Decision Support System to MODEM communications

Bash shell programming Part II Control statements

GeBro-BACKUP. Die Online-Datensicherung. Manual Pro Backup Client on a NAS

XCloner Official User Manual

Hadoop Hands-On Exercises

IBM Pure Application Create Custom Virtual Image Guide - Part 1 Virtual Image by extending

Overview. Remote access and file transfer. SSH clients by platform. Logging in remotely

Answers to Even- Numbered Exercises

Lecture 22 The Shell and Shell Scripting

Automated Offsite Backup with rdiff-backup

Bash Guide for Beginners

Rsync: The Best Backup System Ever

Linux logging and logfiles monitoring with swatch

Installation & Configuration Guide for Solaris 8

Red Hat System Administration 1(RH124) is Designed for IT Professionals who are new to Linux.

Introduction to Linux and Cluster Basics for the CCR General Computing Cluster

This appendix describes the following procedures: Cisco ANA Registry Backup and Restore Oracle Database Backup and Restore

Lecture 4: Writing shell scripts

Installing IBM Websphere Application Server 7 and 8 on OS4 Enterprise Linux

Submitting batch jobs Slurm on ecgate. Xavi Abellan User Support Section

An A-Z Index of the Apple OS X command line (TERMINAL) The tcsh command shell of Darwin (the open source core of OSX)

Advanced Bash Scripting. Joshua Malone

Setting Up the Site Licenses

Basic Linux & Package Management. Original slides from GTFO Security

Fundamentals of UNIX Lab Networking Commands (Estimated time: 45 min.)

Linux System Administration. System Administration Tasks

Running your first Linux Program

Using SVN to Manage Source RTL

CrontabFile Converter

LSN 10 Linux Overview

STEP 4 : GETTING LIGHTTPD TO WORK ON YOUR SEAGATE GOFLEX SATELLITE

24/08/2004. Introductory User Guide

Published. Technical Bulletin: Use and Configuration of Quanterix Database Backup Scripts 1. PURPOSE 2. REFERENCES 3.

Linux System Administration on Red Hat

Fast and efficient backups with rdiff-backup. Presented to the Fraser Valley Linux Users Group March 11, 2004 By Alan Bailward

AN INTRODUCTION TO UNIX

High Availability for Informatica Data Replication in a Cluster Environment

CS 2112 Lab: Version Control

MAX_RMAN_08137_IGNORE=5 DISK_RETENTION_POLICY='RECOVERY WINDOW OF 7 DAYS'

How to Install Multicraft on a VPS or Dedicated Server (Ubuntu bit)

USEFUL UNIX COMMANDS

Transcription:

Automating admin tasks using shell scripts and cron Vijay Kumar Adhikari vijay@kcm kcm.edu.np

How do we go? Introduction to shell scripts Example scripts Introduce concepts at we encounter them in examples Introduction to cron tool Examples

Shell The Shell is a program which provides a basic human-os interface. Two main flavors of Shells: sh,, or bourne shell. It s s derivatives include ksh (korn shell) and now, the most widely used, bash (bourne( again shell). csh or C-shell. Widely used form is the very popular tcsh. We will be talking about bash today.

sh script syntax The first line of a sh script must (should?) start as follows: #!/bin/sh (shebang, http://en.wikipedia.org/wiki/shebang ) Simple unix commands and other structures follow. Any unquoted # is treated as the beginning of a comment until end-of-line Environment variables are $EXPANDED Back-tick subshells are executed and `expanded`

Hello World script #!/bin/bash #Prints Hello World and exists echo Hello World echo $USER, your current directory is $PWD echo `ls` ls` exit #Clean way to exit a shell script ---------------------------------------- To run i. sh hello.sh ii. chmod +x hello.sh./hello.sh

Variables MESSAGE="Hello World #no $ SHORT_MESSAGE=hi NUMBER=1 PI=3.142 OTHER_PI="3.142 MIXED=123abc new_var var=$pi echo $OTHER_PI # $ precedes when using the var Notice that there is no space before and after the =.

Variables cont #!/bin/bash echo "What is your name?" read USER_NAME # Input from user echo "Hello $USER_NAME" echo "I will create you a file called ${USER_NAME}_file" touch "${USER_NAME}_file" -------------------------------------- Exercise: Write a script that upon invocation shows the time and date and lists all logged-in users. The script then saves this information to a logfile.

Sample solution #!/bin/bash DATE_TIME = date` echo $DATE_TIME USERS = `who` echo $USERS echo $DATE_TIME $USERS > log exit

Control Structures If #!/bin/bash T1=43 T2=43 T3=42 if [ $T1 = $T2 ]; then echo expression evaluated as true else echo expression evaluated as false fi if [ $T1 = $T3 ]; then echo expression evaluated as true else echo expression evaluated as false fi

Control Structures For loop #!/bin/bash for i in $( ls ); do echo item: $i done While loop #!/bin/bash COUNTER=0 while [ $COUNTER -lt- 10 ]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done

Example while loop #!/bin/bash while read f do case $f in hello) echo English ;; howdy) echo American ;; gday) ) echo Australian ;; bonjour) echo French ;; "guten tag") echo German ;; *) echo Unknown Language: $f ;; esac done

Useful file tests -d $var - file is a directory -e $var - file exists -f $var - file is a file (i.e., not a directory) -L $var - file is a symbolic link -p $var - file is a named pipe -S $var - file is a socket -o $var - file is owned by the user -r $var - user has read access -w $var - user has write access -x $var - user has execute access -z $var - file is zero-length All return True if correct

When things go wrong. -vx,, set or bash

Example - search #! /bin/sh f=$1 #first parameter passed to the script for d in * do if test -e $d/$f then echo FOUND: $d/$f exit fi done echo $f not found

Example simple one-liner #!/bin/bash find / -perm 0777 -print >`date +%Y-%m-%d`

Example route-backups #!/bin/bash TODAY=`date +%Y-%m-%d` ACCOUNT=pch@npix.woodynet.pch.net ssh $ACCOUNT show ip route > route.$today ssh $ACCOUNT show ip bgp > bgp.$today bzip2 *.$TODAY

Example Backup script #!/bin/bash SRCD="/home/" TGTD="/var var/backups/ /backups/ OF=home-$(date +%Y%m%d).tgz tar -czf- $TGTD$OF $SRCD exit

Example watch for some user #!/bin/bash case $# in 1) ;; *) echo 'usage: watchfor username' ; exit 1 esac until who grep -s "$1" >/dev/null do sleep 5 done echo "$1 has logged in"

Example ftp (non interactive) #!/bin/sh HOST=$1 USERNAME=$2 PASS=$3 FILE=$4 ftp -in <<EOF open $HOST user $USERNAME $PASS bin hash prompt dele $FILE put $FILE bye EOF echo "$FILE backed up successfully" mail -s "backup "$USERNAME@$HOST"

Example mysql-backup #/bin/bash HOST=$1; USER=$2; PASS=$3 FILENAME=`date +%Y%m%d-%H%M` DIRNAME=/home/vijay vijay/mysqldumpdir/ cd $DIRNAME mysqldump -h$host -u$user -p$pass -- all-databases > $FILENAME bzip2 $FILENAME

Example delete old dir #!/bin/bash # wished time. older dirs will be deleted. time="2005-07-10 00:00" reffile=wipeout.ref.$random touch -d "$time" $reffile$ echo echo Deletes all dirs that are older than $time echo find. -type d -maxdepth- 1 -path './*'! -newer $reffile $ while read dir; do echo rm -rf "$dir" rm -rf "$dir" done rm -f $reffile$

#!/bin/sh #Pings all the IPs in a /24 network COUNT=0 X=1 while [ $X -lt 255 ] do ping -c 1 "$1.$X" if [ $? = 0 ]; then echo "$1.$X is alive" COUNT=$(($COUNT 1)) fi X=$((X+1)) done echo $COUNT hosts responded

Crontab A crontab file contains instructions to the cron daemon of the general form: run this command at this time on this date. Each user has their own crontab,, and commands in any given crontab will be executed as the user who owns the crontab.

Crontab cont cron(8) examines cron entries once every minute The time and date fields are: Field allowed values ----- -------------- Minute 0-59 Hour 0-23 day of month 1-31 Month 1-12 (or names, see below) day of week 0-7 (0 or 7 is Sun, or use names) A field may be an asterisk (*), which always stands for first- last.

Examples crontab -e # run five minutes after midnight, every day 5 0 * * * $HOME/bin/daily.job >> $HOME/tmp tmp/out # run at 2:15pm on the first of every month -- output to be mailed 15 14 1 * * $HOME/bin/monthly 5 4 * * sun echo "run at 5 after 4 every sunday"

Examples cont */5 * * * * wget -q -O /dev/null http://classroom.kcm kcm.edu.np/cron.php 1 0 * * * /root/backup_scripts/main 2> /root/backup_scripts/logs/lastlog lastlog > /dev/null

Can you do this? Create a script that creates a zip archive of your public_html directory. Create a script that checks to see if a host is alive(responds to your ping request) Setup cron to run these scripts every 2 hours.

References http://steve-parker.org/sh/sh.shtml http://tldp tldp.org/howto/bash-prog- Intro-HOWTO.htm man 5 crontab

Thank you QUESTIONS?