AMP AMP

How to schedule a CRON job to run a scripts on Ubuntu 20.4.1

To Schedule a CRON Job to Run a Scripts on Ubuntu 20.4.1

introduction:

The Cron is a Scheduling daemon that executes tasks at specified intervals. It reads the crontab (cron tables) for predefined commands and scripts.

Installation process:

Run lsb_release command to check the installed version of OS as follows.

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.1 LTS
Release:	20.04
Codename:	focal

We assume that the crontab was installed on your machine. To view the installed cron job on your machine run the following command

root@linuxhelp:~# crontab -l
no crontab for root

Assign a cron job and run the following command.

root@linuxhelp:~# crontab -e
no crontab for root - using an empty one
Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed
    Choose 1-4 [1]: 1
		
15 19 * * * /bin/sh  /home/Netaxis/Desktop/abc.sh
		

On Ubuntu based system it will ask for the editor for the first time to choose one or by default it uses the nano editor. Add the following lines, this will execute a “ abc.sh” script at 19:15PM Save and quit the file. Then create an abc.sh file on the desktop location.

root@linuxhelp:~# cd /home/Netaxis/Desktop/
root@linuxhelp:~/Desktop#  vim abc.sh

The below lines will create a file named file.txt , do not forget to the make the file executable.

root@linuxhelp:~# touch /home/Netaxis/Desktop/file.txt
root@linuxhelp:~/Desktop#  chmod u+x abc.sh

with this method scheduling a CRON job to run a scripts on Ubuntu 20.4.1 comes to end.

Tag : cron Linux Ubuntu
FAQ
Q
How to redirect the output of the cron job into the specified file in Linux?
A
Use the following syntax to redirect the output of the cron job into the specified file in Linux. For syntax: "*/5 * * * * su admpinster -c /opt/cron.sh > /file/path"
Q
How to schedule the job using the crontab privileged by the specific user?
A
Here I have given the example executing the scheduled job using the crontab privileged under by the specific user "*/5 * * * * su username -c /opt/file.sh"
Q
How to run the schedule using the crontab while rebooting the system?
A
For executing the scheduled job using the crontab while rebooting the system, use the following syntax as follow, "@reboot /root/reboot.sh"
Q
What is the minimum execution interval for cronjobs?
A
Every cronjob can be executed up to 60 times an hour, i.e. every minute.
Q
How to create a scheduled job in crontab?
A
To create a scheduled job in crontab use "*/3 * * * * "