Planowanie zadań: `cron` i `at`
Zautomatyzują Państwo wykonywanie zadań o określonych porach lub w określonych odstępach za pomocą zadań `cron` i poleceń `at`.
Planowanie zadań: `cron` i `at` to bezpłatna lekcja Linux Command Line Mastery na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Linux Command Line Mastery, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Linux Command Line Mastery zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Automating Your Linux Tasks
In Linux, you often need to run tasks automatically. This could be anything from backing up files to generating reports.
Automation saves time and ensures tasks are performed consistently, even when you're not actively at the keyboard.
Introducing `cron` for Recurring Jobs
cron is a powerful utility that helps you schedule commands or scripts to run automatically at specific intervals. Think of it as your personal assistant for repetitive tasks!
These scheduled tasks are often called "cron jobs".
Your Personal `crontab` File
Each user has their own crontab file, which lists their scheduled cron jobs.
- To edit your crontab:
crontab -e - To list your current jobs:
crontab -l - To remove all your jobs:
crontab -r(use with caution!)
When you edit, it typically opens in a text editor like Nano or Vim.
crontab -eUnderstanding `cron` Job Structure
A cron job entry has six parts:
minute hour day_of_month month day_of_week command_to_execute- Minute: 0-59
- Hour: 0-23 (0 is midnight)
- Day of Month: 1-31
- Month: 1-12 (or Jan-Dec)
- Day of Week: 0-7 (0 or 7 is Sunday)
An asterisk (*) means "every" value for that field.
Practical `cron` Examples
Let's look at some common cron job examples you might add to your crontab file.
# Run a script every day at 3:00 AM
0 3 * * * /home/user/daily_backup.sh
# Run a command every Sunday at 10:30 PM
30 22 * * 0 echo "Weekly check!" >> /var/log/weekly.log
# Run every 15 minutes
*/15 * * * * /usr/bin/some_utilityHandy `cron` Shortcuts
For common schedules, cron offers special keywords:
@reboot: Run once after system startup.@hourly: Run once an hour.@daily: Run once a day (midnight).@weekly: Run once a week (midnight Sunday).@monthly: Run once a month (midnight on 1st).@yearly: Run once a year (midnight on Jan 1st).
These keywords replace the first five time fields.
Scheduling Single Tasks with `at`
While cron is for recurring tasks, the at command is perfect for scheduling a command to run only once at a specified future time.
It's useful for tasks that don't need to repeat, like running a report at the end of the day or a specific cleanup task.
How to Use the `at` Command
To schedule a task with at, you provide the time and then type the command(s) you want to run. Press Ctrl+D to finish.
Here's how to schedule a simple message to appear in 1 minute:
echo "echo 'Task completed by at!'" | at now + 1 minuteManaging Your `at` Queue
You can manage your scheduled at jobs:
- To list all pending
atjobs:atq - To remove a specific job:
atrm [job_number]
Each job listed by atq will have a unique job number.
# List all pending 'at' jobs
atq
# Remove the job with ID 42
atrm 42`cron` & `at` Quick Check
Consider the following scenario:
You need to schedule a script named /opt/cleanup.sh to run every Tuesday at 4:45 AM.
Which crontab entry correctly achieves this?
Recap: `cron` and `at`
Great job! You've learned how to automate tasks on Linux:
cronis for scheduling recurring jobs using a `crontab` file and specific time syntax.atis for scheduling commands to run once at a future time.
These tools are essential for system administration and keeping your systems running smoothly without constant manual intervention.
Często zadawane pytania
Czy lekcja „Planowanie zadań: `cron` i `at`” jest bezpłatna?
Tak — pełny tekst „Planowanie zadań: `cron` i `at`” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Linux Command Line Mastery, przejdź na CoddyKit PRO. Kurs Linux Command Line Mastery zawiera 4 lekcji w sumie.
Co nauczysz się w „Planowanie zadań: `cron` i `at`”?
Zautomatyzują Państwo wykonywanie zadań o określonych porach lub w określonych odstępach za pomocą zadań `cron` i poleceń `at`. Ćwiczysz Linux Command Line Mastery z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Linux Command Line Mastery?
Nie wymagamy żadnego doświadczenia. Linux Command Line Mastery w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.
Ile czasu zajmuje lekcja „Planowanie zadań: `cron` i `at`”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Linux Command Line Mastery?
Tak. Każda lekcja Linux Command Line Mastery zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Planowanie zadań: `cron` i `at`
- Zarządzanie usługami: `systemctl` (systemd)
- Automatyzacja zadań za pomocą funkcji powłoki
- Scentralizowane logowanie i rotacja za pomocą journald i logrotate