Skip to content

Android background automation

On Android, ZestSSH uses Android’s WorkManager to run scheduled tasks: a command, a snippet or a workflow on a host, on a schedule you set. This page explains how that works on Android, and how ZestSSH keeps open sessions alive in the background. To set up a task, see Scheduled tasks.

  1. On your phone, open More > Automation.
  2. Turn on External automation (it needs Squeezed).
  3. Tap Scheduled tasks. The + button adds a task.

On an iPhone the screen says “Background scheduling isn’t available on iOS” and points you to the Shortcuts app. A computer runs scheduled tasks itself from 4.0, without WorkManager, while ZestSSH is open or in the tray (see Scheduled tasks).

A task runs on one of three schedules:

  • Every N minutes: every 15 minutes, 30 minutes, 1, 2, 6 or 12 hours, or every day. 15 minutes is the shortest, because WorkManager allows no shorter repeat.
  • Daily at a time you pick.
  • Weekly on a day and at a time you pick.

Every task repeats: there is no task that runs only once. A daily task is a repeat every 24 hours and a weekly task a repeat every 7 days, the first run waiting until the time you picked. Android decides the exact moment, so a run can come later than the time set (see Battery limits).

  1. The app saves the task and hands it to WorkManager over the method channel com.affluentlabs.zestssh/scheduler, as a repeating job.
  2. When the job is due, a small worker hands the task to ZestSSH (an automation intent for Execute, Snippet or Workflow):
    • ZestSSH is on screen: the task starts at once.
    • ZestSSH is not on screen: Android does not let an app start itself from the background, so ZestSSH posts a notification, A scheduled task is ready (“Tap to run it. Android lets ZestSSH start a task by itself only while the app is open.”). Tap it to run the task. The notification stays for up to 12 hours, and a task that fires again replaces its own notification. If notifications are turned off for ZestSSH, the task does not run.
  3. ZestSSH connects to the host, runs the task and records the result in the automation history. The task’s card in Scheduled tasks shows it as its last run (Last: and the time).

A workflow task names its hosts in its own steps, so it needs no host of its own.

Some things keep this safe:

  • The API key is not stored in the job. The task keeps only the id of an automation API key. ZestSSH keeps no copy of a key itself, only a hash of it, so the run is checked by that id: the key must still be there and not expired. Delete a key, or let it expire, and its tasks stop.
  • Only ZestSSH’s own scheduler can use that id. Each run carries a one-time launch token, kept where only ZestSSH can read it and usable once: for 5 minutes when the task starts at once, or for as long as its notification is shown. Without a valid token, the key id is dropped and the run is refused. Another app on your phone cannot start a run with a key id it found.
  • Keys must allow it. A task signs in only with SSH keys that have Allow unattended runs turned on, and that goes for a jump host’s key and the key a network gives the host too. A run that is refused for this says why, with a button that opens Keys.
  • A run is answered once. Reopening ZestSSH from Recents does not run the task again.

To see whether tasks ran, open the automation history.

Sessions in the background: the foreground service

Section titled “Sessions in the background: the foreground service”

While at least one session is connected, ZestSSH runs an Android foreground service so Android keeps the app running when you switch away. Port forwards and long-running terminal sessions stay up.

  • The service does no networking itself; the app handles all SSH traffic. The service only holds an ongoing notification, which Android requires for a foreground service.
  • The notification is titled ZestSSH and says how many sessions are open, for example “One active SSH session” or “3 active SSH sessions”, in your phone’s language. Tapping it brings ZestSSH back to the front.
  • It uses a low-importance notification channel named SSH sessions (“Shown while SSH sessions are open, so they keep running”).
  • The service starts with your first connected session, updates its count as sessions open and close, and stops when none are left.
  • The service holds no wake lock of its own.

Android can delay or stop background work to save battery, and WorkManager never repeats a job more often than every 15 minutes. If tasks do not run when you expect, let ZestSSH run without battery limits in Android’s settings for the app. Phone makers add their own battery managers on top of Android and name these settings differently. Don’t Kill My App lists the steps for each maker.