In my last article I had shared the steps to downgrade rpm to a specific old version in Linux and Unix. How to update crond using a shell script? Setting up cron job using command line bash shell script in Linux? Create cron job automatically for root user and normal user using script in Linux. Schedule cron job via crontab using shell script with examples.

Create cron job or schedule jobs using bash scripts in Linux or Unix

 

Steps to create cron job manually

Step 1: Give crontab privilege

Before we start we need to give crontab privilege to the respective user. For the sake of this article I will create some sample cron job for root and deepak user, so I will assign permission for these two users.

Append the username to /etc/cron.allow

# cat /etc/cron.allow
root
deepak

 

Step 2: Create cron file

Create a cron file for root user

# touch /var/spool/cron/root
# /usr/bin/crontab /var/spool/cron/root

Create a cron file for deepak user

# touch /var/spool/cron/deepak
# /usr/bin/crontab /var/spool/cron/deepak

 

Step 3: Schedule your job

I will create some dummy jobs. To give a demonstration I will schedule a job to clear temporary files every midnight for both the user

# echo "0 0 * * * rm -f /tmp/root/*" >> /var/spool/cron/root

# echo "0 0 * * * rm -f /tmp/deepak/*" >> /var/spool/cron/deepak

 

Step 4: Validate the cron job content

Here you can use -u to define the username for which you wish to perform the cron action/

# crontab -u deepak -l
0 0 * * * rm -f /tmp/deepak/*

# crontab -u root -l
0 0 * * * rm -f /tmp/root/*

So our cron jobs are updated successfully for both root and deepak user.

NOTE:

You do not need to restart your crond service for the new changes.

 

Script to create cron job using bash shell script

Let us put them all together in a script

#!/bin/bash
   if [ `id -u` -ne 0 ]; then
      echo "This script can be executed only as root, Exiting.."
      exit 1
   fi

case "$1" in
   install|update)

	CRON_FILE="/var/spool/cron/root"

	if [ ! -f $CRON_FILE ]; then
	   echo "cron file for root doesnot exist, creating.."
	   touch $CRON_FILE
	   /usr/bin/crontab $CRON_FILE
	fi

	# Method 1
	grep -qi "cleanup_script" $CRON_FILE
	if [ $? != 0 ]; then
	   echo "Updating cron job for cleaning temporary files"
           /bin/echo "0 0 * * * rm -f /home/deepak/cleanup_script.sh" >> $CRON_FILE
	fi

	# Method 2
	grep -qi "cleanup_script" $CRON_FILE
	if [ $? != 0 ]; then
	   echo "Updating cron job for cleaning temporary files"
	   crontab -u deepak -l >/tmp/crontab
           /bin/echo "0 0 * * * rm -f /home/deepak/cleanup_script.sh" >> /tmp/crontab
	   crontab -u deepak /tmp/crontab
	fi

	;;
	
	*)
	
	echo "Usage: $0 {install|update}"
	exit 1
    ;;

esac

Here I have shared two methods to update cron job using a shell script for root and deepak user.

You can validate the changes after executing your script

# /tmp/script.sh install
Updating cron job for cleaning temporary files

 

List the cron jobs

# crontab -u root -l
0 0 * * * rm -f /home/deepak/cleanup_script.sh

# crontab -u deepak -l
0 0 * * * rm -f /home/deepak/cleanup_script.sh

So all is working as expected.

 

Lastly I hope the steps from the article to create cron job using shell script on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *