#readwise
# Linux Fundamentals - Task Scheduling

## Metadata
- Author: [[Hack The Box]]
- Full Title: Linux Fundamentals - Task Scheduling
- URL: https://academy.hackthebox.com/module/18/section/2093
## Summary
Task scheduling in Linux automates tasks like software updates and backups at set times. Systemd and Cron are two tools for this purpose, with Systemd using timers and services, while Cron uses a crontab file. Understanding these tools helps cybersecurity specialists identify potential security risks. Both methods ensure tasks run reliably without manual intervention.
## Highlights
Task scheduling is a critical feature in Linux systems that allows users and administrators to automate tasks by running them at specific times or regular intervals, eliminating the need for manual initiation. ([View Highlight](https://read.readwise.io/read/01jmambx8w4jp6j1tfaxvz5x1p))
---
Knowledge of how tasks are automated allows you to identify potential security risks, such as unauthorized cron jobs that execute harmful scripts or maintain persistent backdoors at scheduled intervals. By comprehending the intricacies of task scheduling, you can detect and analyze these hidden threats, enhance system audits, and even utilize scheduled tasks to simulate attack scenarios during penetration testing. ([View Highlight](https://read.readwise.io/read/01jmamdh7cv97yed82frd5kj09))
---
Systemd and Cron are both tools that can be used in Linux systems to schedule and automate processes. The key difference between these two tools is how they are configured. With Systemd, you need to create a timer and services script that tells the operating system when to run the tasks. On the other hand, with Cron, you need to create a `crontab` file that tells the cron daemon when to run the tasks. ([View Highlight](https://read.readwise.io/read/01jmamrr47727jy9xy6j9zbthk))
---
## Systemd
Systemd is a service used in Linux systems such as Ubuntu, Redhat Linux, and Solaris to start processes and scripts at a specific time. With it, we can set up processes and scripts to run at a specific time or time interval and can also specify specific events and triggers that will trigger a specific task. To do this, we need to take some steps and precautions before our scripts or processes are automatically executed by the system.
1. Create a timer (schedules when your `mytimer.service` should run)
2. Create a service (executes the commands or script)
3. Activate the timer
([View Highlight](https://read.readwise.io/read/01jmamkxaqaqjy5a00mc4tjy3d))
Note: Creating a timer is optional.
---
### Create a Timer
To create a timer for systemd, we need to create a directory where the timer script will be stored.
```sh
sudo mkdir /etc/systemd/system/mytimer.timer.d
sudo vim /etc/systemd/system/mytimer.timer
```
Next, we need to create a script that configures the timer. The script must contain the following options: "Unit", "Timer" and "Install". The "Unit" option specifies a description for the timer. The "Timer" option specifies when to start the timer and when to activate it. Finally, the "Install" option specifies where to install the timer.
Mytimer.timer:
```
[Unit]
Description=My Timer
[Timer]
OnBootSec=3min
OnUnitActiveSec=1hour
[Install]
WantedBy=timers.target
```
Here it depends on how we want to use our script. For example, if we want to run our script only once after the system boot, we should use `OnBootSec` setting in `Timer`. However, if we want our script to run regularly, then we should use the `OnUnitActiveSec` to have the system run the script at regular intervals.
([View Highlight](https://read.readwise.io/read/01jmamm9gyaz1r1vkwyxw6fq8y))
Note: Timers are used for recurring or delayed tasks and are optional.
---
### Create a Service
```
sudo vim /etc/systemd/system/mytimer.service
```
Here we set a description and specify the full path to the script we want to run. The "multi-user.target" is the unit system that is activated when starting a normal multi-user mode. It defines the services that should be started on a normal system startup.
mytimer.service:
```
[Unit]
Description=My Service
[Service]
ExecStart=/full/path/to/my/script.sh
[Install]
WantedBy=multi-user.target
```
([View Highlight](https://read.readwise.io/read/01jmamndfvn16je2d7jmcndagx))
---
Reload Systemd
```
sudo systemctl daemon-reload
```
After that, we can use `systemctl` to `start` the service manually and `enable` the autostart.
```
sudo systemctl start mytimer.timer
sudo systemctl enable mytimer.timer
```
This way, `mytimer.service` will be launched automatically according to the intervals (or delays) you set in `mytimer.timer`. ([View Highlight](https://read.readwise.io/read/01jmamp2jetjn6gq1vkf7dcjsw))
---
## Cron
Cron is another tool that can be used in Linux systems to schedule and automate processes. It allows users and administrators to execute tasks at a specific time or within specific intervals. For the above examples, we can also use Cron to automate the same tasks. We just need to create a script and then tell the cron daemon to call it at a specific time.
With Cron, we can automate the same tasks, but the process for setting up the Cron daemon is a little different than Systemd. To set up the cron daemon, we need to store the tasks in a file called `crontab` and then tell the daemon when to run the tasks. Then we can schedule and automate the tasks by configuring the cron daemon accordingly. The structure of Cron consists of the following components:
| Time Frame | Description |
| ---------------------- | --------------------------------------------------------------------- |
| Minutes (0-59) | This specifies in which minute the task should be executed. |
| Hours (0-23) | This specifies in which hour the task should be executed. |
| Days of month (1-31) | This specifies on which day of the month the task should be executed. |
| Months (1-12) | This specifies in which month the task should be executed. |
| Days of the week (0-7) | This specifies on which day of the week the task should be executed. |
([View Highlight](https://read.readwise.io/read/01jmamqv55yy6w1ye1tj6keje9))
---