The cron system is basically a smart alarm clock. When the alarm sounds, Linux runs the commands of your choice automatically. You can set the alarm clock to run at all sorts of regular time intervals.
Linux installs the cron daemon (crond) by default. It's configured to check the /var/spool/cron directory for jobs by user. It also checks for scheduled jobs for the computer under /etc/ crontab and in the /etc/cron.d directory.
Login form root and check system date, and run crontab command to schedule job
In open file you can schedule job. There are 6 field in this file 5 for time and one for commands.
|hour||Based on a 24-hour clock; for example, 23 = 11 P.M.|
|day of month||1–31|
|month||1–12, or jan, feb, mar, etc.|
|day of week||0–7; where 0 and 7 are both Sunday; or sun, mon, tue, etc.|
|command||The command you want to run|
If you see an asterisk in any column, cron runs that command for all possible values of that column. For example, an * in the minute field means that the command is run every minute during the specified hour(s). Consider another example, as shown here:
11 5 3 5 * ls
This line runs the ls command every May 3 at 5:11 A.M. The asterisk in the day of week column simply means that it does not matter what day of the week it is; crontab still runs the ls command at the specified time.
For example time in my system is 21:46 and date is 14 Feb Sunday. ( See image above). Now I will set cron to display the output of ls commands on tty2 at 21:50
save file and quit
In real life you do not have to restart cron every time you make a change because cron always checks for changes, But so far exams concern we suggest you to restart cron whenever you made change.
Wait for four minute and on 21:50 you will get the output of ls command on tty2
Setting Up cron for Users
Each user can use the crontab command to create and manage cron jobs for their own accounts. There are four switches associated with the crontab command:
- -u user Allows the root user to edit the crontab of another specific user.
- -l Lists the current entries in the crontab file.
- -r Removes cron entries.
- -e Edits an existing crontab entry. By default, crontab uses vi.
If you want to set up cron entries on your own account, start with the crontab -e command.
Securing cron daemon
You may not want everyone to be able to run a job in the middle of the night. If your system have important security data, someone may download important data or worse, and it could be done before you discover the security violations.
Two files are used to control the behavior of crond daemons
- /etc/cron.allow If present then only users those name are in this file can use crond daemons
- /etc/cron.deny If present then only user those name are in this file will not be able to use crond daemons apart from these user all other can use cron daemons
- If both files are not present then only root can access cron daemons
For example create two user Vinita and nikita
These files are formatted as one line per user; add user nikita to cron.allow
To test login on other terminal from user nikita and schedule job from cron commands
Now login on other terminal from vinita and schedule job form cron commands