Amazing Linux Crontab Commands For SysAdmins

superior_hosting_service

Crontab
Crontab-in-Linux

Top 50 Linux Crontab Commands For SysAdmins


Linux offers some of the most reliable, consistent, and smart ways to control your system, be that personal or business. In fact, there are hundreds of system utilities explicitly designed for particular use cases. The crontab command is one such command that can make system managements much more productive. This is a handy software utility built-into most Unix systems and allows the sysadmin to create and edit their cron jobs. So, what are these cron jobs, and what’s their significance? Here’s a detailed guide that will introduce you with cron jobs and showcase 50 useful crontab examples.

Useful Linux Crontab Commands

This guide aims at introducing the crontab commands to beginning Linux users alongside starting sysadmins. You can learn the basics of crontab Linux commands and may keep this post bookmarked for future reference. If you’re a seasoned sysadmin, consulting the man page would be a better choice for you. Our editors have also provided some crucial information on cron and crontab in general.

Cron and Crontab: An Early Glimpse


So, cron is basically a job scheduler that can help automate many conventional but repeatedly used functionality. Say, a sysadmin needs to upgrade his system for newer packages every week. He might also want to clean unused packages every month. Now, these are simple yet repetitive tasks that the sysadmin may choose to automate. Cron allows us to do exactly this.

Cron works using the crontab file. It is a file that lists all the scheduled jobs and when to perform them. On a standard Linux machine, there are different crontab files based on user privileges. We’ll mostly base our examples on the default users’ crontab.

The crontab command in Linux maintains crontab files for individual users. So, crontab is the command that allows us to modify crontab files and cron executes them. The jobs inside our crontab file are known as cron jobs, and consist of standard Linux commands. They also contain the time-period when cron needs to execute them.

Crontab Command Structure Explained

Each entry in crontab stands for a specific job. These take the form of commands prefixed by the time of their execution. They have a rigorous syntax that needs to be maintained, or else your cron jobs might not perform what you want them to do. This syntax is shown below for further assessment.

The default user’s crontab entries have six distinct fields. The root user’s crontab entries contain one additional field for specifying for whom to run a particular cron job. Now, let us inspect what these fields determine and their purpose.

m h dom mon dow command

The first five fields above are used to pass information about when to run the commands specified in the six fields. The first field is used to denote at what minute cron should invoke the command. The second field indicates the hour (24-hour format), the third field represents the date of the month, the following field the month, and the next one the day of the week.

Don’t worry if you do not get them right now. Going over some useful commands will make you understand them much better. So, keep up with us and try these commands. For now, stick to your user session, no need to try them using root privileges.

Basic Linux Crontab Commands

Understanding the basic crontab commands will help you master the tool in the long term. Below, we discuss some very fundamental yet crucial commands that will enhance your productivity as a Linux sysadmin to a whole new level. Try them right now for gaining first-hand experience.

1. Edit Crontab

$ crontab -e

The above command is used for invoking your default crontab. You can now edit this file and insert your own jobs to run at a given time. By default, this crontab should contain some comments that will help you understand its functionality in greater detail.

2. List Crontab

You can use the following command to list the current crontab in use. This command just dumps the contents inside the crontab file in the standard output. It can come in handy when inspecting crontabs.

$ crontab -l

3. Edit Crontab for User

Say your system has a user named User and you want to edit the crontab configuration of this user. The next command demonstrates how to do this. The username is passed through the -u option. This is a handy command for sysadmins who need to check up on other users of a system.

$ crontab -u User -e

This command is analogous to the below command.

$ crontab -e User

4. List Crontab for User

The same structure is followed when listing the crontab of another user. All you need to do is replace the -e option with the -l option like in the second example.

$ crontab -u User -l

This command is synonymous to the following command.

$ crontab -l User

5. Verify Crontab File

Often you might need to verify whether a specific user has any crontabs of their own or not. The next command shows how you can do this.

$ sudo ls -l /var/spool/cron/crontabs

It should print out all of the available crontabs of each user in your system to the standard output.

