Home Server 10 essential cronjobs for your Ubuntu server

10 essential cronjobs for your Ubuntu server

Vim, with its steep learning curve, rewards those who master it. Unlock the power of efficient editing with our guide, filled with must-know commands, tips, and tricks.

by Arun Kumar
important cronjobs ubuntu server

Welcome to the world of server management, where efficiency and automation are keys to a smooth-running Ubuntu server. In this blog, we will delve into the realm of cronjobs, a vital tool for any server administrator. Whether you are new to server management or a seasoned pro looking to optimize your workflow, understanding how to leverage cronjobs effectively can make a substantial difference in your server’s performance and reliability.

Cronjobs are the unsung heroes in the server world, as they can automate routine tasks like system updates and backups, as well as more advanced operations like disk space monitoring and custom script execution. In this guide, we will explore the top 10 essential cronjobs that you should be running on your Ubuntu server. We will cover their syntax, implementation details, and significance. We will also explain how to set them up and address some FAQs to ensure you are fully equipped to make the most of this powerful feature.

Join us on this journey to enhance your server’s efficiency and ensure it runs like a well-oiled machine.

What is a cron job?

Before we dive in, let’s quickly recap what a cron job is. It’s a time-based scheduler in Unix-like operating systems, including Ubuntu. You can schedule scripts or commands to run periodically at fixed times, dates, or intervals. The syntax might look a bit cryptic at first, but once you get the hang of it, it’s pretty straightforward.

10 essential cronjobs for your Ubuntu server

1. Updating the system

Regular system updates are vital for security and performance. This cronjob automates the process of fetching the latest package lists (apt-get update) and upgrading all installed packages to their latest versions (apt-get -y upgrade). Running this task during off-peak hours (like 2 AM) minimizes the impact on server performance and availability.

Why it’s essential: Regular updates keep your server secure and running smoothly.

Cron syntax:

0 2 * * * /usr/bin/apt-get update && /usr/bin/apt-get -y upgrade

This cronjob runs daily at 2 AM, updating package lists and upgrading all packages.

Expected output: A log of updated and upgraded packages.

Why it’s a favorite: I love this cronjob because it keeps the server up-to-date without any manual intervention, ensuring that security patches and performance improvements are always applied promptly.

2. Backing up your database

Data is the lifeblood of most servers. This cronjob uses mysqldump to create a backup of your database. It includes the date in the backup filename, making it easier to track and organize backups. Running this daily ensures that you have a recent copy of your data in case of corruption or data loss.

Why it’s essential: Regular backups prevent data loss.

Cron syntax:

30 3 * * * /usr/bin/mysqldump -u [username] -p[password] [database] > /path/to/backup/db_$(date +\%F).sql

This cronjob creates a daily backup of your database at 3:30 AM.

Expected output: A database backup file named with the current date.

My note: As someone who has experienced data loss in the early days, I cannot stress enough the peace of mind that comes from knowing your data is regularly backed up.

3. Monitoring disk space

Running out of disk space can lead to various problems, from failing services to corrupted data. This cronjob sends you an hourly disk space report using df -h (disk filesystems human-readable format), helping you to proactively manage disk space.

Why it’s essential: Preventing your server from running out of space avoids many headaches.

Cron syntax:

0 * * * * /bin/df -h | /usr/bin/mail -s "Disk Space Report" your@email.com

This cronjob sends an hourly disk space report.

Expected output: An email with the disk space report.

Why I like it: This job is like a constant watchdog for disk space. It’s simple but incredibly effective at preventing one of the most common server issues.

4. Deleting old logs

Log files are essential for troubleshooting, but they can grow large over time. This cronjob finds and deletes log files in /var/log older than seven days. It’s a great way to keep your disk usage in check without losing recent logs which might be needed for analysis.

Why it’s essential: Old logs can consume a lot of disk space.

Cron syntax:

0 4 * * * /usr/bin/find /var/log -type f -name '*.log' -mtime +7 -exec rm {} \;

This cronjob deletes log files older than 7 days at 4 AM daily.

Expected output: Freed disk space.

My experience: I learned the hard way how quickly log files can fill up a disk. This cronjob is a lifesaver, especially on servers with limited storage.

5. Checking server uptime

Tracking server uptime can help diagnose issues and plan for maintenance. This cronjob appends the current uptime to a log file daily. Over time, this log provides valuable data about the server’s stability and performance.

Why it’s essential: It’s good to keep track of your server’s uptime for maintenance and troubleshooting.

Cron syntax:

