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

cron, How To

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


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.

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

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

case "$1" in


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

	# 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/" >> $CRON_FILE

	# 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/" >> /tmp/crontab
	   crontab -u deepak /tmp/crontab

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


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/ install
Updating cron job for cleaning temporary files


List the cron jobs

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

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

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.

Views: 807
Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

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 send mail to

Thank You for your support!!

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

  1. I had to make a minor adjustment to the cron file location to get this working for me:


    Leaving it in the base /var/spool/cron/ directory always created the job under the root user.

  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.

    • 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)

  3. 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?


Leave a Comment