6. Delete Your Crontab

If you want to terminate all pre-scheduled tasks, deleting the crontab is one option — the below command shows how this can be done for the currently logged in user.

$ crontab -r

Simply pass the -r(remove) flag to crontab for removing the crontab.

7. Delete the Crontab of User

To delete the crontab of User, follow the next command.

$ crontab -u User -r

The same can be done using the below command.

$ crontab -r User

8. Limit Crontab Access to Specified Users

Sysadmins might want to limit crontab access to some specified users only. To do this, we’ll need the cron.allow file. Run the below command to check whether it exists in your system or not.

$ ls /etc/cron.d/ | grep ".allow"

If this file exists, you can edit it as root and specify who can access crontab files in your system. Else, login as superuser and create this file yourself.

# vim /etc/cron.d/cron.allow

Enter the usernames for whom you want to enable crontab access. Don’t forget to list root at the top or you may lock yourself out from your own crontab.

9. Deny Crontab Access to Specified Users

You can deny crontab access to specific users very easily by utilizing the cron.deny file. Login as root and create/edit the cron.deny file.

# vim /etc/cron.d/cron.deny

List the usernames for whom you want to deny crontab access. These users won’t be able anymore to list or edit crontabs in your system.

Everyday Crontab Linux Commands for Beginners


Now we’ll show you some regularly used crontab commands that will make computing much more fun for you. These commands are generic so you can edit them quickly based on your needs. We suggest you play with them carefully, else you may do something you’ll end up regretting later.

10. Create a Backup of All User Accounts at 5 am Daily

0 5 * * * tar -zcf /var/backups/home.tgz /home/

Put the above entry in your crontab file using the command crontab -e and cron will now create a backup of every user account on your system at 5 am sharp every day. The 0 at the beginning denotes the first minute, and the 5 indicates the hour 5 am.

11. Create a Backup of All User Accounts at 5 am Per Week

0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

The above crontab command will create the backup file every week instead of every day. Notice, here how the last time value has been replaced with 1 instead of the *.

12. Schedule Cron to Execute a Job Twice a Day

0 5,17 * * * /scripts/script.sh

The above crontab command will make cron execute the script.sh executable at 5 AM and 5 PM daily. Notice how the comma has been used to denote multiple hour values. You can add more values using a comma-separated list to perform the task more than twice.

13. Schedule Cron to Execute a Job at 2 am Daily

0 2 * * * /bin/sh backup.sh

If you add the above entry in your crontab file, cron will execute the script backup.sh at 2 am every day. However, make sure the script has executable permission and sits in the specified directory.

14. Schedule Cron to Execute a Job at 3:15 am Daily

15 3 * * * /bin/sh script.sh

The above crontab entry will run the bash script called ‘script.sh’ at 3:15 am each day. This type of crontabs will be beneficial when scheduling jobs that need to be run every day.

15. Schedule Cron to Execute a Job at 8 pm Every Week

0 20 * * 1 /bin/sh script.sh

The above crontab command will make cron execute the script.sh file at 8 pm every week. The hour value needs to be specified in the 24-hour time format to specify pm values inside your crontabs.

16. Schedule Cron to Execute a Job at 8 pm on Monday

0 20 * * Mon /bin/sh script.sh

The above crontab command invokes the cronjob at 8 pm on every Monday. This command is the same as the next command.

0 20 * * 7 /bin/sh script.sh

So, 7 in the last time field denotes Monday.

17. Schedule a Cron Job at 8 pm on Monday, and Saturday

0 20 * * Mon,Sat /bin/sh script.sh

The above entry in your crontab will make cron execute the script.sh file at 8 pm in every Mondays and Saturdays. You can make this script run on any other day of the week by providing the day name using a comma-separated list.

However, you should always try to specify numerics value as many cron jobs will not work with the abbreviated values. The same command specified using the numeric value would be as shown below.

0 20 * * 1,6 /bin/sh script.sh

18. Schedule a Cron Job to Run on Every Minutes

