# Predict Service and Timer Documentation

## Overview

This setup uses **systemd services and timers** to repeatedly execute a
Python script that performs prediction on the latest sample and sends a
message.

The systemd unit files are typically stored in:

    /etc/systemd/system/

For this setup, the relevant files are:

    /etc/systemd/system/predict.service
    /etc/systemd/system/predict.timer

These files define the service execution and the timer scheduling.

-   `predict.service` -- defines how the Python script is executed
-   `predict.timer` -- schedules the repeated execution of the service

The timer triggers the service **every 5 seconds** after the first
activation.

------------------------------------------------------------------------

# Systemd Timer

File: `predict.timer`

``` ini
[Unit]
Description=Run predict sample every 5 seconds

[Timer]
OnActiveSec=60
OnUnitActiveSec=5
AccuracySec=1s
Unit=predict.service

[Install]
WantedBy=timers.target
```

## Behavior

-   **OnActiveSec=60**\
    The timer starts **60 seconds after it is activated**.

-   **OnUnitActiveSec=5**\
    After the service has run once, it will be triggered again **every 5
    seconds**.

-   **AccuracySec=1s**\
    Allows systemd to schedule the timer with **1 second precision**.

-   **Unit=predict.service**\
    Defines which service should be triggered by the timer.

------------------------------------------------------------------------

# Systemd Service

File: `predict.service`

``` ini
[Unit]
Description=Predict latest sample and send message
After=network.target
StartLimitIntervalSec=0

[Service]
Type=oneshot
User=edgekit
ExecStart=/home/edgekit/anaconda3/envs/p310_FS_TF/bin/python /home/edgekit/MSY_FS/fahrsimulator_msy2526_ai/predict_pipeline/predict_sample.py

[Install]
WantedBy=multi-user.target
```

## Behavior

-   **Type=oneshot**\
    The service runs the script once and then exits.

-   **User=edgekit**\
    The script is executed under the `edgekit` user.

-   **ExecStart**\
    Executes the Python script using the specified conda environment.

-   **After=network.target**\
    Ensures the service only runs after the network is available.

------------------------------------------------------------------------

# Execution Flow

1.  The **timer starts** after it is enabled.
2.  After **60 seconds**, the first execution happens (this results from the duration of the camera processing initialization)
3.  The timer triggers `predict.service`.
4.  The service runs `predict_sample.py`.
5.  Once the script finishes, the service exits.
6.  The timer triggers the service again **every 5 seconds**.

------------------------------------------------------------------------

# Debugging and Monitoring

## View Live Output

All `print()` output from the Python script is written to the **systemd
journal**.

Follow the output live with:

``` bash
journalctl -u predict.service -f
```

This command is typically the most useful for debugging.

------------------------------------------------------------------------

# Common Systemd Commands

## Check Service Status

``` bash
systemctl status predict.service
```

Shows the last execution result and recent log lines.

------------------------------------------------------------------------

## Check Timer Status

``` bash
systemctl status predict.timer
```

Shows when the timer last ran and when it will run next.

------------------------------------------------------------------------

## List All Timers

``` bash
systemctl list-timers
```

Displays all active timers and their next scheduled execution.

------------------------------------------------------------------------

# Manual Execution

To run the service manually once:

``` bash
systemctl start predict.service
```

------------------------------------------------------------------------

# Restarting the Systemd Units

## Restart the Service

``` bash
systemctl restart predict.service
```

## Restart the Timer

``` bash
systemctl restart predict.timer
```

------------------------------------------------------------------------

## Reload Systemd After Changes

If `.service` or `.timer` files were modified:

``` bash
systemctl daemon-reload
systemctl restart predict.timer
systemctl restart predict.service
```

------------------------------------------------------------------------

## Enabling the Timer

To ensure the timer starts automatically on system boot:

``` bash
systemctl enable predict.timer
systemctl start predict.timer
```

------------------------------------------------------------------------

# Summary

-   `predict.timer` schedules periodic execution.
-   `predict.service` runs the Python prediction script.
-   The script runs **every 5 seconds** after the initial delay.
-   Logs and script output are available through:

``` bash
journalctl -u predict.service -f
```
