Top Interesting cron Jobs To Run On Linux

sohail
sohail

Table of Contents

​A cron job is a scheduling service on Linux/Unix operating systems. These are simply a set of commands that are run periodically to do various tasks mostly used by system administrators and other users to do mundane tasks and procedures automatically with no user interaction. One can setup commands or scripts which run repeatedly at a prescribed time.   ​Cron is one of the most important and useful tools in Linux systems.

The cron service (daemon) runs in the background and constantly checks the –

1) /etc/crontab file
2) /etc/cron.*/ directories
3) /var/spool/cron/ directory

If it finds commands listed here it executes them for you.  

Command Structure

​Crontab file consists of command per line and has six fields actually and separated either of space or tab. The beginning five fields represent time to run tasks and the last field is for command. Minute (hold values between 0-59)

Hour (hold values between 0-23)
Day of Month (hold values between 1-31)
The month of the year (hold values between 1-12 or Jan-Dec, you can use the first three letters of each month’s name i.e Jan or Jun.)
Day of week (hold values between 0-6 or Sun-Sat, Here also you can use the first three letters of each day’s name i.e Sun or Wed.)  

Some Interesting Tasks System Administrators Run That Can Be Automated Using Cron

A system administrator who does everything manually wastes not only her time but yours as well. Tasks that a system admin performs repeatedly should be automated. Automation through scripting, specialized software and system scheduling frees her time, saves you money and prevents human error-related mistakes. These 10 sysadmin tasks are prime targets for automation and will help streamline your daily operations.  

1. Malware Scans

You can scan for spyware, malware, viruses and other nasties using automated processes. Using scripts, you can map or mount drives, scan your filesystems, disconnect when finished with the scan, scrape the scan log for positive hits, and send the results to a database or in an email. You don’t need to manually perform these scans when your system is perfectly happy and suited to do so on its own.  

2. Performance Monitoring

Taking an occasional performance snapshot is a good method for a single point-in-time glance at system performance. That singular peak is only a pixel in the entire performance picture. You need something with more depth and breadth that will provide you with performance trends and predictive peaks and valleys. Setting up such a system is easy with Orca. Orca compiles performance data from disparate sources (UNIX, Windows, Linux) and creates easy-to-read performance graphs. Gathering of data, calculations, graph generation and display are all part of the automated system.  

3. Code Promotion

​How you promote code from test to staging and into production can have a profound effect on marketing campaigns and other time-specific events. Moving the code from one environment to another manually is cumbersome, error-prone and requires coordination between developers and sysadmins. Enable your developers to promote code from one environment to another using an automated code deployment system. Some sys admins use RSYNC for automated code deployment and it’s safe to use if coupled with SSH keys to secure the transfers between hosts.  

4. Security Sweeps

You should perform regular, automated security sweeps on your entire network to expose and fix any wire-borne vulnerabilities. The frequency and intensity of the scans depend on the complexity of your network. Through scripting magic, you can set up scheduled scans, send the output to a database, extract a post-scan report from the database, and email it to yourself or create an HTML version of the report suitable for online viewing. One such tool, available for every modern operating system, is Nmap. Nmap is a free network security scanner designed to rapidly scan large networks and report vulnerabilities.  

5. Reboot

​Yes, you can automate system restarts. Sitting around waiting for systems to bounce back to life is a waste of time. Automate the process during low-use hours. Don’t worry, your automated monitoring system will notify you if the system doesn’t come back online within a reasonable amount of time.  

6. User and Group Maintenance

​You’ve probably used Active Directory, LDAP, NIS+ or other user and group account management software, but have you ever used one that really made you happy? The reason you haven’t is that there’s not a lot of automation built into them. Sure, you can create a user account, remove a user account, and create groups and manage groups, but when it comes down to real management, you probably haven’t found the right tool. The one you want might have to be the one you create yourself via scripts.In UNIX, it’s simple to create scripts to prompt you for the accounts that you wish to remove, have the system copy the user’s files to a new location, change the permissions, search all systems for any files owned by that user, change permissions on those files, or move them and complete the process by removing the user account from the directory service. Check out some of the add-on modules for your user management tool of choice. Microsoft, for example, offers its Active Directory Resource Kit Book and CD that includes utilities for automation scripting.