Generally, you won’t require any cron job to run every minute. But this is an excellent example on how you can do this using the crontab command in Linux.

* * * * * /scripts/script.sh

All asterisks in the time field of your crontabs mean the script.sh file will run every minute. You should not try this on servers; else you may disrupt the system very quickly.

19. Schedule a Cron Job to Run on Every 10 Minutes

Sometimes you may want cron to run a job in every specified amount of time. The below command showcases how to execute a task every 10 minutes.

*/10 * * * * /scripts/script.sh

The above command will make cron run the script.sh executable in every 10th minute. The / operator is used for achieving these step values inside your crontab.

20. Schedule a Cron Job to Run on Every 15 Minutes on Sunday and Monday

The next command tells cron to run the specified job on every 15th minutes during Sunday and Monday. This command is useful for several reasons. If your system performs specific tasks in select days, this command may come in handy.

*/15 * * * Sun,Mon /scripts/script.sh

The same command can be written more pragmatically, as shown below.

*/15 * * * 7,1 /scripts/script.s

21. Schedule a Cron Job to Run on Specified Months

* * * jan,may,aug * /script/script.sh

The above crontab will make cron execute the given script at every minute in January, May, and August. As with weeks, the same command can be written using numeric values only as shown below.

* * * 1,5,8 * /script/script.sh

22. Schedule a Cron Job to Run on 15 January at 8 PM

0 20 15 1 * /script/script.sh

The real power of crontab is that it allows sysadmins to define very robust time-periods. The above command will execute the script.sh file on every 15 January at 8 pm sharp. The same command can be written as below.

0 20 15 Jan * /script/script.sh

23. Schedule a Cron Job to Run in Every Second Month

You can schedule a cron job to be run in every second month of the year using the crontab Linux command. Simply add the next line in your crontab file.

0 0 15 */2 * /script/script.sh

The above crontab entry tells cron to invoke the script.sh file on every 15th day of the month in every second month of the year. These type of commands are useful for updating the system or clean-ups.

24. Schedule a Cron Job to Run on the First Sunday of Each Month

It’s not possible to schedule a job that will run on the first Sunday of every month using the crontab time-period values. However, we can leverage the conditional section of the command portion to achieve this. Notice how the below command is implemented carefully.

0 2 * * sun [ $(date +%d) -le 07 ] && /script/script.sh

This entry will only invoke the script.sh file on the first Sunday of every month. It is analogous to the command shown below.

0 2 * * 7 [ $(date +%d) -le 07 ] && /script/script.sh

25. Schedule a Cron Job to Run on Every Three Hours

The below crontab entry invokes the cron job on every three hours interval.

0 */3 * * * /script/script.sh

Look how the * is prefixed before the /3. It tells cron to run the command in ‘every’ 3 hours

26. Schedule a Cron Job to Execute Twice on Every Saturday and Monday

Sysadmins often want to run specific commands more than one time in every week. The below crontab entry will make cron run a job twice on every Saturday and Monday.

0 8,20 * * 6,1 /scripts/script.sh

Cron will now run the script.sh at both 8 am and 8 pm on every Saturday and Monday. The above entry is equivalent to the next entry.

0 8,20 * * sat,mon /scripts/script.sh

27. Schedule a Cron Job to Run on Every 30 Seconds

It is not possible to specify a cron job to run in every 30 seconds or so using the time field parameter of the crontab. However, we can still do this using the following entries.

* * * * * /scripts/script.sh
* * * * * sleep 30; /scripts/script.sh

There are actually two different entries needed for this purpose. The first crontab entry tells cron to run the script at every minute. And the second entry makes cron run the next one after a 30-second pause.

28. Schedule Multiple Jobs in a Single Crontab Entry

Crontab allows admins to specify more than one jobs in a single entry. All you need to do is separate the tasks using the semicolon(;) delimiter. The below crontab Linux command invokes two scripts at 8 am each day.

0 8 * * * /scripts/script.sh; /scripts/scrit2.sh

29. Schedule Yearly Cron Jobs

