Top cron job for Linux admin

Cron Job For Linux Admin

With cron and shell scripting some of your daily tasks would become much easier. For Linux admin there are some tasks that should be automated. These are the tasks that are done on a daily basis and I am sure every System admin would want these tasks to be automated instead of doing it manually everyday. Cron is one of the most important tool in Linux to automate tasks such as backups, monitoring, cleaning etc.
Lets see what are these cron jobs for Linux and how to make it automated with the help of cron and shell scripting. But before going further you must be aware of two features that we will be utilizing throughout this tutorial.

a) Crontab --- Crontab is an utility in Linux that lets you run any tasks in background at any scheduled time.

Crontab format :- Minute : Hour : Day of month : Month : Day of week Command
(0-59)   (0-23)    (1-31)          (1-12)    (0-6)

Example:- Wednesday, January 31, 12:52    Reverse it and write as  52 12 31 01 04

b) Shell script -- It is a programming language a scripting language used by Linux or Unix to perform many operations on your system.

Note: All script must start with    #!/bin/bash   or #!/bin/sh  depending on what shell you have


Important commands of crontab(cron) you should know

a) To list all cron jobs currently running
# crontab -l
b) To edit cron job
# crontab -e
c) To list cron job set by specific user
# crontab -u username -l
d) To remove cron job
# crontab -r
e) To remove cron job of a specific user
# crontab -r -u username

Special strings for cron

To automate a task in cron at a scheduled time you have to set time in the cron file. There are some short cuts you can use to set time. Its more easy to use. See the table below.

@rebootRun once at reboot
@yearlyRun once in a year
@monthlyRun once every month
@weeklyRun once in a week
@dailyRun once daily
@hourlyRun every hour

For example:- To run a cron task once a day you write in a script as follows.

 0 0 * * * /bin/bash/Directory-path/command

is same as

@daily /bin/bash/Directory-path/command

So, the cron task becomes much easier using the strings.

Important directories for cron jobs

Alternatively you can put your important cron task scripts into the cron directories. These directories are under /etc directory.

# /etc/cron.daily/
# /etc/cron.hourly/
# /etc/cron.monthly/
# /etc/cron.weekly/

You can put the scripts which are to be run daily under cron.daily directory, similarly for the scripts which are to be run once in a month you can put inside cron.monthly

1. Shell script to backup files every day

I will be showing you an example here to make a shell script to backup files in a directory.
Taking backups is the most important task that's why I put it in the top most important task.
What ever you do on your system every day, like a file creation or any editing inside the file don't forget to take backups.
Don't consider to take backups manually if you have to to the do the same task everyday. Automate the task. The following example will illustrate how to create a shell script to backup files in a directory in the local computer.

Create a shell script

Create a directory "dailytask"

# mkdir dailytask
# cd dailytask
# vi


# cron job for linux
rsync -av /workings/ /backup/


Create cron task

Now  create a cron task to take backups everyday at 17:41.

# crontab -e
41 17 * * * /bin/bash /root/dailytask/


At 17:41 every day your computer will take backup from workings directory to backup directory.

2.Shell script to check computer performance

To display computer performance you can use the "top" command. But top command itself cannot display all the necessary data , Instead you have to use several other commands to display the necessary output to check the performance.
You can make a script to make the work much easier Under the daily task directory create a file called "" and edit the file as follows.

# vi /dailytask/


# cron job for linux

echo -e "\e[1;33m-------------- FREE MEMORY AND USED MEMORY -------------------------\e[0m"
free -h
echo ""
echo -e "\e[1;33m-------------- FREE DISK SPACE -------------------------------------\e[0m"
df -h
echo ""
echo -e "\e[1;33m-------------- USERS LOGGED IN AND LOAD AVERAGE --------------------\e[0m"
echo ""
echo -e "\e[1;33m-------------- ACTIVE CONNECTIONS ---------------------------------\e[0m"
netstat -tlnp
echo ""


To run the script go the directory where the script is and type the following command.

# bash


cron job for linux
Cron job for Linux


3.How to set e-mail when disk space exceeds the limit

Disk space monitoring is the most important thing that you should consider first. No doubt about that you have to check your server disk space everyday. If you hard disk crashes your whole system comes to a stop.
Here we  make a script to alert by email when the disk space exceeds certain limit,

# vi /


# cron job for linux

# Diskspace will be stored in DISKSPACE and check if its more than 85 %
DISKSPACE=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')

# If diskspace is found more than MAXIMUM a alert will be triggered.

if [ "$DISKSPACE" -gt "$MAXIMUM" ] ; then
mail -s 'Disk Space Very Low!!' techsakh@techsakh.local << EOF
Hello! Your Disk space is very low. Current Used Disk Space is = $DISKSPACE%


As the script is ready, set it for crontab task.
To know how to set it for crontab check the previous script where I have showed how to set crontab.


cron job for linux
Cron job for Linux



4. Email alert when login in Linux

A competent System administrator knows very well that how important it is to set an email alert to track any suspicious SSH root login into your server. A script can be made to set an email alert. But to set an email alert you have to set up a mail server. See A complete guide to set up local mail server in CentOS 7 .

Open file .bashrc. .bashrc is a hidden file. To display the hidden file type the command "ls -a" on  the terminal.
Paste the following line at the bottom in .bashrc file.

echo 'This is an email alert - Root Access in Hostname  on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" user@yourdomainName


# vi .bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc

echo 'This is an email alert - Root Access  in server.techsakh.local   on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" techsakh@techsakh.local


Now if there is any root login you will get an email alert Fox example see the image below of an email alert when there is a root login.

cron job for linux
Cron job for Linux

5. Shell script to create user with password

How would you feel if you are ask to create 100 users and their passwords. This would be a very hectic task if you have to do this every other day. Making a shell script to create users in Linux and their password will make your work much easier. The following example is a shell script to create users in Linux and their passwords.

# vi

# cron job for linux

# Shell script to create users in Linux

# This checks if you are a root user, id=0 means you are a root user
if [ $(id -u) -eq 0 ]; then
read -p "Enter the username : " user
read -s -p "Enter the password : " passw

# This searches the user in the passwd file for its existence
egrep -w "^$user" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$user already exists"
exit 1

# password will be encrypted and saved to the system
pass=$(perl -e 'print crypt($ARGV[0], "password")' $passw)
useradd -m -p $pass $user
[ $? -eq 0 ] && echo "User has been added to system." || echo "Failed to add user."

# If you are not root you will not be allowed to create new user and password.
echo "Only root is permitted to add user to the system"
exit 2



To run the script go to the directory where the script is stored and type the following command.

# ./

Take backups of all your cron jobs

Don't forget to take backups of cron tasks(scripts). This is how you can take the backups.Here cron.backup is a file to take the backup.

# crontab -l > /home/cron.backup


Image credit:Frederico Cintra


This is it  with Top 5 cron job for Linux admin. Stay tuned for more exciting posts.

Download Our Free eBook now

Linux and UNIX Shell scripting ebook

Be the first to comment

Leave a Reply

Your email address will not be published.