@daily echo "Uptime as of $(date):" >> /path/to/uptime.log && /usr/bin/uptime >> /path/to/uptime.log

This cronjob logs the server’s uptime daily.

Expected output: An updated uptime log file.

Why it’s close to my heart: Uptime logs have often helped me pinpoint recurring issues that were otherwise hard to track. It’s a simple yet powerful tool for long-term server health monitoring.

6. Synchronizing time with NTP

Accurate timekeeping is critical for everything from log timestamps to time-sensitive applications. This cronjob synchronizes the server clock with an NTP (Network Time Protocol) server, ensuring your server’s time is always accurate.

Why it’s essential: Accurate timekeeping is crucial for logs and time-sensitive tasks.

Cron syntax:

0 1 * * * /usr/sbin/ntpdate ntp.ubuntu.com

This cronjob syncs the server clock with Ubuntu’s NTP server daily at 1 AM.

Expected output: Correct system time.

My perspective: It’s a small but crucial task. I’ve seen scenarios where out-of-sync clocks led to baffling issues, especially in time-sensitive applications.

7. Scanning for malware

Regular malware scans help identify and mitigate threats before they cause harm. This cronjob uses clamscan to perform a comprehensive scan of your system and emails you the results.

Why it’s essential: Regular scans help in identifying and mitigating threats.

Cron syntax:

0 5 * * * /usr/bin/clamscan -r / | /usr/bin/mail -s "ClamAV Scan Report" your@email.com

This cronjob runs a full system scan for malware at 5 AM daily.

Expected output: An email with the scan report.

Why I appreciate it: Security is paramount, and this job is like having a diligent security guard always on duty. It’s particularly crucial for servers exposed to the internet.

8. Testing server load

Keeping an eye on server load helps you understand your server’s performance and when it might be under stress. This cronjob records the server load every 30 minutes, providing valuable data to identify patterns and potential issues.

Why it’s essential: Monitoring server load prevents performance issues.

Cron syntax:

*/30 * * * * /usr/bin/uptime >> /path/to/load_report.log

This cronjob logs server load every 30 minutes.

Expected output: Regularly updated load report.

My take: This is like a health checkup for your server. It’s helped me avert many potential performance issues by providing early warning signs.

9. Renewing SSL certificates

SSL certificates are crucial for secure communications. Letting them expire can lead to service interruptions and security warnings for users. This cronjob uses certbot to automatically renew certificates, ensuring that your secure connections stay uninterrupted.

Why it’s essential: Keeping SSL certificates up-to-date ensures secure connections.

Cron syntax:

0 2 * * 1 /usr/bin/certbot renew >> /var/log/ssl_renew.log

This cronjob attempts to renew any SSL certificates weekly on Monday at 2 AM.

Expected output: A log of renewal attempts.

My viewpoint: I’ve seen sites go down because of expired certificates. Automating this process is a no-brainer for maintaining user trust and security.

10. Running custom scripts

Every server has unique needs, and custom scripts can address those. Whether it’s cleaning up temporary files, running performance checks, or anything else specific to your environment, scheduling these scripts as cronjobs ensures they run regularly without manual intervention.

Why it’s essential: Automating routine custom tasks saves time and effort.

Cron syntax: This varies based on your script.


@weekly /path/to/your/custom/script.sh

This cronjob runs a custom script weekly.

Expected output: Depends on the script.

Why this is essential to me: Custom scripts are the essence of server personalization. I’ve automated many mundane tasks through these scripts, saving countless hours of manual work.

How to use these cronjobs on your Ubuntu server

Now that we’ve explored the cronjobs in detail, let’s talk about how to implement them on your Ubuntu server. If you’re new to cron, this section will be especially helpful.

Step 1: Access the cron file

To set up these cronjobs, you’ll need to edit the crontab file. Each user on your system, including root, has their own crontab. To edit the crontab for your current user, use the following command:

crontab -e

If you want to edit the crontab for the root user (which is often necessary for system tasks), you’ll need to use:

sudo crontab -e

Step 2: Editing the crontab

Once you enter the crontab file, you can add new cronjobs. The crontab file is structured in a specific way. Each line in the file represents a separate job, and the syntax is as follows:

* * * * * command-to-execute

These asterisks correspond to different units of time:

  1. Minute (0 – 59)
  2. Hour (0 – 23)
  3. Day of the month (1 – 31)
  4. Month (1 – 12)
  5. Day of the week (0 – 6) (Sunday to Saturday; 7 is also Sunday on some systems)

Step 3: Adding cronjobs

For each of the cronjobs discussed earlier, you would add a line to this file. For example, to add the system update job, you would insert:

0 2 * * * /usr/bin/apt-get update && /usr/bin/apt-get -y upgrade

This tells the cron daemon to run the update and upgrade commands at 2 AM daily.

Step 4: Saving and exiting

Once you’ve added your cronjobs, save and exit the editor. If you’re using the default editor (usually Nano for Ubuntu), you can do this by pressing CTRL + X, then Y to confirm the changes, and Enter to save.

Step 5: Verifying your cronjobs

After saving your cronjobs, it’s a good practice to ensure they are correctly listed. Use the following command to display the current user’s crontab:

crontab -l

This will list all cronjobs scheduled for the user. Review this list to make sure your new cronjobs are correctly added.

Additional tips

  • Testing: Before relying on a cronjob for critical tasks (like backups), manually run the command to ensure it works as expected.
  • Logging: For jobs where you’d like a record, you can redirect the output to a log file. For example: 0 4 * * * /command/to/run >> /path/to/logfile.log 2>&1
  • Error Handling: The 2>&1 at the end of a cronjob ensures that both standard output and standard error are redirected to the log file, which is useful for troubleshooting.

Frequently Asked Questions (FAQ) about using cronjobs in Ubuntu

Q1: What happens if my server is off during a scheduled cronjob?

A: If your server is off or unavailable when a cronjob is scheduled to run, the job will be skipped. Cron does not have a built-in method to run missed tasks once the system is back online. For critical tasks, consider using an alternative scheduling tool that can handle missed executions or ensure your server has high uptime.

Q2: How do I edit a cronjob?

A: To edit a cronjob, you need to edit your crontab file. Use crontab -e for your user or sudo crontab -e for the root user. Make your changes in the editor, save, and exit. The new changes will automatically be picked up by the cron daemon.

Q3: Can I set up a cronjob to run every X minutes?

A: Yes, you can. For example, to run a job every 15 minutes, use */15 in the minute field: */15 * * * * command. This syntax tells cron to run the command at every 15-minute interval.

Q4: How can I troubleshoot a cronjob that isn’t running?

A: Troubleshooting cronjobs involves a few steps:

  • Ensure your cron syntax is correct.
  • Check that the cron daemon is running (systemctl status cron or service cron status).
  • Make sure the command works when run manually.
  • Check the cron logs (/var/log/syslog on Ubuntu) for any errors related to the cronjob.
  • Verify that the user running the cronjob has the necessary permissions.

Q5: Are there any security concerns with cronjobs?

A: Yes, there are a few security considerations:

  • Ensure that only trusted users have access to edit cronjobs, especially the root crontab.
  • Be cautious with commands in cronjobs, as they run automatically. Test them manually first.
  • Regularly review your cronjobs to ensure they are still necessary and secure.

Q6: How do I remove a cronjob?

A: To remove a cronjob, edit your crontab using crontab -e, delete the line containing the job, save, and exit. The job will no longer run.

Q7: Can I run a cronjob at a specific time, like 2:15 PM?

A: Yes, you can schedule a cronjob at a specific time. For 2:15 PM, you would use 15 14 * * * command. This sets the job to run at 2:15 PM server time.

Q8: How do I set up a cronjob to run on specific days of the week?

A: The last field in the cron syntax is for days of the week (0 for Sunday, 6 for Saturday). For example, to run a job every Monday, use * * * * 1 command.

Q9: Is it possible to email the output of a cronjob?

A: Yes, cron can email the output of a job. By default, cron sends the output to the email address of the user who scheduled the job. You can also redirect output to a specific email using MAILTO='youremail@example.com' at the top of your crontab.

Q10: What is the difference between crontab -e and editing cron files in /etc/cron.* directories?

A: crontab -e edits a user-specific crontab. The /etc/cron.* directories (like cron.daily, cron.weekly, etc.) are for system-wide cron scripts and are organized based on their execution frequency. Scripts placed in these directories are automatically run by cron based on their naming convention.


Understanding and effectively utilizing cronjobs on your Ubuntu server can significantly streamline your administrative tasks, enhancing both performance and security. We delved into essential cronjobs like system updates, database backups, disk space monitoring, log file management, and more, providing insights into their importance and implementation. The guide also offered a practical walkthrough on setting up these cronjobs, coupled with a comprehensive FAQ section to address common queries and concerns.

You may also like

Leave a Comment



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us


©2016-2023 FOSS LINUX



“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.