How to schedule cron Jobs in Linux for Task Scheduling
Cron is a built-in scheduling utility in Linux that allows you to schedule tasks to run periodically at fixed times, dates, or intervals.
To create a cron job, open a terminal window on your ubuntu system.
1. Edit the cron table for the current user by running:
This will open the default text editor (usually nano or vi) with the user's cron jobs.
2. The syntax for a cron job entry consists of five fields followed by the command to be executed:
* * * * * command_to_execute
The five fields represent minute, hour, day of the month, month, and day of the week, respectively.
You can use asterisks (*) to specify "any" value or specify individual values or ranges. For example:
· ‘*' in the minute field means "every minute".
· ‘0’ in the hour field means "at the start of every hour".
· ‘*’ in the day of the month field means "every day".
· ‘*’ in the month field means "every month".
· ‘1-5’ in the day of the week field means "Monday to Friday".
3. Add your desired cron job entry to the file. For example, to run a script database backup.sh located in your /home/msa/ directory every day at 9:30 PM, you would add:
4. After adding your cron job in nano editor to save and close a file in the nano text editor, you can follow these steps:
Press Ctrl + O (the letter "O", not zero) to write the current contents to a file.
Press Enter to confirm the filename (or modify it if you wish).
Press Ctrl + X to exit nano.
After completing these steps, you'll have successfully saved and closed the file in the nano editor.
The cron daemon will automatically reload the cron table, and your cron job will be scheduled to run according to the specified schedule.
5. You can view your current cron jobs by running:
This will list all cron jobs for the current user.
That's it! You have now set up a cron job to automate a repetitive task on your Ubuntu system. Make sure to test your cron job to ensure it runs as expected at the specified schedule.
Take the first step towards data-led growth by partnering with MSA Infotech. Whether you seek tailored solutions or expert consultation, we are here to help you harness the power of data for your business. Contact us today and let’s embark on this transformative data adventure together. Get a free consultation today!
We utilize data to transform ourselves, our clients, and the world.
Partnership with leading data platforms and certified talents