Cron installation and first steps.
Cron? What is it…?
Well… Cron is a task scheduler. Its an application that runs as a daemon in the background and can run other applications / scripts / commands at the times scheduled by you. It can run chosen command every 5 minutes or exactly at 12:11 each day, or on every Thursday at 5:05 or right after a reboot and so on and so forth. It can automate some tedious and boring tasks for You.
So how do I get it?
Gain root and then run:
pacman -S cronie
Now You need to enable and start it:
systemctl enable cronie
systemctl start cronie
Now You can start adding tasks.
Adding tasks is easy but You need to know few things.
First of all: your user needs to have the permission to run the task. Cron uses the permissions of the user that the job was added by to run the tasks. What does it means? If You want to run something that needs root access You have to add this task to root’s cron task list. If You want your user john to run something – You add it to the john’s cron jobs list.
Use su command.
will log You in as john for this terminal session.
But how do I know if I am john?
and the terminal will tell You.
So… IF I want to add a root’s job…
You need to do this:
This will open text editor and will allow You to add the task.
Just remember that running stuff as root has its risks and that You should avoid doing it.
I have no idea how to syntax the cron task properly…
Ok – visit this site. It will generate the syntax for You BUT… here is another thing You need to know.
Well – if You want a task to be ran exactly at 12…
Yes yes all I have to do is add:
* 12 * * * /path/to/executable
Yes AND no…
This command will be ran at 12.
But it will also be run at 12:01 and 12:02 and 12:03… 12:59…
Awwww… But I only want it to run once…
Yes. You need to be more specific…
Oh… So I need to do it this way?
0 12 * * * /path/to/executable
That is correct. You need to tell cron to start it at exactly 12:00. By specifying hour only You’re telling cron to run this command on every minute of this hour.
Another thing that You may want to know is that there is more than one way to skin the cat…
Let me give You and example. I want to run an executable exactly every 6 hours. I can do it this way:
0 0,6,12,18 * * * /path/to/executable
OR I can do it this way:
0 */6 * * * /path/to/executable
That’s neat! But You said that I can also run stuff right after my machine boots up after a reboot. I don’t see how I can specify that value using the time / date.
That is correct. You would have to use @reboot option.
Oh… Cool! So if I want to run something right after cronie starts after reboot I have to add this:
Thats a a roger. You can also use things like:
3 0 * 1 1 /path/to/executable
So this will run the executable on every Monday of January exactly 3 minutes past midnight?
So You can use:
to add new or edit current cron jobs and You can also use commands like:
to list all the jobs for the user You are currently logged in as and
to remove all the crontab entries for the user You are currently logged in as.
Oh I think I got it! This cron thing is awesome. It can make life so much easier! Thanks Andy.
You’re welcome Andy.