Continuing with his Linux 101 series, Jack Wallen introduces you to the basics of task scheduling with cron.
For the admins who are new to Linux, I want to introduce you to the cron tool. What is cron? Simply stated, cron allows you to create scheduled jobs on a Linux system. Say, for instance, you have a backup script, called backup.sh. You’ve placed that script in /usr/local/bin, so it can be executed globally and you want to make sure the backup happens at either a specific time of day or regularly on a certain day of the week.
How do you do this? You invoke cron.
To use this, you add conjobs to your user’s crontab file. Log on to your Linux server and issue the command:
This will list out all existing cron jobs. To create a new cron job, you edit the crontab file. To do this, issue the command:
You would then add a line at the bottom of that file to schedule the running of your backup script. Here’s where it gets a bit tricky. The first section of the line is where you declare when the job is to run. At the beginning of that line there are five slots for time, which are (from left to right): Minutes (0-59), hours (0-23), day of month (1-31), month (1-12), day of week (0-6, although you can use Sunday, Monday, Tuesday, etc., and Sunday can be represented by 0, 7, or Sunday).
Let’s say you want that backup job to run every Sunday at 11pm. That entry could be 0 23 * * 0. A star indicates the job is to happen every iteration. In this case we’ve indicated the job should happen Sunday at 11pm, every week and every month. The rest of this line would include the explicit path to the executable, so /usr/local/bin/backup.sh.
Our entire line would be 0 23 * * 0 /usr/local/bin/backup.sh. Save the crontab file with the Ctrl+X combination (if nano is your editor of choice), and the job is scheduled. You might want to make sure your job is listed with the crontab -l command.
Congratulations, you’ve just scheduled your first cron job. This should be just enough to get you going with this powerful Linux scheduling tool.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.
This post was written by and was first posted to TechRepublic
Do you find this article helpful? Your Friend might too. So, please Share it with them using the Share button above.
Will you like to get notified when I post new updates? Then Follow me on any of my social media handles: Google News, Telegram, WhatsApp, Twitter, Facebook, Pinterest.
You can also drop your email address below if you wish to be notified by mail.