How to Write a Crontab Shell Script
Are you using a Linux system? Sometimes, you may want to automate some tasks to run at off-hours while you focus on other important stuff or get some rest. Unlike on a Windows PC, where you can use a software app to automate tasks, Linux requires the use of specific commands.
Here you will find out:
- what the crontab shell script service is
- what the crontab uses for
- how to use the crontab command (with examples)
- when DiskInternals can help you
Are you ready? Let's read!
About the crontab shell script service
The cron service allows a Linux sysadmin to run commands, tasks, or programs at scheduled time and date, either daily, weekly, or monthly. Cron runs with stipulated commands entered in a cron table (crontab). Even though cron is used for sysadmin actions, every user on a Linux system can create a cron file.
Arguably, cron is one of the handiest tools for Linux or UNIX users. It is possible to run backups, clean temp directories, and do many other things using the cron service.
What the crontab is used for
It’s possible to edit the cron file only if you use the crontab command. You can use crontab for creating, editing, installing, removing and listing cron jobs in Vixie Cron. Crontab files can be saved in /var/spool/cron/crontabs, and cannot be edited directly; instead, you must use the crontab command for editing the cron jobs.
Interestingly, crontab activates the crond service (daemon) if you want to save and close the editor. Vi can be used as a default editor. Note that a cron file doesn’t contain any command; you must write the commands from scratch.
How to use the crontab command
Here is a code sample that illustrates how cron works.
Note: use crontab to create, view or edit cron jobs.
You should note that any environment cannot be provided by cron; thus, the cron job can be run by the default environment that is set by the first three lines of the above code. It will run with the bash shell as shown in the first line, while the MAILTO variable indicates where the user can get the cron job results (the email address). The third line indicates the path for the environment.
Furthermore, the several comments included in the script above detail the required syntax that defines a cron job.
The meaning of special cron strings
Using special cron strings helps to improve the readability of the script. Below are the special strings for cron jobs.
|@reboot||Run at startup|
|@yearly / @annually (0 0 1 1 *)||Run once a year|
|@monthly (0 0 1 * *)||Run once a month|
|@weekly (0 0 * * 0)||Run once a week|
|@daily / @midnight (0 0 * * *)||Run once a day|
|@hourly (0 * * * *)||Run once an hour|
Some examples of cron job scripts
Here are some examples of cron jobs. If you want to run a script with the name “/root/test.sh” daily at 5 a.m., the crontab entry will look like this:
To run /path/to/command seven minutes after midnight, every day:
If you wish to run a script at 3:20 p.m. on the first day of every month:
To run scripts on weekdays at 8 p.m.:
If you wish to run a script 23 minutes after midnight, 2 a.m., 4 a.m., etc. everyday:
Is it possible to open Linux files from Windows?
Naturally, you cannot access or view Linux partitions on Windows; however, with the help of DiskInternals Linux Reader, you can easily access your Linux files from Windows. This software app is a free utility that lets you view Ext2/3/4 partitions and access all your Linux files on a Windows PC. DiskInternals Linux Reader is an intuitive software solution that is very easy to use.