Skip to content

Instantly share code, notes, and snippets.

@trevorbox
Last active December 8, 2025 21:31
Show Gist options
  • Select an option

  • Save trevorbox/d963d45ab86858075a11a14692c085a4 to your computer and use it in GitHub Desktop.

Select an option

Save trevorbox/d963d45ab86858075a11a14692c085a4 to your computer and use it in GitHub Desktop.
Create a MachineConfig that runs a script on worker nodes using systemd on a schedule
variant: openshift
version: 4.20.0 # Use the appropriate OpenShift version you are running (e.g., 4.15.0, 4.16.0)
metadata:
name: 99-worker-myscript-job
labels:
machineconfiguration.openshift.io/role: worker
storage:
files:
# Create the script file
- path: /usr/local/bin/myscript.sh
mode: 0755 # Make the script executable
overwrite: true
contents:
inline: |
#!/bin/bash
echo "Systemd job ran at $(date)" >> /var/log/99-worker-myscript-job.log 2>&1
systemd:
units:
- name: myscript.service
contents: |
[Unit]
Description=My custom systemd script service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/myscript.sh
- name: myscript.timer
enabled: true
contents: |
[Unit]
Description=Run date.service every 2 minutes
[Timer]
OnCalendar=*:0/2
[Install]
WantedBy=multi-user.target
# Generated by Butane; do not edit
apiVersion: machineconfiguration.openshift.io/v1
kind: MachineConfig
metadata:
labels:
machineconfiguration.openshift.io/role: worker
name: 99-worker-myscript-job
spec:
config:
ignition:
version: 3.5.0
storage:
files:
- contents:
compression: ""
source: data:;base64,IyEvYmluL2Jhc2gKZWNobyAiU3lzdGVtZCBqb2IgcmFuIGF0ICQoZGF0ZSkiID4+IC92YXIvbG9nLzk5LXdvcmtlci1teXNjcmlwdC1qb2IubG9nIDI+JjEK
mode: 493
overwrite: true
path: /usr/local/bin/myscript.sh
systemd:
units:
- contents: |
[Unit]
Description=My custom systemd script service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/myscript.sh
name: myscript.service
- contents: |-
[Unit]
Description=Run date.service every 2 minutes
[Timer]
OnCalendar=*:0/2
[Install]
WantedBy=multi-user.target
enabled: true
name: myscript.timer

Periodically run a script using MachineConfig

Generate Maching config from butane...

butane 99-worker-myscript-job.bu -o 99-worker-myscript-job.yaml

Verify on node...

sh-5.1# systemctl status myscript 
○ myscript.service - My custom cron script service
     Loaded: loaded (/etc/systemd/system/myscript.service; static)
     Active: inactive (dead) since Tue 2025-12-02 18:00:00 UTC; 1min 20s ago
TriggeredBy: ● myscript.timer
    Process: 40599 ExecStart=/usr/local/bin/myscript.sh (code=exited, status=0/SUCCESS)
   Main PID: 40599 (code=exited, status=0/SUCCESS)
        CPU: 5ms

Dec 02 18:00:00 crc systemd[1]: Starting My custom cron script service...
Dec 02 18:00:00 crc systemd[1]: myscript.service: Deactivated successfully.
Dec 02 18:00:00 crc systemd[1]: Finished My custom cron script service.
# needed to prevent restart of node when we only need to restart systemd services
apiVersion: operator.openshift.io/v1
kind: MachineConfiguration
metadata:
name: cluster
spec:
nodeDisruptionPolicy:
units:
- actions:
- restart:
serviceName: myscript.service
type: Restart
name: myscript.service
- actions:
- restart:
serviceName: myscript.timer
type: Restart
name: myscript.timer
files:
- actions:
- restart:
serviceName: myscript.service
type: Restart
path: /usr/local/bin/myscript.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment