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.
| Special string | Meaning |
| @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:
More examples
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.
Interestingly, DiskInternals Linux Readers is simple, straightforward, and handy for the purpose.Related articles
- How to Access Linux Ext2 or Ext3 on Windows
- Mount Linux Drive on Windows for Free
- Guide in Pictures
- A Bash Dirname Command
- Learn about Korn shell scripting
- Bash: how to split strings in Linux
- Here is everything you need to know about Bash for Windows
- How to Use Shell Script Sleep Command
- Linux: Sudo in Bash Scripts
- Use a Shell Script to Send an Email
- Learn about useful bash scripts
- Learn about systemd startup script
- Using /usr/bin/env command
- The disk you inserted was not readable by this computer error
- Shell script usage
- About a bash date command
- How to Access Ext4 from Windows
- How to Mount Ext4 on Windows for Free
- Bash Script: All You Need to Know
- An Algorithm: How to Create Bash While Loop
- Linux Shell: What You Need to Know at First
- 5 Basic Shell Script Examples for Your First Script
- Bash: How to Check if the File Does Not Exist
- Bash Time Command on Linux
- How to use bash get script directory in Linux
- How to Check Bash String Equality
- Bash: How to Loop Through Files in Directory
- 20 Examples of Bash Find Command
- Bash Cat Command in Examples
- Bash Script SSH: How to Use It
- Bash: A Script For User Input
- Linux ZSH: the basic you need to know
- Basic of Grep in Linux Shell Script
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Hello World Script in Bash
- A Linux bin/bash Shell
- Linux: New Line in Shell Script
- Linux: Bash String Ends With
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: $0 in a Shell Script
- Linux: A Bash Startup Script
- Linux: Write a Shell Script
- Shell Script: Replace String in File
- A Bash Multiline Command
- Bash and sh: is there any difference
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- Linux: A Bash Linter
- A Bash Nested Loop
- A Bash Test Command
- A Bash Status of Last Command
- Linux: A Bash Basename Command
- Using Bash to Write to a File
- About Bash Language
- Bash for Loop in One Line
- About chaining bash commands
- Learn about a bash error code
- Using a bash tee command
- About a bash export command
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- To run a shell script in Dockerfile
- About a bash UNTIL loop
- Learn about C shell script
- Whether bash waits for command to finish
- Using bash if 0
- Using && in an IF statement in bash
- If you want to run shell script in background
- Learn about SFTP in bash scripting
- Learn to run Perl script in Linux
- Examples of using the Expect
- Copy command in shell scripts
- Install Oracle Database
- AWK in a Bash Script
- How to Use Linux Wait Command




