1. List Crontab Entries
List or
manage the task with crontab command with -l
option for current user.
# crontab -l
00 10 * * * /bin/ls >/ls.txt
2. Edit Crontab Entries
To edit
crontab entry, use -e option as shown
below. In the below example will open schedule jobs in VI editor. Make a
necessary changes and quit pressing :wq keys which saves the setting
automatically.
# crontab -e
3. List Scheduled Cron Jobs
To list
scheduled jobs of a particular user called tecmint using option as -u (User) and -l
(List).
# crontab -u tecmint -l
no crontab for tecmint
Note: Only root
user have complete privileges to see other users crontab entry. Normal user
can’t view it others.
4. Remove Crontab Entry
Caution: Crontab with -r parameter will remove complete scheduled jobs
without confirmation from crontab. Use -i
option before deleting user’s crontab.
# crontab -r
5. Prompt Before Deleting Crontab
crontab
with -i option will prompt you
confirmation from user before deleting user’s crontab.
# crontab -i -r
crontab: really delete root's crontab?
6. Allowed special character (*, -, /, ?, #)
- Asterik(*) – Match all values in the
field or any possible value.
- Hyphen(-) – To define range.
- Slash (/) – 1st field /10 meaning
every ten minute or increment of range.
- Comma (,) – To separate items.
7. System Wide Cron Schedule
System
administrator can use predefine cron directory as shown below.
- /etc/cron.d
- /etc/cron.daily
- /etc/cron.hourly
- /etc/cron.monthly
- /etc/cron.weekly
8. Schedule a Jobs for Specific Time
The below
jobs delete empty files and directory from /tmp at 12:30 am
daily. You need to mention user name to perform crontab command. In below
example root user is performing cron job.
# crontab -e
30 0 * * *
root find /tmp -type f -empty
-delete
9. Special Strings for Common Schedule
Strings
|
Meanings
|
@reboot
|
Command
will run when the system reboot.
|
@daily
|
Once
per day or may use @midnight.
|
@weekly
|
Once
per week.
|
@yearly
|
Once
per year. we can use @annually keyword also.
|
Need to
replace five fields of cron command with keyword if you want to use the same.
10. Multiple Commands with Double amper-sand(&&)
In below
example command1 and command2 run daily.
# crontab -e
@daily <command1> && <command2>
11. Disable Email Notification.
By
default cron send mail to user account executing cronjob. If you want to
disable it add your cron job similar to below example. Using >/dev/null
2>&1 option at the end of the file will redirect all the output of
the cron results under /dev/null.
[root@tecmint ~]# crontab -e
* * * * * >/dev/null 2>&1
conclusion: Automation of tasks may help us
to perform our task better ways, error free and efficiently. You may refer
manual page of crontab for more information typing ‘man crontab‘ command
in your terminal.