Crontab allows users to schedule yearly cron jobs. It executes these jobs on the first minute of each year. These commands may come in handy to make system-wide changes or to send new-year greetings.

@yearly /scripts/script.sh

The above command is equivalent to the following commands.

0 0 1 1 * /scripts/script.sh
@annually /scripts/script.sh

30. Schedule Monthly Cron Jobs

It is also possible to specify monthly and weekly cron jobs using short forms. The syntax of a monthly cron job that executes the script system-upgrade.sh is shown below.

@monthly /scripts/system-upgrade.sh

The above command is equivalent to the next command.

0 0 1 * * /scripts/system-upgrade.sh

31. Schedule Weekly Cron Jobs

You may want to clean up your system for unused packages every week. Crontab allows users to specify weekly jobs easily using the @weekly identifier. The syntax is shown below.

@weekly /scripts/system-cleanup.sh

This command will invoke the script system-cleanup.sh in the first minute of the week. The notation is similar in meaning to the following command.

0 0 1 * mon /scripts/system-cleanup.sh

32. Schedule Daily Cron Jobs

Cron also allows users to use the short form @daily for specifying daily cron jobs. They are useful for the day to day maintenance of your system. Use them like shown below.

@daily /scripts/script.sh

This script will be run in the first minute of every day. The command is similar in action to the next command.

0 0 * * * /scripts/script.sh

33. Schedule Hourly Cron Jobs

Hourly cron jobs are useful for many tasks like blacklisting unwanted IPs, cleaning failed login attempts and such. The @hourly identifier can be used to specify cron jobs that need to be rn every hour. These crontab entries take the below form.

@hourly /scripts/script.sh

They are similar to the below command.

0 0 * * * /scripts/script.sh

34. Schedule a Cron Job on System Reboot

The crontab command allows admins to specify cron jobs that need to be run on system reboot. These jobs can range from changing path variables to autoloading custom configuration files. They can be done quickly using the @reboot identifier.

@reboot /scripts/script.sh

This crontab entry invokes the script.sh bash script after each system restart.

35. Send Cron Results to Specified Email Account

By default, cron sends the reports of scheduled cron jobs to the mail of the user who scheduled the job. You can redirect this by changing the mail variable’s value, as shown in the next example.

# crontab -l
MAIL=bob@admin.com
0 2 * * * /script/backup.sh

After the cron job backup.sh is executed, cron will send the reports to the mail address bob@admin.com.

Running Crontab Commands as Root


In Linux, many tasks require additional privileges such as sudo. However, to run sudo commands from a standard users’ crontab, users need to store their password in a plaintext file somewhere in their system. It is not a good practice, and such commands should be scheduled from the root user’s crontab instead. The root user’s crontab consists of one more entry between the time field and the command section. It is used for specifying the user for whom to run the jobs.

36. Clear all Faillog attempts at 1 am Everyday

0 1 * * * root echo " " > /var/log/faillog

The above command will clear every failed login attempts to your system at 1 am every day. You can simply change the values to run this command on any specific time instead of 1 am.

The same can be done using the below command. Here we are using dd instead of the echo command to clear the log data.

0 1 * * * root dd if=/dev/null > faillog

37. Save all System Logs at 2 am every 10 Days

The system log file provides useful insights on our Linux machine and is crucial for many sysadmins. Add the below crontab command in your crontab to save all system logs at 2 am in every ten days.

0 2 */10 * * echo " "> /var/log/syslog

The same can be done using the dd command in the crontab entry.

0 2 */10 * * dd if=/dev/null > /var/log/syslog

38. Check for and Download New System Packages

The below crontab command entry checks for new system packages at 12 am on every first day of the month.

0 12 1 * * root apt-get update

This command is handy for updating your system packages on a monthly basis.

39. Schedule System Upgrades as Cron Jobs

You can schedule cron to upgrade your system automatically. The crontab entry for this process is outlined below.

0 12 1 * * root apt-get -y upgrade

The -y flag is necessary; otherwise, the process will hang waiting for your manual acceptance.