7. Disk Usage Scans

​There is a constant turf war raging between users and sysadmins, and it is one that the sysadmin must ultimately win. To that end, the sysadmin has some tools to employ: disk space quotas, disk partitions, and disk space scans. Scans are regular audits of disk space usage by the user. Offenders usually receive a warning or two before personal contact from a sysadmin. Typical remedies for disk space gluttons are temporary account suspension, removal of files, moving the files to a new location or an extension of the user’s space quota. These automated scans, when performed regularly (about once per week), prevent harsh actions by the sysadmin and keep users apprised of their disk use.  

8. Patching

​Automation through scripting, specialized software and system scheduling frees up a sys admin’s time, saves you money and prevents human error-related mistakes. These 10 repetitive processes are prime candidates for automation.

The only time manual patching is called for is when that stubborn minority of systems will not take patches by automated means. Linux and Windows include tools to perform automated updates, but if you’d like more control of which patches your systems receive and when they receive them, investigate HP’s Data Center Automation Center (HPDCAC) software (formerly Opsware). Much more than just an automated patching application, HPDCAC moves managing a complex infrastructure into a single, simple interface. If you’re looking for a “patch only” solution, check out Ecora’s Patch Manager for agentless patch management.  

9. File Transfers

​Using command-line scripting power (Windows, UNIX, and Linux), you can perform automated file transfers between hosts. There’s no need to do them interactively. If you’re clever in your timing, you can set up elaborate automated schemes that not only transfer your files but also unzip, change permissions, move, copy and insert information into a database. Use the secure versions of your file transfer utilities (e.g., SSH, SFTP, SCP) to ensure that anyone snooping doesn’t grab an important password from your network stream.  

10. High-Level Administration

​You can perform those housekeeping duties, service restarts and maintenance notices through automation. Set up your scripts to fire during low-use hours for clearing temporary file dumps, restarting your favorite services and sending out any maintenance or downtime notices via email. You’ll find that automating these tasks takes some of the pressure off of you to remember which day it is and which list of things you need to do. There’s no reason to keep a calendar of these; let the system handle them.

crontab examples –   crontab -e
Edit your crontabcrontab -l
Display (“list”) the contents of your crontabcrontab -r
Remove your crontab, effectively un-scheduling all crontab jobs sudo crontab -u john -e
Edit the crontab of the user named charles. The -u option requires administrator privileges, so the command is executed using sudosudo crontab -u patrick -l
View the crontab of user jeffsudo crontab -u monica -r
Remove the crontab of user monica.
Examples Of crontab Entries
15 6 2 1 * /home/melissa/backup.sh
Run the shell script /home/melissa/backup.sh on January 2 at 6:15 A.M.15 06 02 Jan * /home/melissa/backup.shSame as the above entry. Zeroes can be added at the beginning of a number for legibility, without changing their value.

0 9-18 * * * /home/carl/hourly-archive.sh
Run /home/carl/hourly-archive.sh every hour, on the hour, from 9 A.M. through 6 P.M., every day.

0 9,18 * * Mon /home/wendy/script.sh

Run /home/wendy/script.sh every Monday, at 9 A.M. and 6 P.M.

30 22 * * Mon,Tue,Wed,Thu,Fri /usr/local/bin/backup

Run /usr/local/bin/backup at 10:30 P.M., every weekday.

Regardless of your environment, you will find that some tasks are common to all system administration functions. For example, monitoring system services and starting and stopping them takes on a role of its own. Your Linux box might appear to be running smoothly while one or more processes have stopped. A Linux server might seem happy on the outside, for example, while the database serving Web pages has failed.

When services to users become critical needs, you need to be prepared and stay ahead of problems. Most of these tasks can be managed using Cron. It will make your life easier and keep your users happy.

Linux toolsUncategorizedhow-to