Cron and Crontab
Hey guys, welcome back. Like mentioned in my last post I recently started my own server. After a while I recently started with Cron. Cron is a package to schedule your tasks for you. Today I’m going to show you how to work with Cron.
Cron has created a few folders in /etc, cron.hourly, cron.daily, cron.weekly cron.monthly. Inside all of them are bash script, that will be run once every time the folder name mensions. Ain’t that hard to follow right?
The main file for you is the crontab file, located at /etc/crontab. The file doesn’t look really easy to handle but really it is. In the crontab you see the folders hourly, weekly and so on.
example: (thanks wikipedia!)
.---------------- minute (0 - 59) | .------------- hour (0 - 23) | | .---------- day of month (1 - 31) | | | .------- month (1 - 12) OR jan,feb,mar,apr ... | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat | | | | | * * * * * command to be executed
17 * * * * root cd / && run-parts –report /etc/cron.hourly
the above line will run everything in de folder /etc/cron.hourly at the 17th minute of the hour and will run this as root.
I needed cron to create a kind of mirror so it will copy the files in my homedir to another server. Since I am doing this trough ssh I can do this trough root since this isn’t permitted on my servers. Besides that I would like to do this with my own user and not with root.
I created a little bash script and placed it in my homedir, chmoded it with +x so it will be allowed to be run by everyone. Now I added the following line to the crontab file:
0 10 * * * junke /home/junke/cron/rsync-homedir
this will be run ones a day at 10 am / 10:00 and will be run by my own user. There are more ways to accomplish my goal but since I wanted to learn more about cron I did it this way.
There will follow more posts about Bash and RSync later on.