# at and crontab - scheduling commands

Ref: <http://faculty.salina.k-state.edu/tim/unix_sg/advanced/schedule.html>

You may want to schedule some programs to run at later time (**at**) or want them to run on a regular, repeating schedule (**crontab**).

Unix has a facility for running scheduled tasks called **cron**, but users do run **cron** directly. It is always running in the background to run scheduled commands at the appropriate times. We call system programs, such as **cron**, that run in the backgroud *daemons*.

The programs that users use to schedule programs are **crontab** and **at**.

## at

```bash
$ at 2350 -f query.sh
# run query.sh at 23:50
# in the file, use absolute paths

$ at 2340
echo abc
# (ctrl-D)
job 3 at Sun Jan 12 23:00:00 2020
# input scheduled command in terminal

$ atq
# show all scheduled work, same as at -l
2	Sun Jan 12 23:40:00 2020
1	Sun Jan 12 23:50:00 2020

$ atrm 2
# remove job #2, same as at -r 2

$ atq
1	Sun Jan 12 23:50:00 2020
# job #2 has been removed
```

**at** understands noon, midnight, teatime (4PM), tomorrow (10AM), now + 1 hour, 4pm + 3 days, etc. (more examples [here](https://www.computerhope.com/unix/uat.htm))

## crontab

```bash
$ crontab -e
# edit (add/remove) scheduled jobs (time + script)
# for time format, see https://crontab.guru/
# for script, again, use absolute path
# (in the script also)
# in vim
6 10 19 11 * /Users/davidfeng/query.sh
# :wq save and quit
crontab: installing new crontab

# alternatively
# task.crontab
# 6 10 19 11 * /Users/davidfeng/query.sh
$ crontab task.crontab

$ crontab -l
# list all scheduled jobs
6 10 19 11 * /Users/davidfeng/query.sh

$ crontab -r
# Remove your crontab
# effectively un-scheduling all crontab jobs
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.davidfeng.us/command-line-1/scheduling-commands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
