A cron job in Linux is a time-based job scheduler. It is a utility that allows you to schedule and automate the execution of scripts, commands, or programs at specific intervals or at predetermined times. Cron jobs are commonly used in Linux and other Unix-like operating systems to automate repetitive tasks, such as system maintenance, log rotation, backups, and periodic data processing.
The cron service is responsible for running these scheduled tasks. It reads a configuration file called the "crontab" (short for cron table) to determine when and which commands to execute. Each user on a Linux system can have their own crontab file, which lists the cron jobs specific to that user
The crontab file uses a specific
format to define the schedule of a cron job. It consists of six fields that
specify the timing of the job:
scss
* * * * * command_to_be_executed
| | | | |
| | | | +----- Day of the Week (0 - 7) (Sunday = 0 or 7)
| | | +------- Month (1 - 12)
| | +--------- Day of the Month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Each field can be set to a specific
value, a range of values, or an asterisk (*) to indicate any value. For
example, setting the hour field to 2 and the minute field to 0 would execute
the specified command every day at 2:00 AM.
To create or edit your crontab file,
you can use the crontab command with the -e option. This will open the crontab
file in your default text editor. After making the necessary changes, you can
save and exit the editor, and the cron service will automatically reload the
updated crontab file.
Cron jobs are a powerful tool for automating tasks in Linux, allowing you to schedule routine operations without manual intervention.
crontab commands
1. View the current crontab:
· crontab -l
· Edit the current crontab:
· crontab -e
· Remove the current crontab:
· crontab -r
· Install a new crontab from a file:
php
· crontab <filename>
· Schedule a command or script to run at specific intervals:
bash
* * * * *
command
The five asterisks represent minute, hour, day of the month, month, and day
of the week, respectively. You can use specific values or wildcards (*
) to match any value.
For example, the following entry runs the command /path/to/script.sh
every day at 3:30 AM:
JavaScript
· 30
3 * * *
/path/to/script.
sh
· Schedule a command or script to run at regular intervals:
bash
· */5 * * * *
command
The above entry executes the command every 5 minutes.
· Redirect the output of the scheduled command to a file:
lua
7.
* * * * * command > /
path/to/
output.
log
2>&
1
8. This
saves the standard output and error messages to /path/to/output.log
.
These are just a few examples of crontab commands. You can use various combinations of timing expressions and commands to create customized schedules for your tasks.
Cron job Format?
In Linux, the cron utility is used to schedule recurring tasks or commands to be executed at specific intervals. A cron job consists of a cron expression that defines the timing and frequency of the task, and the command or script to be executed.
The general format of a cron job in
Linux is as follows:
markdown
* * * * * command_to_be_executed
Each field in the above format represents a time or date value. Here's what each field signifies:
- Minute (0-59)
- Hour (0-23)
- Day of the month (1-31)
- Month (1-12)
- Day of the week (0-6, where Sunday is 0 or 7)
An asterisk () in any field means
"any value" or "every" value. For example, an asterisk ()
in the minute field means the cron job will run every minute.
You can also use specific values or
ranges in each field. Here are a few examples:
- */5 * * * *
command_to_be_executed:
This runs the command every 5 minutes.
- 0 2 * * *
command_to_be_executed:
This runs the command at 2 AM every day.
- 0 0 * * 1
command_to_be_executed:
This runs the command at midnight on Mondays.
To schedule a cron job, you
typically use the crontab command to edit the user's crontab file. Here's an example
of how to add a cron job:
- Open the crontab file for editing using the command crontab -e.
- Add a new line at the bottom of the file in the format
mentioned above.
- Save the file and exit the editor.
The cron daemon will automatically
read the crontab file and execute the scheduled jobs accordingly.
Note: The actual path to the crontab executable may vary depending on your Linux distribution.
Create job in linux example
To create a job in Linux, you can use the crontab command
to schedule the execution of a script or command at specific intervals. Here's
an example of how you can create a job:
- Open
the crontab editor by running the following command:
· crontab -e
· If prompted, choose an editor (such as nano
or vim) to edit the crontab file.
· In the editor, add a new line to specify the
schedule and command for your job. For example, let's say you want to execute a
script called backup.sh every day
at 2:30 AM. You would add the following line:
javascript
3. 30 2 * * * /path/to/backup.sh
4.
In this example:
o
30
represents the minute (30th minute of the hour).
o
2
represents the hour (2 AM).
o
*
* *
represents the day of the month, month, and day of the week (every day).
5.
Save the file and exit the editor. In nano, you can do this
by pressing Ctrl + X, then Y to confirm and Enter to save.
The job is now scheduled to run according to the specified
interval. Make sure to replace /path/to/backup.sh with the
actual path to your script.
You can use crontab
-l
to view the current crontab and ensure your job is added correctly.
Comments
Post a Comment