Hello!
This is Alex Gorzen from Liquid Web with our video on how to create a Cron Task in Ubuntu 16.04.
Cron jobs are a great time saving tool to help you execute repetitive tasks on a set schedule.
They can be used for setting up backups, updating inventory, or run whatever other task or program you configure it for.
In this video, I will walk through how to set up a cron and make a quick script to back up data.
So first, log in to Ubuntu 16 as the user you want the cron to run under.
If you do not need root access, then using the user with the proper permissions would be best.
I am under my lwtest user.
The next step would be to make sure your system is up to date, so I will run:
sudo apt-get update
Then
sudo apt-get upgrade
Just to make sure everything I am running is properly updated so I do not get any surprises later.
Next, we can quickly check to make sure the cron package is installed with:
dpkg -l cron
So I see it listed here which should be the case with most systems.
If it is not installed, you can install it with:
sudo apt-get install cron
I have it installed so that did not do anything.
So next I will check to ensure the cron service is running using:
systemctl status cron
Here it shows my service is inactive. This is because I stopped it a few seconds ago.
To start it up, run:
sudo systemctl start cron
Then checking the service again, it now shows that it is active, so the crons will run on schedule.
To set up the cron, make sure the user you are logged in as can perform all the actions
you need, as the cron will only have the same access and permissions as that user.
There are multiple ways to add crons, but a quick and easy way is to use crontab, so if I run:
crontab -e
It first asks which text editor I would like to use.
Use what you are familiar with, but I am familiar with vim, so I will type 3 and press enter.
Now in this file it shows some information on how to set up a cron, all these lines are
comments since they start with the number sign.
To keep things clean I will press the down key go to the bottom of the file and then
press O to add a new line and enter Insert Mode where I can start typing.
I will start with a quick example here and then explain what it means.
0 4 * * * /bin/sh /scripts/backup-script.sh
So this might look confusing at first, but let me
help break it down so it is easier to understand.
The line starts with the schedule for when the cron will be run.
First comes the minute the cron would run at, then the hour of the day,
then day of the month, the months it runs, then the day of the week, then finally after that
would be the actual command you want it to run.
My example starts with a 0 so that means it will run at the start of the hour, and since
there is a 4 in the next column, then it would only run at 4:00am.
Since the next three are all asterisks, that means it will run every day of the month,
every month of the year, and every day of the week.
You can put multiple numbers in a column, separated by a comma, to have it run at any
of those instances, like if I change 4 to 4, 5, 20 then it will run at 4am, 5am, and 8pm.
If I wanted it to run every 2 hours, I can change the hour column to */2.
So it would run at midnight, 2am, 4am, 6am, and so on all day long every day.
So depending on your needs, this can offer a lot of flexibility in when the server runs the command.
I will change it one more time so it runs every 5 minutes just so we do not have to
wait long when it is being tested.
Now I press Escape, colon, w, q then press enter to save the crontab and exit vim.
I set up the cron, but it will not actually do anything unless the command I run actually works.
I will create a simple bash script in the /scripts/backup-script.sh file that the cron is calling.
This will copy files from /var/www/html/ and zip them up into a file in the /backup named lwtest_backup.zip.
This is just a quick and easy example which will overwrite the same file each time it runs,
but whatever code you add there, it will try to run it.
Now I will save and back out of the file, and to make sure the script actually runs,
I have to give it execute permissions with:
chmod +x /scripts/backup-script.sh
Now It will run the script manually to make sure it works, and I see the zip file is created.
Now waiting for the cron to run and checking the file again after the next 5 minute point
I see the timestamp was updated without me having to manually run that backup command.
I hope you enjoyed our video on creating cronjobs.
For more how-tos and tutorials, visit our knowledge base at https://liquidweb.com/kb/
or our YouTube channel for more videos. Thank you for watching!
https://www.youtube.com/user/LiquidWebInc