Use cronjob to execute task only once

0
5682
Cronjob

As you know, cronjob is used often to schedule repetitive tasks on systems; however, you can make use of its time configuration to execute a task only once.

To achieve this, let move back to the crontab format: [minute] [hour] [date] [month] [day]

Usually, we repeat task by providing * to the appropriate field. But if you provide a very specific future date time, it will execute just only once; well, in facts, it is just once per year.

For example, if I want to truncate a log file automatically on next Friday 12 Jan at 2am, I will write this crontab entry:


0 2 12 1 * cat /dev/null > /home/myuser/logs/apps.log

Here what happens:
– At the current time of this writing, which is 2.55pm Thu 11/01/2018, if I add above entry to crontab; it will execute that task.
– But if I add above entry after 2am on 12/01/2018, it will execute next time at 2am 12/01/2019.

So the fact is that, the config is to make task execute once per year, and we make use of it just to trigger only once. When we finish with it, the crontab entry should be removed to avoid execution in following year.