Recently, when I was working on my research I had to check whether a particular file was updated the way I wanted. One way of doing this was to sit by my computer and continuously check the file whenever it updated. This file was created from a continuous process that can run for days until was killed. Obviously I did not want to sit by my computer for days looking at that file. Fortunately there is another way to do this in Linux systems. Linux has a utility called 'cron' to do this task.
In my case, I created a small shell script to copy the file in question to a different location with a different file name. Every time I run that script, it creates a copy of the file I want to a different location with a new file name. Now I had to set up a way to run the script every few minutes automatically, so I can come back later and check the new set of files.
If you are looking for a way to do a similar task, you also can use cron utility in Linux.
To do this, first you have to open up the cron utility. Go to the terminal and type,
Now, to schedule a task you have to follow the format as follows,
MIN - Minutes, can have values from 0 - 59
HOUR - Hours, can have values from 0 - 24
DOM - Day of the month, can have values from 1 - 31
MON - Month, can have values from 1 - 12, 1 being January
DOW - Day of the week, can have values from 0 - 6, 0 being Sunday
CMD - The command you want to execute
So Let's say I want to execute a script in my 'Documents' directory every 30th minute, every hour, everyday. In that case I can use the following line in my cron
30 * * * * /home/usr/documents/script_name.s
asterisk(*) is used to represent all the values in a particular field. Which means my "script_name.s" file will be executed every 30th minute of every hour, every day, every month.
Let's look at another example. Let's say I want to run my script every weekday at 6.00 p.m,
0 18 * * 1-5 /home/usr/documents/script_name.s
what if want to run my script every 5 minutes
*/5 * * * * /home/usr/documents/script_name.s
You are not limited to run scripts with cron. You can execute any command you want. For example, when I first found out about cron, I used the echo command to write a line into a file every minute just to check if I was doing it correctly.
*/1 * * * * echo "testing cron">>/home/usr/documents/cron_test.txt
These examples will give you an idea on different ways you can use cron utility to schedule your tasks. As you can see, you have lot of flexibility in scheduling tasks using cron. Once you type your cron command, just save the cron file and you're done.
You can schedule multiple tasks using cron and if you want to comment out any line from the cron file, you can use # symbol to do so.