Skip to content

Latest commit

 

History

History
158 lines (94 loc) · 3.34 KB

File metadata and controls

158 lines (94 loc) · 3.34 KB

Job Scheduling with crontab in Linux

Job scheduling in Linux is often accomplished using the crontab command. crontab allows users to automate the execution of tasks at specific intervals, making it a powerful tool for managing periodic activities. This tutorial will guide you through the usage of crontab to schedule jobs in a Linux environment.

Linux cron Commands

1. Displaying Current User's Crontab

crontab -l

This command lists the cron jobs for the current user.

2. Editing Current User's Crontab

crontab -e

This command opens the current user's crontab for editing.

3. Removing All Cron Jobs

crontab -r

This command removes all cron jobs for the current user.

4. Displaying Another User's Crontab

crontab -u username -l

This command displays the cron jobs for a specific user (replace username with the actual username).

5. Editing Another User's Crontab

sudo crontab -u username -e

This command opens the crontab for editing for a specific user with elevated privileges.

6. Checking Cron Service Status

systemctl status cron

This command checks the status of the cron service.

7. Restarting Cron Service

sudo systemctl restart cron

This command restarts the cron service.

8. Checking Cron Log

grep CRON /var/log/syslog

This command displays the cron-related entries in the system log.

9. Testing Cron Syntax

crontab -l | crontab -

This command checks the syntax of the crontab file without applying changes.

10. Checking Cron Version

cron -v

This command displays the version information for the cron daemon.

11. Displaying System's Cron Configuration

cat /etc/crontab

This command displays the system-wide cron configuration.

12. Displaying Daily Cron Jobs

ls /etc/cron.daily/

This command lists the daily cron jobs configured on the system.

13. Displaying Weekly Cron Jobs

ls /etc/cron.weekly/

This command lists the weekly cron jobs configured on the system.

14. Displaying Monthly Cron Jobs

ls /etc/cron.monthly/

This command lists the monthly cron jobs configured on the system.

15. Displaying Hourly Cron Jobs

ls /etc/cron.hourly/

This command lists the hourly cron jobs configured on the system.

Example

Crontab Job Schedule Example

Schedule a Job Every Day at 3:30 AM

30 3 * * * /path/to/your/command_or_script.sh
  • 30 - Minutes field, indicating that the job should run when the minute is 30.
  • 3 - Hours field, indicating that the job should run at 3 AM.
  • * - Wildcard for the day of the month, meaning the job runs every day.
  • * - Wildcard for the month, meaning the job runs every month.
  • * - Wildcard for the day of the week, meaning the job runs every day of the week.
  • /path/to/your/command_or_script.sh - The full path to the command or script you want to run.

Make sure to adjust the path and the script/command accordingly based on your requirements. Save the crontab entry using the crontab -e command.

crontab practice best website: Click Here