Cron jobs

Run a command inside a Hangar service on a cron schedule, in your time zone, for cleanups, reports and queue drains, with a log of every run.

Updated

A cron job runs a command inside one of your services on a schedule: clearing expired sessions every hour, sending a report every morning, draining a queue every five minutes. You add it to the service that has the code it needs; there is no separate worker to deploy.

Create a job

Open the service and go to Settings → Automations. Add a job and fill in:

  • Name and, if you like, a description.
  • Schedule. Pick a common one, such as hourly or daily, or write any cron expression, for example 0 3 * * * for every day at 03:00.
  • Time zone. The schedule follows the time zone you choose, so 09:00 stays 09:00 for your team.
  • Shell. bash or sh, depending on what the image has.
  • Command. What to run, exactly as you would type it in the container, such as npm run cleanup or python manage.py clearsessions.

Jobs can be switched off without deleting them.

How a job runs

When the schedule fires, Hangar runs the command inside the service's running container. It sees the same files, environment variables and private network as the service itself, so it can reach your databases the same way the app does.

Because the job runs in the existing container, the service has to be running. If it is stopped, the run fails and its log says that no container was found.

Docker Compose

In a Docker Compose service, choose which of the compose file's services the command runs in.

History and logs

Each job keeps a history of its runs with their logs, so you can see when it ran and what it printed. Use the run button to start a job immediately, which is the quickest way to check a new command.

Frequently asked questions

Does a cron job on Hangar start a new container?
No. The command runs inside the service's running container, with the same code, variables and network. The service has to be running when the schedule fires.
Can I run a cron job by hand to test it?
Yes. Each job has a run button that starts it right away, and the run shows up in its history with its log.

Deploy your app on Hangar

Connect a repository and it is live in minutes.

Start deploying