40. Update Package List and Upgrade the System

You can combine the above two commands for updating your packages and upgrading them to newer versions using the below-mentioned crontab entry.

0 12 1 * * root apt-get update && apt-get -y upgrade

The above crontab Linux command will update your package list and upgrade to newer packages if available at 12 am on the first day of every month.

41. Schedule a Cron Job for Removing Unnecessary Dependencies

Linux systems often hoard a lot of dependencies not required by the system anymore. Manually deleting these dependencies can become a cumbersome task for many sysadmins. You can use cron to automate this process for you. The next crontab entry detects and removes all dependencies that are no longer required by your system automatically.

0 1 1 * * root apt-get -y autoremove

The above command clears all unused dependency at 1 am on the first day of every month. The -y flag is mandatory or else the process will hang waiting for user confirmation.

42. Schedule a Cron Job for Clearing out the Local Repository

You can use cron for automatically clearing out the local repository of retrieved package files. All you need to do for this is add the next entry in your system-wide crontab.

0 2 1 * * root apt-get clean

The above entry schedules a repository cleaning for your packages at 2 am on the first day of every month.

43. Schedule a Cron Job for Cleaning Caches

Caches are used for providing fast access to services. However, they can get enormous with time and requires admins to clear them periodically. The below crontab entry shows how to schedule a cron job for cleaning the caches PageCache, dentries, and inodes.

0 3 1 * * root sync; echo 3 > /proc/sys/vm/drop_caches

The above entry clears all data in PageCache, dentries, and inodes at 3 am on the first day of every month. However, we suggest you do not add this inside your crontab unless you know precisely what this does to your system.

Miscellaneous Crontab Commands


Since Linux crontab commands can be quite diverse, we’re going to list out some essential commands that have a considerable impact on system administration. They can often lead to the effective automation of your system and in turn, increase your productivity as a sysadmin to a whole new level.

44. Install Custom Crontab for Your User

Apart from the default crontab, users may install their custom crontab file. How this can be done is shown below.

$ crontab -a filename

This command installs the ‘filename’ document as your crontab. In many systems, the -a flag is not required.

45. Backup All Cron Jobs to Plain Text File

Often sysadmins want to store a backup of their crontab entries for future reference. This can be done in several ways. The below command shows how to keep a backup of all cron jobs in a plaintext file called cron-backup.txt.

$ crontab -l > cron-backup.txt

This command will save the crontab entries for only the current user. You need to be root to save all system-wide cron jobs.

46. Restore Cron Jobs from Backup File

In case you deleted the current crontab, you can restore it using the backup file you created using the above command. The syntax to this command is shown below.

$ crontab cron-backup.txt

You should notice that all this command does is, essentially installs the cron-backup.txt file as your new crontab.

47. Change the Mail Address for Cron Reports

You can change the mail address at which cron sends our job-specific reports by adding the MAILTO variable inside your crontab. This is demonstrated below.

#crontab -e
MAILTO=root@example.com
#add the above line inside your crontab

After adding this line, cron will now send reports on scheduled jobs to root@example.com.

48. Change the Path Variable’s Value

You can change or add the path variables’ value directly from inside your crontab using the PATH variable. This is shown below.

#crontab -e
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
#add the above line inside your crontab

Now, cron will treat the above-mentioned directories as your path.

49. Check Crontab Manual

$ man crontab

The above command prints out the manual page for the crontab command. If you want to learn crontab in details or looking for a quick fix to some problem, check out the man page should definitely be your first priority.

50. Check Cron Manual

The cron manual page provides cron-specific information. It is a handy tool for users who want to master cron effectively. You can consult the man page of cron by simply using the below command.

$ man cron

Crontab Linux offers a flexible mean for automating many day-to-day system administration jobs and allows admins to manage their system much more conveniently. People often think crontab command is out of their scope due to an abundance of possible commands. However, they are quite simple once you understand a handful of simple commands. Our editors have tried to outline some of the most used crontab commands that can make Linux administration fun for new users and help to gain experience with crontab in the process.