`Cron` is a time-based job scheduler in Unix-like operating systems, including Debian/Ubuntu. It automates the execution of tasks by scheduling them to run at specific times, dates, or intervals according to predefined schedules. Each scheduled task is known as a `cron job`.
To edit the `crontab` file for the current user, use the following command:
Linux (Debian/Ubuntu)
crontab -e
This command opens the `crontab` file in your default text editor (such as `nano` or `vi`). If it's your first time using it, the system may prompt you to choose an editor.
Each line in a `crontab` file represents a `cron job` and follows this format:
Linux (Debian/Ubuntu)
* * * * * command_to_execute
- - - - -
| | | | |
| | | | +----- Day of week (0 - 7) (Sunday= 0 or 7)
| | | +------- Month (1 - 12)
| | +--------- Day of month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Where:
Minute: 0-59
Hour: 0-23
Day of month: 1-31
Month: 1-12
Day of week: 0-7 (0 and 7 represent Sunday)
An asterisk (`*`) means `every` within that field.
Here are some examples to illustrate the syntax:
Run a script every day at 2:30 AM:
Linux (Debian/Ubuntu)
30 2 * * * /path/to/your/script.sh
Run a command every Monday at 8 PM:
Linux (Debian/Ubuntu)
0 20 * * 1 /path/to/your/command
`Cron` also supports special strings for scheduling:
`@reboot`: Runs once at startup.
`@yearly`, `@annually`: Runs once a year (equivalent to `0 0 1 1 *`).
`@monthly`: Runs once a month (equivalent to `0 0 1 * *`).
`@weekly`: Runs once a week (equivalent to `0 0 * * 0`).
`@daily`, `@midnight`: Runs once a day (equivalent to `0 0 * * *`).
`@hourly`: Runs once an hour (equivalent to `0 * * * *`).
Example:
Run a script every hour
Linux (Debian/Ubuntu)
@hourly /path/to/your/script.sh
After making changes to your `crontab` file using `crontab -e`, it's essential to save and close the editor for the changes to take effect. `Cron` will automatically install the new `crontab` file, which means it will begin scheduling tasks according to the updated configuration.
Example:
Suppose you've added the following line to your `crontab` to execute a script every day at 3:15 AM:
Linux (Debian/Ubuntu)
15 3 * * * /path/to/another/script.sh
After adding this line and saving the `crontab` file (`Ctrl + X` in `nano` editor, then `Y` to confirm and `Enter`), `cron` will start executing `/path/to/another/script.sh` at 3:15 AM daily.
Once you've added `cron jobs`, it's crucial to verify that they are running as expected. Here are a few methods to check:
Checking Output Files: `Cron jobs` often produce output that can be logged to specific files (`/var/log/syslog`, `/var/log/cron.log`). Check these files to see if there are any errors or logs from your `cron` jobs.
Using `ps` Command: The `ps` command in Unix-like systems is used to list currently running processes. `ps aux` shows all processes in a user-oriented format, and `grep cron` filters the results to show only processes containing `cron`.
Example Usage:
ps aux | grep cron
Output could look something like this:
root 1234 0.0 0.0 12345 6789 ? Ss Jun01 0:00 /usr/sbin/cron -f
In this example:
`root`: The user under which the `cron` daemon is running.
`1234`: Process ID (PID) of the `cron` daemon.
`Jun01`: Start time of the process.
`/usr/sbin/cron -f`: The command being executed (`cron -f` means `cron` is running in the foreground).
If you see an output similar to the above, it confirms that `cron` is actively running and managing scheduled tasks on your system.
`Cron jobs` provide a powerful way to automate tasks on your Debian system. By following these steps and examples, you can effectively schedule scripts and commands to run at specified times without manual intervention. Adjust the timing and commands as per your specific requirements, and use verification methods like checking logs and using `ps` commands to ensure your `cron jobs` are running smoothly.