Cron is a utility that allows you to automatically run commands or scripts in the background at specific times by use of the cron daemon. You set what you want to run and when you want it to run by editing the crontab file. Use the following command to edit your crontab file.
The first time you run this, you’ll likely see the following…
no crontab for sudofry – using an empty one
Select an editor. To change later, run '
select-editor'
.
1. /bin/nano <---- easiest
2. /usr/bin/vim.tiny
3. /bin/ed
Choose 1-3 [1]:
I like nano, but you can use whatever you want. If you are also using nano, when you are finished editing the file, press CTRL + o and then press Enter to save the file. CTRL + x will close the file.
If you have never edited your crontab file before, you will see a bunch of comments at the beginning of the file. Just scroll to the bottom and you can edit your settings there. The comments can be removed if desired.
Basically, you set the times you want your command or script to run and then type the command or put the path to the script directly behind it. You can add as many of these cron jobs as you like, just use a new line for each cron job you create.
This is how it breaks down.
The * represent the following in order from left to right.
– Minute (0 – 59)
– Hour (0 – 23)
– Day of the month (1 – 31)
– Month (1 – 12)
– Day of the week (0 – 6) (Starting with Sunday)
Here are some examples. Adding this line to the bottom of your crontab file will execute your command or script at midnight, every day. The minutes are set to 0. The hour is set to 0. The other three are set to *, which means that all legal values count. (Every day of the week and every day of the month, of every month.)
This next line will execute your command or script every weekday at 5:30pm. The minutes are set to 30, the hour is set to 17 (which would be 5pm), every day of the month, of every month, on days 1-5 (Mon-Fri).
Let’s say you only want specific weekdays, just separate them with a comma. (Mon,Wed,Fri)
If you want to run a script every 10 minutes, you could use a forward slash like so.
If you would like to run a program with a graphical user interface (GUI), add export DISPLAY=:0 && in front of the command.
You can also overwrite your crontab file with a custom file you already made by typing crontab followed by the path to your file. This will wipe your crontab file and replace it with the contents of your new file. This is great for updating your crontab file from a script.