Manual operations lead to human errors. Adding and removing Cronjobs frequently can be a very time-consuming task. In this article, we will create Shell scripts that automate the addition and deletion of Cronjobs from Ubuntu EC2 instances on AWS. To perform these operations you will need access to the EC2 instance. The user you will use needs to have sudo access so that the user can switch to root and perform addition and deletion of Cronjobs.
Let's get started.
Pre-requisites
- Basic understanding of Shell scripts and Cronjobs.
- AWS Account (Create if you don’t have one).
- EC2 Instance with the user having sudo access (Click here to learn to create an EC2 instance if you don’t have one or if you want to learn )
What will we do
- Create a shell script to add Cronjobs.
- Execute the Shell script to add a Cronjob.
- Create a shell script to remove Cronjobs.
- Execute the Shell script to remove the Cronjob.
Create a shell script to add Cronjobs
Create a file on your local Linux System and add the following code to it. You can also find the code on my Github repo on the following link.
Github Link: https://github.com/shivalkarrahul/DevOps/blob/master/aws/shell-scripts/aws-ec2-add-remove-cron-job/add-cronjob.sh
File: add-cronjob.sh
#!/bin/bash helpFunction() { echo "" printf "\033[1;32mUsage: $0 -K <internal.pem> -U <internal-user> -I <internal-ip> -a <cron-to-be-added>" echo "" echo -e "\t-K \".pem key of the server on which a cron job has to be added\"" echo -e "\t-U UserName of the server on which a cron job has to be added" echo -e "\t-I IP of the server on which a cron job has to be added" echo -e "\t-a Name of the cron to be added (in double quotes)" echo "Add a new Cron Job" echo "e.g." echo "./add-cronjob.sh -K /Users/cloudcover/Documents/Rahul/access/rahuls.pem -U ubuntu -I ec2-35-180-234-158.eu-west-3.compute.amazonaws.com -a \"0 5 * * 1 testCronJob\"" echo -e "\033[0m" #reset color exit 1 # Exit script after printing help } while getopts "I:K:U:a:" opt do case "$opt" in K ) internalServerPemKey="$OPTARG" ;; U ) internalServerUser="$OPTARG" ;; I ) internalServerIP="$OPTARG" ;; a ) addCron="$OPTARG" ;; ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent esac done echo "******************" #echo $listCronJobs # Print helpFunction in case parameters are empty if [ -z "$internalServerIP" ] || [ -z "$internalServerPemKey" ] || [ -z "$internalServerUser" ] || [ -z "$addCron" ] then printf "\033[1;31m" echo "Some or all of the parameters are empty"; helpFunction fi # Begin script in case all parameters are correct printf "\033[1;33m------------------------------------------------------------------Before ssh" echo -e "\033[0m" #reset color echo ".pem key of the server on which a new user has be created : $internalServerPemKey" echo "UserName of the server on which a new user has be created : $internalServerUser" echo "IP of the server on which a new user has be created : $internalServerIP" echo "Name of the cron to be added : $addCron" printf "\033[1;31mLogging into: "$internalServerPemKey" "$internalServerUser"@"$internalServerIP"\033[0m\n" ssh -i "$internalServerPemKey" "$internalServerUser"@"$internalServerIP" << HERE printf "\033[1;33m------------------------------------------------------------------After ssh" echo -e "\033[0m" #reset color #echo "Executing connect_prod_cron_new.sh" #sh connect_prod_cron_new.sh #sleep 2 echo "after ssh" echo "IP Of the Server:" hostname -I echo "Hostname of the Server:" hostname echo "Changing user to root" sudo su <><> EOF echo "User Switched To;" whoami printf "\033[1;33m------------------------------------------------------------------List of Cron Jobs Before Addition" echo -e "\033[0m" #reset color crontab -l | cat -n if [ -n "$addCron" ] then echo "Inside addCron" crontab -l >crontab.tmp printf '%s\n' "$addCron" >>crontab.tmp crontab crontab.tmp && rm -f crontab.tmp fi printf "\033[1;33m------------------------------------------------------------------Updated List of Cron Jobs" echo -e "\033[0m" #reset color crontab -l | cat -n printf "\033[1;31mExiting from ---> "$internalServerPemKey" "$internalServerUser"@"$internalServerIP"\033[0m\n" #echo "Existing user ---> $userName" EOF HERE
Before you create a new Cronjob, check if the EC2 instance has any existing Cronjobs
Login to the EC2 instance and check existing Cronjobs
ssh -i ~/Downloads/howtoforge-test.pem ubuntu@ec2-15-236-64-128.eu-west-3.compute.amazonaws.com
List the Cronjobs
crontab -l
Execute the Shell script to add a Cronjob
Go to your local Linux machine and add a Cronjob on Ubuntu 18.04 EC2 instance using the following command. This will create a Cronjob that will be triggered every minute and write the current date to a file. You can change the Cronjob as per your requirement.
./add-cronjob.sh -K ~/Downloads/howtoforge-test.pem -U ubuntu -I ec2-15-236-64-128.eu-west-3.compute.amazonaws.com -a "* * * * * /bin/date >> /tmp/cron_output"
Now, you can also go to the EC2 instance to check if the Cronjob has been added or not.
ssh -i ~/Downloads/howtoforge-test.pem ubuntu@ec2-15-236-64-128.eu-west-3.compute.amazonaws.com
sudo -i
crontab -l
cat /tmp/cron_output
In the following screenshot, you can see that the Cronjob has been added and executed every minute.
Create a shell script to remove Cronjobs
Now, if you think you need to remove the Cronjob you added, you can easily do it using the shell script available on my Github.
Create a new file on your local system with the following code.
Github Link: https://github.com/shivalkarrahul/DevOps/blob/master/aws/shell-scripts/aws-ec2-add-remove-cron-job/remove-cronjob.sh
File: remove-cronjob.sh
#!/bin/bash helpFunction() { echo "" printf "\033[1;32mUsage: $0 -K <internal.pem> -U <internal-user> -I <internal-ip> -l <yes/no>" echo "" echo -e "\t-K \".pem key of the server on which a cron job has to be removed\"" echo -e "\t-U UserName of the server on which a cron job has to be removed" echo -e "\t-I IP of the server on which a cron job has to be removed" echo -e "\t-l List the existing Cron Jobs, provide \"yes\" as a parameter. Get a list first and then specify job no which needs to be removed" echo -e "e.g." echo "Remove a new Cron Job" echo "./remove-cronjob.sh -K /Users/cloudcover/Documents/Rahul/access/rahuls.pem -U ubuntu -I ec2-52-47-90-247.eu-west-3.compute.amazonaws.com -l yes" echo -e "\033[0m" #reset color exit 1 # Exit script after printing help } while getopts "I:K:U:l:" opt do case "$opt" in K ) internalServerPemKey="$OPTARG" ;; U ) internalServerUser="$OPTARG" ;; I ) internalServerIP="$OPTARG" ;; l ) showListOfJobs="$OPTARG" ;; ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent esac done echo "******************" echo $listCronJobs # Print helpFunction in case parameters are empty if [ -z "$internalServerIP" ] || [ -z "$internalServerPemKey" ] || [ -z "$internalServerUser" ] || [ -z "$showListOfJobs" ] then printf "\033[1;31m" echo "Some or all of the parameters are empty"; helpFunction fi # Begin script in case all parameters are correct printf "\033[1;33m------------------------------------------------------------------Before ssh" echo -e "\033[0m" #reset color echo ".pem key of the server on which a new user has be created : $internalServerPemKey" echo "UserName of the server on which a new user has be created : $internalServerUser" echo "IP of the server on which a new user has be created : $internalServerIP" if [ $showListOfJobs == "yes" ] then printf "\033[1;31mLogging into: "$internalServerPemKey" "$internalServerUser"@"$internalServerIP"\033[0m\n" ssh -i "$internalServerPemKey" "$internalServerUser"@"$internalServerIP" << HERE printf "\033[1;33m------------------------------------------------------------------After ssh" echo -e "\033[0m" #reset color echo "after ssh" hostname -I hostname echo "Changing user to root" sudo su << EOF echo "User Switched To;" whoami printf "\033[1;33m------------------------------------------------------------------List of Cron Jobs Before Deletion" echo -e "\033[0m" #reset color crontab -l | cat -n printf "\033[1;31mExiting from ---> "$internalServerPemKey" "$internalServerUser"@"$internalServerIP"\033[0m\n" EOF HERE fi echo "Enter Cron Job Line Number to be removed" read lineNumber printf "\033[1;31mLogging into: "$internalServerPemKey" "$internalServerUser"@"$internalServerIP"\033[0m\n" ssh -i "$internalServerPemKey" "$internalServerUser"@"$internalServerIP" << HERE printf "\033[1;33m------------------------------------------------------------------After ssh" echo -e "\033[0m" #reset color echo "after ssh" hostname -I hostname #sleep 2 echo "Changing user to root" sudo su << EOF echo "User Switched To;" whoami printf "\033[1;33m------------------------------------------------------------------List of Cron Jobs Before Deletion" echo -e "\033[0m" #reset color crontab -l | cat -n crontab -l | sed -e "$lineNumber"d >crontab.tmp crontab crontab.tmp && rm -f crontab.tmp printf "\033[1;33m------------------------------------------------------------------Updated List of Cron Jobs" echo -e "\033[0m" #reset color crontab -l | cat -n printf "\033[1;31mExiting from ---> "$internalServerPemKey" "$internalServerUser"@"$internalServerIP"\033[0m\n" EOF HERE
Execute the Shell script to remove the Cronjob
To remove the Cronjobs, execute the shell script. It will list all the Cronjob available on your Ubuntu 18.04 EC2 instance. You can then select the job to be deleted, the script will do the deletion for you.
./remove-cronjob.sh -K ~/Downloads/howtoforge-test.pem -U ubuntu -I ec2-15-236-64-128.eu-west-3.compute.amazonaws.com -l yes
Now, you can execute the same script again to list the Cronjob in the EC2 instance.
./remove-cronjob.sh -K ~/Downloads/howtoforge-test.pem -U ubuntu -I ec2-15-236-64-128.eu-west-3.compute.amazonaws.com -l yes
You can also check the Cronjob from the EC2 instance itself.
ssh -i ~/Downloads/howtoforge-test.pem ubuntu@ec2-15-236-64-128.eu-west-3.compute.amazonaws.com
sudo -i
crontab -l
Conclusion
In this article, we saw Shell scripts to add and remove Cronjobs from Ubuntu EC2 instance. This will help to automate the manual task of adding or removing Cronjobs and also avoid potential human errors that can come due to manual operations.