هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
المهارات في هذا الفصل
- تشغيل المهام لاحقًا أو بجدول زمني كمستخدم عادي
Run a command once later with at
at TIME reads commands and runs them one time at that time. The atd service must run.
systemctl enable --now atd
at 22:30
at> tar czf ~/backup-$(date +%F).tar.gz ~/Documents
at> <EOT>
job 7 at Mon Oct 27 22:30:00 2026
echo "systemctl --user restart myapp" | at now + 15 minutes
at -f ~/bin/report.sh tomorrow 06:00
atq
7 Mon Oct 27 22:30:00 2026 a student
8 Mon Oct 27 12:05:00 2026 a student
at -c 7 | tail -n 3
atrm 8
batch < ~/bin/heavy-job.shPress Ctrl+D to end the input of at (it shows <EOT>).
| Time format | Means |
|---|---|
22:30, 10:15 PM | The next time it is that hour |
now + 30 minutes, now + 2 hours, now + 3 days | From now |
tomorrow 06:00, noon, midnight, teatime (16:00) | Named times |
2026-12-31 23:00 | An exact date and time |
batch runs the job when the load is low (below 0.8).
Write a personal crontab
crontab -e opens your cron table. Each line = 5 time fields + a command. crond runs it with your rights.
crontab -e
crontab -l
*/15 * * * * /home/student/bin/checkdisk.sh >> /home/student/disk.log 2>&1
0 7 * * 1-5 /home/student/bin/report.sh
@reboot /home/student/bin/start-tunnel.sh
crontab -r
crontab -u anna -l┌──────── minute (0-59)
│ ┌────── hour (0-23)
│ │ ┌──── day of month (1-31)
│ │ │ ┌── month (1-12 or jan-dec)
│ │ │ │ ┌ day of week (0-7 or sun-sat; 0 and 7 = Sunday)
│ │ │ │ │
* * * * * command| Example | Runs |
|---|---|
30 2 * * * | Every day at 02:30 |
*/10 * * * * | Every 10 minutes |
0 9-17 * * 1-5 | Every full hour from 9 to 17, Monday to Friday |
0 0 1 * * | At midnight on the first day of every month |
15 3 * * 0 | Sundays at 03:15 |
@daily / @weekly / @reboot | Once a day / once a week / at boot |
انتبه
When day of month and day of week are both set, cron runs the job when either matches. 0 8 1 * 1 runs on the 1st and on every Monday.
Make cron jobs work like in the shell
Cron jobs run with a very small environment: few variables, a short PATH, no terminal. Write the job so it does not depend on your login shell.
- Use full paths for commands and files:
/usr/bin/rsync, notrsync. - Send the output to a log:
>> /home/student/job.log 2>&1. Otherwise cron tries to mail it. %is special in a crontab: writedate +\%F.- Set variables at the top of the crontab when you need them.
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=""
0 1 * * * /home/student/bin/backup.sh >> /home/student/backup.log 2>&1
5 1 * * * tar czf /home/student/etc-$(date +\%F).tgz /home/student/etcفي العمل
Test the command first with env -i /bin/bash -c 'COMMAND' — that is close to the clean environment cron uses.
Check that cron jobs ran
crond logs every start in the journal (and in /var/log/cron).
crontab -l
journalctl -u crond --since today
grep student /var/log/cron | tail -n 5
Oct 27 12:15:01 server1 CROND[6120]: (student) CMD (/home/student/bin/checkdisk.sh >> /home/student/disk.log 2>&1)
tail -n 3 ~/disk.logControl who may use cron and at:
| File | Effect |
|---|---|
/etc/cron.allow | If it exists, only these users may use crontab |
/etc/cron.deny | These users may not (used when cron.allow does not exist) |
/etc/at.allow, /etc/at.deny | The same for at |
Use a systemd user timer
A user timer is a timer unit in ~/.config/systemd/user/. It starts a user service, logs in the journal, and can catch up on missed runs.
[Unit]
Description=Make my daily report
[Service]
Type=oneshot
ExecStart=/home/student/bin/report.sh[Unit]
Description=Run my report every work day at 07:00
[Timer]
OnCalendar=Mon..Fri 07:00
Persistent=true
[Install]
WantedBy=timers.targetsystemctl --user daemon-reload
systemctl --user enable --now report.timer
systemctl --user list-timers
journalctl --user -u report.service
loginctl enable-linger student
loginctl show-user student -p Linger
Linger=yesتذكّر
User services run only while the user is logged in — unless linger is on. loginctl enable-linger USER lets the user's timers and services run all the time, also after a reboot.
ملاحظة
Persistent=true runs a missed job at the next start (for example when the laptop was off at 07:00).
Choose the right tool
All three tools run jobs, but each one fits a different case.
| Need | Use |
|---|---|
| Run something once, later today or tomorrow | at |
| A simple repeating job, classic syntax everybody knows | crontab -e |
| Logs in the journal, missed runs caught up, dependencies, resource limits | systemd timer |
systemd-run --user --on-calendar='*-*-* 18:00' /home/student/bin/sync.sh
systemd-run --user --on-active=30m /usr/bin/notify-send "Break!"systemd-run makes a temporary timer without writing unit files — like at, but in systemd.