# at daemon
- `atd` checks `/var/spool/at` every 60 sec for jobs submitted using `at` command and runs them
- one time command execution
- allows a non-repeating command to be executed at a specific time
- for example `sleep 10; echo "hello"` can b converted to an at job
- `at now +2 minutes` opens `at>` shell, which shows the time at which a command to be entered will execute
- for example `echo "hello" > ~/temp/hello.txt`
- crtl+d to exit at shell
- `atq` shows queue of at jobs and execution times and job numbers
- `jobs` shows jobs for current shell (as opposed to processes)
- `fg job_number` moves job to foreground
- `bg job_number` moves job to background
- `nohup at -f file_name time` to persist at job upon shell close (no hangup)
- result sent appended to `nohup.out` in current directory
- `at tomorrow` opens `at>` shell, which shows the time at which the command to be entered will execute
- for example `rm ~/temp/hello.txt
- `at -f file_name time` runs file at specified time
- `atrm job_number` removes at job
- `batch` is part of the `at` daemon
- execution trigger is based on system load as opposed to precise timing
- `batch` executes commands in batch queue (part of `atq`) once system has a load below 1.5
# User Crontab
![[images/Pasted image 20250808205414.png]]
- `cron` tries to run a commands in cron table repeatedly over a specified interval
- useful for repeated task such as a backup script
- five fields that determine when a command is executed
- f1 = minute of the hour (0-59)
- `*/15` = every 15 minutes
- `09,39` for 9th and 39th minutes of every hour
- f2 = hour of the day (0-23)
- f3 = day of the month (1-31)
- f4 = month of the year (1-12)
- f5 = limit to specific day of the week (0-6, where Sunday=0 or 7)
- enter an * for every minute/hour/day/month
- enter an * for f5 to be agnostic as to day of week
- edit user crontab via `crontab -e`
- file lives in `/var/spool/cron/user_name`
- but don't edit directly bc `crontab -e` provides syntax checks
- `crontab` options:
- -e to edit user's crontab
- -l to list user's crontab ![[images/Pasted image 20250806171124.png]]
- -r to delete user's crontab
- -n for dry run
# System Crontab
![[images/Pasted image 20250808205501.png]]
- root has its own user crontab
- system wide cron files live in `/etc` including `/etc/crontab`![[images/Pasted image 20250807200814.png]]
- best not to edit `/etc/crontab` and use `cron.d` instead
- `/etc/cron.d` includes custom system wide cron files
- system wide cron files include an extra f6 for user name
- who does system execute this command as
- `cron.hourly`, `cron.daily`, `cron.weekly`, `cron.monthly` are folders within `/etc/` where any files within them run according to that interval ![[images/Pasted image 20250806171343.png]]
- the cron timing fields do not apply here, instead only includes executable scripts
- timing based on `/etc/crontab`, which is randomized by default
# systemd as a cron replacement
- timer units
- enable more optionality than `cron` such as waiting X seconds from a specified event such as starting script B 30min after script A completes
- `sudo systemctl status *timer` shows all timer units ![[images/Pasted image 20250717202037.png]]
## Create custom service and timer untis
- `cd /etc/systemd/system` shows systemd units
- `nano /etc/systemd/system/test1.service` to create timer test service
- `sudo systemctl status test1.servicce` shows that test1 is loaded but inactive ![[images/Pasted image 20250717202911.png]]
- `sudo systemctl start test1.service` starts test1.service ![[images/Pasted image 20250717203557.png]]
- `sudo journalctl -S today -u test1.service` shows log entires for today for test1.service ![[images/Pasted image 20250717203121.png]]
- `sudo nano test1.timer` to configure associated timer unit to run every second ![[images/Pasted image 20250717203410.png]]
- `sudo systemctl restart test1.service` to now restart test1.service to affect config changes
- `sudo journalctl -S today -u test1.service -f` follows real-time updates with test1.service's execution of the `free` command