# Cron and workers

> Scheduled jobs and always-on background processes.

Source: https://livops.dev/en/docs/cron-workers

Not every service answers HTTP requests. For a process that reads a queue or a script that sends a report every night, there are two types: `worker` and `cron`.

## Worker

A **worker** is an always-on background process: a queue consumer, a subscriber, a Telegram bot. It gets no public URL.

```json
{
  "kind": "worker",
  "start": "python worker.py"
}
```

A worker receives databases and shared variables just like a regular service.

A worker writes to the disk and `/tmp` the same way an app does; see [Files and data](https://livops.dev/en/docs/files) for what is kept.

## Cron

A **cron job** is a command that runs on a schedule.

```json
{
  "kind": "cron",
  "start": "python job.py",
  "schedule": "*/15 * * * *"
}
```

You can also set the schedule in the cabinet. While `livops.json` names a schedule, the file wins on every deploy; take it out of the file and the cabinet's value is back.

### Schedule syntax

Standard five-field cron: `minute hour day month weekday`.

| Expression | When |
| --- | --- |
| `*/15 * * * *` | every 15 minutes |
| `0 * * * *` | at the start of every hour |
| `30 4 * * *` | every day at 04:30 UTC |
| `0 9 * * 1-5` | weekdays at 09:00 UTC |
| `@daily` | every day at midnight (UTC) |

- `*`, lists (`1,15`), ranges (`1-5`) and steps (`*/10`) work.
- Shortcuts: `@yearly`, `@annually`, `@monthly`, `@weekly`, `@daily`, `@midnight`, `@hourly`.
- Sunday is `0` or `7`.
- **Time zone is UTC.** Tashkent is UTC+5: for 09:00 in Tashkent, write `0 4 * * *`.
- The smallest interval is 1 minute.
- Not supported: a seconds field, month and day names (`JAN`, `MON`), `L` and `#`.

A cron job requires a `start` command and gets no public URL.

## Time zones

The container's clock runs on UTC. A schedule inside a bot or worker (apscheduler, node-cron, `schedule`) reads the same clock: "every day at 09:00" fires at 14:00 in Tashkent.

For Tashkent time, add `TZ=Asia/Tashkent` under **Variables** and deploy again. Or name the zone in the code, and the variable is not needed.

Python (apscheduler):

```python
from apscheduler.schedulers.asyncio import AsyncIOScheduler

scheduler = AsyncIOScheduler(timezone="Asia/Tashkent")
scheduler.add_job(send_report, "cron", hour=9, minute=0)  # 09:00 in Tashkent
scheduler.start()
```

Node (node-cron):

```js
import cron from "node-cron";

cron.schedule("0 9 * * *", sendReport, { timezone: "Asia/Tashkent" }); // 09:00 in Tashkent
```

`TZ` only changes schedules in your code. The `schedule` field of a `cron` service is still read in UTC.
