how to schedule a cron job to run a script on Ubuntu 16.04

To schedule a cron job to run a script on Ubuntu 16.04

Crontab allows Linux and Unix users to run commands or scripts at a given date and time. You can schedule scripts to be executed periodically. Crontab files can be used for system maintenance, backup, and other repetitive tasks. In this tutorial, we will show you how to schedule a job to create a file using crontab.

Schedule process

We assume you have crontab installed on your machine. To list the cron job installed on your machine run the following command

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

Assign a cron job 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/ed
  2. /bin/nano        < ---- easiest
  3. /usr/bin/vim.athena
  4. /usr/bin/vim.athena-py2
  5. /usr/bin/vim.basic
  6. /usr/bin/vim.gnome
  7. /usr/bin/vim.gnome-py2
  8. /usr/bin/vim.gtk
  9. /usr/bin/vim.gtk-py2
  10. /usr/bin/vim.gtk3
  11. /usr/bin/vim.gtk3-py2
  12. /usr/bin/vim.nox
  13. /usr/bin/vim.nox-py2
  14. /usr/bin/vim.tiny

Choose 1-14 [2]:

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 20.23PM

23 20 * * * /bin/sh /home/user/Desktop/abc.sh 

Save and quit the file. Then create an abc.sh file on the desktop location.

root@linuxhelp:~# cd /home/user/Desktop/
root@linuxhelp:~/Desktop# vim abc.sh
touch /home/user/Desktop/file.txt

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

root@linuxhelp:~/Desktop# chmod u+x abc.sh                                  

The below image shows the clock at 8.22 PM waiting for the file to get created.

At 8.23 a file named file.txt has been created on the Desktop as we scheduled.

So This is How you run a script on scheduled time using crontab. with this, the method to schedule a cron job to run a script on Ubuntu 16.04 comes to an end.

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 create schedule job in crontab?
A
For creating the schedule job using the crontab, use the following syntax for that,
"*/3 * * * * "
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 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
How to schedule the job using the crontab privileged under 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"