We have already covered – How to schedule tasks with cron in Ubuntu here. We recommend you to follow the mentioned article first because things we write here (i.e. special strings in cron) are an extension to that article. With the help of cron we can make particular commands to execute at a specific time and frequency. So, all we need to do is – edit the crontab file and make necessary entries and our system would execute those commands.
To edit crontab file, we use –
crontab -e
There are six fields for each entry –
* * * * * command
The first * (asterisk) is for the minute, next for hour, day of month, month of year, day of week and at the last, its the command itself – which we wish to execute.
Instead of using * (asterisk), we have special strings in cron which can be used directly.
special strings in cron
The special strings in cron are –
- @reboot,
- @yearly,
- @monthly,
- @weekly,
- @daily,
- @hourly.
So, in place of * (asterisk), we can use above mentioned special strings to get the command executed at a specific time. The syntax for special strings –
<special_strings> command
For instance, if we want to run a command or script at every reboot then use –
@reboot touch /dev/shm/test_file
The above would create a test_file using command touch in directory /dev/shm/ at every boot. We would like to add here that, if we use @reboot – it would execute the command only once at every boot.
Similarly, we can use –
@yearly – to run a command once a year,
@monthly – to run a command once a month,
@weekly – to run a command once a week,
@daily – to run a command once a day, and lastly we have
@hourly – to run a command hourly.
Using special strings only helps us save valuable time. It is not necessary that we have to use special strings all the time, we can go with * (asterisk) method as well. Choose the one we are most comfortable with.
In conclusion, we have discussed special strings in cron here.