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
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.
|@reboot||Run once at reboot|
|@yearly||Run once in a year|
|@monthly||Run once every month|
|@weekly||Run once in a week|
|@daily||Run once daily|
|@hourly||Run 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
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.
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 dailyscript.sh
# 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/dailyscript.sh|
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 "performa.sh" and edit the file as follows.
# vi /dailytask/performa.sh
echo -e "\e[1;33m-------------- FREE MEMORY AND USED MEMORY -------------------------\e[0m"
To run the script go the directory where the script is and type the following command.
# bash performacheck.sh
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 /diskmail.sh
# Diskspace will be stored in DISKSPACE and check if its more than 85 %
# If diskspace is found more than MAXIMUM a alert will be triggered.
if [ "$DISKSPACE" -gt "$MAXIMUM" ] ; then
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.
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
# User specific aliases and functions
alias rm='rm -i'
# Source global definitions
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`" firstname.lastname@example.org
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.
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 createuser.sh
# Shell script to create users in Linux
# This checks if you are a root user, id=0 means you are a root user
# This searches the user in the passwd file for its existence
# password will be encrypted and saved to the system
# If you are not root you will not be allowed to create new user and password.
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.