Cron is a powerful tool in Unix and Unix-like operating systems, which allows users to schedule jobs (commands or scripts) to run at fixed times, dates, or intervals. This article will guide you on how to set up Cron jobs in Ubuntu.
To set up Cron jobs in Ubuntu, you need to edit the crontab file for the user whose job you want to run. Use the
crontab -e command to open the crontab file in the default text editor. Specify the schedule for your job using the appropriate values and add the command you want to run. Save the file and exit the editor.
What is Cron?
Cron is a time-based job scheduling daemon found in Unix-like operating systems, including Linux distributions like Ubuntu. Cron reads the configuration files for a list of commands to execute. The commands are typically used to automate system maintenance or administration tasks, though they can be used for other purposes as well.
Setting Up Cron Jobs
To set up a Cron job in Ubuntu, you need to edit the crontab (cron tables) file for the user whose job you want to run. Here are the steps:
- Open the terminal. You can do this by pressing
Ctrl + Alt + Tor by searching for ‘terminal’ in the application menu.
crontab -eand press Enter. This command opens your personal crontab file in the default text editor.
- In the crontab file, each line represents a command to run and its schedule. The structure of a cron job is as follows:
* * * * * command to be executed - - - - - | | | | | | | | | +----- day of the week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of the month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59)
- Specify the schedule for your cron job using the appropriate values. You can use numbers, ranges, and intervals to define the schedule. For example,
*/15 * * * *would mean “every 15 minutes”.
- Add the command you want to run at the specified schedule. For example, if you want to run a script located at
/home/user/myscript.shevery day at 5:30 PM, your cron job would look like this:
30 17 * * * /home/user/myscript.sh
- Save the file and exit the editor. In most editors, you can do this by pressing
Ctrl + X, then
Examples of Cron Job Schedules
Here are some examples of cron job schedules:
- To run a command every Monday at 5:30 PM:
30 17 * * 1 /path/to/command
- To run a command every 15 minutes:
*/15 * * * * /path/to/command
- To run a command at system startup:
Cron also allows you to run scripts at predefined intervals using the
/etc/cron.weekly directories. Simply place your script in one of these directories, and it will run at the specified interval.
Graphical Interface for Cron
If you prefer a graphical interface, you can install tools like “Scheduled tasks” or “gnome-schedule” from the Software Center. These tools provide a user-friendly GUI to add and manage cron tasks.
Cron is a powerful and flexible tool for automating tasks on Ubuntu. With a little practice, you can use it to automate a wide variety of system administration tasks and other routine jobs. For more detailed information and examples, you can refer to the Ubuntu CronHowto documentation.
You can check if Cron is installed by running the command
crontab -l in the terminal. If Cron is installed, it will display the current user’s Cron jobs.
To edit someone else’s Cron job, you need to have administrative privileges. You can use the
sudo crontab -u [username] -e command to edit another user’s Cron job, replacing
[username] with the desired username.
Cron logs its activities to the system log file
/var/log/syslog. You can view the logs using the
tail command, like this:
tail -f /var/log/syslog | grep CRON.
To disable a Cron job temporarily, you can comment out the corresponding line in the crontab file by adding a
# at the beginning of the line. This will prevent the job from running until you remove the comment.
To remove a Cron job, you can open the crontab file using the
crontab -e command and delete the corresponding line for the job you want to remove. Save the file after making the changes, and the Cron job will be removed.
No, Cron is not designed to run jobs every second. The minimum interval for Cron is one minute. If you need to schedule a task to run every second, you might need to consider other alternatives like running a daemon or using a different scheduling tool.
Cron doesn’t provide direct feedback on the success or failure of a job. However, you can redirect the output of your Cron job to a log file to capture any error messages or diagnostic information. You can also use the
MAILTO variable in the crontab file to receive email notifications for the output of your Cron job.
Yes, you can schedule a Cron job to run on specific dates by specifying the desired date and time in the crontab file. For example,
0 12 25 12 * /path/to/command would run the command on December 25th at 12:00 PM.
By default, Cron runs jobs as the user who created the crontab file. If you need to run a Cron job as a different user, you can use the
sudo -u [username] [command] syntax in the crontab file to specify the desired user.
Yes, you can exclude specific hours by using the
! symbol in the hour field of the crontab file. For example,
0 0-23,!8-17 * * * /path/to/command would run the command every hour except between 8 AM and 5 PM.