How to Add Crontab Entry in Linux

How to Add Crontab Entry in Linux

The quickest way to understand how to add crontab entry in linux is to open your user crontab with crontab -e, add a schedule line in the correct format, then save the file. Cron will run the command at the time you specify, as long as the command path, permissions, and syntax are correct.

What a crontab entry does

A crontab entry tells the Linux cron scheduler when to run a command or script. Each line in the crontab represents one scheduled task. This is useful for backups, cleanup jobs, report generation, log rotation, and other recurring tasks.

For most users, cron jobs are added to a personal crontab. System-wide cron files also exist, but they are managed differently and usually require root access.

Cron syntax explained

A standard crontab line has five time fields followed by the command to run:

minute hour day-of-month month day-of-week command

FieldAllowed valuesExample
Minute0 to 590
Hour0 to 233
Day of month1 to 3115
Month1 to 127
Day of week0 to 7, where 0 and 7 usually mean Sunday1

Special symbols are also supported:

  • * means every possible value in that field.
  • , lets you list multiple values, such as 1,15.
  • - defines a range, such as 1-5.
  • / defines an interval, such as */10 for every 10 minutes.

How to add a crontab entry in linux

How to add a crontab entry in linux

Follow these steps to create a cron job for your user account.

1. Open your crontab editor

Run:

crontab -e

The first time you run it, Linux may ask you to choose a text editor. Pick the one you are comfortable with. After that, your personal crontab opens for editing.

2. Add your cron job line

Insert a new line using the correct schedule and command. For example, to run a script every day at 2:30 AM:

30 2 * * * /home/youruser/scripts/backup.sh

For a command to run every 15 minutes:

*/15 * * * * /usr/bin/php /home/youruser/app/cleanup.php

Always use the full path to the command or script. Cron runs with a limited environment, so commands that work in your terminal may fail in cron if you rely on PATH shortcuts.

3. Save and exit

Save the file and close the editor. Once the file is saved, cron automatically reads the updated crontab and schedules the new entry.

4. Verify the entry was added

Check your current crontab with:

crontab -l

This prints the active cron jobs for your user. If your line appears in the output, it was added successfully.

Example cron entries

Here are a few practical examples you can adapt:

  • Run every hour at minute 0: 0 * * * * /path/to/script.sh
  • Run every day at 1:00 AM: 0 1 * * * /path/to/script.sh
  • Run every Monday at 6:30 AM: 30 6 * * 1 /path/to/script.sh
  • Run on the 1st of every month at midnight: 0 0 1 * * /path/to/script.sh
  • Run every 5 minutes: */5 * * * * /path/to/script.sh

How to add output logging for easier troubleshooting

How to add output logging for easier troubleshooting

If a cron job does not behave as expected, redirect its output to a log file. This helps you see errors and confirms whether the command ran.

Example:

0 2 * * * /home/youruser/scripts/backup.sh >> /home/youruser/cron.log 2>&1

This sends both standard output and errors to cron.log. It is one of the simplest ways to debug failed cron jobs.

Common cron mistakes

  • Using relative paths: Cron may not find script.sh unless you provide the full path.
  • Forgetting execute permission: A script may need chmod +x script.sh.
  • Wrong shell syntax: A line that works interactively may fail under cron if it depends on shell features not available in the command string.
  • Missing environment variables: Cron does not load your full login environment by default.
  • Incorrect time fields: A single misplaced number can change when the job runs.
  • Not using the right user crontab: crontab -e edits the current user’s crontab, not the system-wide one.

How to troubleshoot a cron job that is not running

How to troubleshoot a cron job that is not running

If your new entry does not run, check these items in order:

  1. Confirm the job is in the crontab: Use crontab -l.
  2. Check the command path: Use absolute paths for the script and any tools it calls.
  3. Check permissions: Make sure the script can be read and executed by the correct user.
  4. Test the command manually: Run the exact command in a shell first.
  5. Look at logs: Review your redirected log file, system logs, or mail output if enabled.
  6. Check the cron service: On many systems, the cron daemon must be running for jobs to execute.

If your script depends on other programs, include their full paths too. For example, a shell script that calls python may need /usr/bin/python3 instead of just python3.

Editing or removing an existing cron job

Editing or removing an existing cron job

To change an existing entry, open the crontab again with crontab -e, edit the line, then save the file.

To remove all jobs for your user, use:

crontab -r

Be careful with this command because it deletes the entire crontab for the current user. If you only want to remove one job, edit the crontab instead of deleting it.

When to use cron versus other schedulers

Cron is a strong choice for simple recurring jobs. If you need calendar-aware scheduling, service supervision, or tight integration with system startup and dependencies, a systemd timer may be a better fit. For straightforward repetition, though, cron remains one of the easiest tools on Linux.

Frequently Asked Questions About how to add crontab entry in linux

What is the easiest way to add a cron job in Linux?

Use crontab -e, add the schedule line, save the file, and confirm it with crontab -l.

Do I need root to add a crontab entry?

No, not for your own user crontab. Root is only needed for system-wide cron files or jobs that must run as another user.

Why does my cron job work in the terminal but not in crontab?

cron runs with a smaller environment, so PATH settings, shell aliases, and login profile variables may not be available. Use absolute paths and keep the command explicit.

How can I check whether cron is installed and running?

That depends on your Linux distribution. In many cases, the package is called cron or cronie, and the service should be active for scheduled jobs to run.

Can I schedule a script to run every minute?

Yes. Use * * * * * followed by the full path to the script or command.

How do I see all my current cron jobs?

Run crontab -l to list the active entries for your user.

Conclusion

To add a crontab entry in Linux, open crontab -e, write the job in the correct five-field format, save the file, and verify it with crontab -l. The most common issues are bad syntax, missing execute permission, and commands that rely on relative paths or missing environment variables. If you use full paths and test your command carefully, cron is a reliable way to automate repetitive Linux tasks.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *