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

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.

Advertisement

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.

Advertisement
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.

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

4 thoughts on “Create cron job or schedule jobs using bash scripts in Linux or Unix”

  1. Hi , Thansk for this info. My python script is working fine while running manually. But it is not working properly though crontab. Means it stuck at halfway of execution.
    Any suggetion?

    Reply
  2. Hi,
    Very useful article, I have different query. Currently I directly ssh and edit the cronjobs using `crontab -e` there are 10-15 different jobs over there. I want to manage these jobs via git because there are other team users who also has access and don't want to mess up with jobs. So can you suggest what is the best way to manage jobs via git and get auto add/updated in crontab.

    Reply
    • I think the shell script I shared here does exactly that, it checks for existing jobs and adds new one (as many as you want)

      Reply

Leave a Comment