Skip to content

Automation with Cron Jobs and Variables

Advanced 15 minutes

Overview

The RosettaHub CLI is designed for automation. Variables let you store and reference values across commands, cron jobs schedule recurring tasks, and startup scripts initialize machines on boot. In this tutorial you will set up variables, create scheduled jobs, and build shell scripts that leverage the CLI.

Prerequisites

Steps

Step 1: Work with Variables

Variables store reusable values that can be referenced in commands.

Set a variable:

rh var set mykey myvalue

Get a variable value:

rh var get mykey

List all variables:

rh var ls

List variable keys:

rh var keys

Delete a variable:

rh var delete mykey

Step 2: Save Command Results to Variables

Many list commands support the -v / --set flag to save results to a variable:

rh formation ls -v my-formations
rh key ls -v my-keys

You can then retrieve the stored value:

rh var get my-formations

Step 3: Use Variable Fields

Access specific fields from stored variables:

rh var fields my-formations
rh var lsf my-formations

Save variable data to a file:

rh var save my-formations

Step 4: Create a Cron Job

Schedule a recurring task:

rh cron create --label "Nightly Backup"

List scheduled jobs:

rh cron ls

Delete a cron job:

rh cron delete <cronUid>

Step 5: Create Startup Scripts

Startup scripts run automatically when a machine boots:

rh startup-script create --label "Install Tools"

List startup scripts:

rh startup-script ls

Step 6: Use --quiet and --yes for Non-Interactive Scripts

When using the CLI in shell scripts, suppress prompts and extra output:

rh formation delete <formationUid> --yes --quiet
  • --quiet (-q) suppresses informational messages
  • --yes (-y) auto-confirms destructive operations

Step 7: Use JSON Output for Scripting

Set JSON as the default output for programmatic use:

rh set-output json

Or override per command:

rh formation ls -O json | jq '.[].formationUid'
rh session ls -O json | jq '[.[] | select(.status == "running")]'
rh image ls -O json | jq 'length'

Step 8: Build a Deployment Script

Combine CLI commands into a deployment workflow:

#!/bin/bash
set -e

IMAGE_UID="your-image-uid"
KEY_UID="your-key-uid"
LABEL="Deploy-$(date +%Y%m%d)"

echo "Launching formation..."
rh image launch "$IMAGE_UID" \
  -l "$LABEL" \
  -k "$KEY_UID" \
  -g -q -y

echo "Formation is running."
rh session ls

Step 9: Use --dry-run for Safety

Preview any operation without executing:

rh formation delete <formationUid> --dry-run
rh compliance execute --portfolio-uid <uid> --cloud-account-uid <uid> --dry-run

Warning

Always test batch scripts with --dry-run first to preview what will happen.


Next Steps

Troubleshooting

Variable not found

Variable names are case-sensitive. Check with rh var keys.

Script hangs waiting for input

Add --yes to skip confirmation prompts and --quiet to suppress interactive output.

How do I run the CLI without the daemon?

The RosettaHub daemon must be running for all CLI commands. Ensure it is started before running scripts.