Module: PWN::Cron

Defined in:
lib/pwn/cron.rb

Overview

PWN::Cron provides cron / scheduled task management for the pwn-ai agent. Jobs are defined in ~/.pwn/cron/jobs.yml and can be triggered by system cron, manual run, or from within pwn-ai agent loops.

Each job can contain a prompt (for pwn-ai), a ruby script snippet, or reference to external script. Delivery can be 'log' (default), 'email', etc. (email would require additional plugins).

Constant Summary collapse

CRON_DIR =
if ENV['PWN_CRON_DIR'].to_s.strip.empty?
  File.join(Dir.home, '.pwn', 'cron')
else
  ENV.fetch('PWN_CRON_DIR', nil)
end
JOBS_FILE =
File.join(CRON_DIR, 'jobs.yml')
DEFAULT_JOBS =
Supported Method Parameters

PWN::Cron.install_defaults

Idempotently seed the RL feedback-loop cron jobs so a fresh install closes the loop by default (S1 practice + P3 offline_judge + W2 train dry-run + M1 consolidate). Re-running is a no-op if jobs with these names already exist. These are seeded to jobs.yml only — pass install_crontab: true to PWN::Cron.create yourself if you also want a system crontab entry. Hygiene jobs are enabled. Curriculum self-play / judge / LoRA are seeded disabled: practice runs Loop.run (real tools) overnight; offline_judge spends API tokens; train needs a GPU trainer.

[
  {
    name: 'curriculum_practice_nightly',
    schedule: '0 3 * * *',
    ruby: 'PWN::AI::Agent::Curriculum.practice(limit: 3) if defined?(PWN::AI::Agent::Curriculum)',
    enabled: false
  },
  {
    name: 'curriculum_train_weekly',
    schedule: '0 4 * * 0',
    ruby: 'PWN::AI::Agent::Curriculum.train_and_gate(dry_run: true) if defined?(PWN::AI::Agent::Curriculum)',
    enabled: false
  },
  {
    name: 'curriculum_offline_judge',
    schedule: '30 3 * * *',
    ruby: 'PWN::AI::Agent::Curriculum.offline_judge(since_hours: 24) if defined?(PWN::AI::Agent::Curriculum)',
    aliases: %w[offline_judge_nightly],
    enabled: false
  },
  {
    name: 'learning_consolidate_nightly',
    schedule: '0 5 * * *',
    ruby: 'PWN::AI::Agent::Learning.consolidate if defined?(PWN::AI::Agent::Learning)',
    enabled: true
  },
  {
    name: 'pwn_stores_lean_nightly',
    schedule: '15 5 * * *',
    ruby: 'PWN::AI::Agent::Learning.gc_stores! if defined?(PWN::AI::Agent::Learning)',
    enabled: true
  }
].freeze
DEFAULT_INTERVAL =

In-process scheduler. System crontab is optional (install_crontab) and install_defaults never writes it, so last_run only moves when something ticks jobs.yml. The worker is that something: poll once or loop, fire due enabled jobs via PWN::Cron.run, and stay alive behind a pidfile so pwn setup can start / restart it.

Spawned workers honor PWN_CRON_DIR so tests can sandbox the daemon without touching the operator's real ~/.pwn/cron.

60
DEFAULT_LOCK_STALE =
7_200
PID_FILE =

Kept for callers / older snippets. Prefer the methods above.

File.join(CRON_DIR, 'worker.pid')
WORKER_LOG =
File.join(CRON_DIR, 'worker.log')
SYSTEMD_WORKER_UNIT =

OS-agnostic native persistence. jobs.yml remains the source of truth (5-field cron + relative "every 30m"). The in-process worker evaluates due? and fires jobs. A native backend only (a) restarts that worker after reboot / login and (b) optionally plants a calendar trigger per 5-field job so the schedule still fires if the worker is down.

linux -> systemd --user (Restart=always) else crontab @reboot osx -> launchd LaunchAgent (RunAtLoad + KeepAlive) windows -> schtasks ONLOGON *bsd / cygwin -> crontab @reboot when crontab(1) exists else -> in-process worker only (lives with the login)

'pwn-cron-worker.service'
LAUNCHD_WORKER_LABEL =
'com.0dayinc.pwn.cron.worker'
SCHTASKS_WORKER_NAME =
'PWN\CronWorker'

Class Method Summary collapse

Class Method Details

.apply_native?(opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


782
783
784
785
786
787
788
# File 'lib/pwn/cron.rb', line 782

public_class_method def self.apply_native?(opts = {})
  return opts[:apply] if opts.key?(:apply)
  return false unless ENV['PWN_CRON_DIR'].to_s.empty?

  default = File.join(Dir.home, '.pwn', 'cron')
  File.expand_path(cron_dir.to_s) == File.expand_path(default)
end

.authorsObject

Author(s)

0day Inc. [email protected]



1515
1516
1517
# File 'lib/pwn/cron.rb', line 1515

public_class_method def self.authors
  "AUTHOR(S):\n  0day Inc. <[email protected]>\n"
end

.create(opts = {}) ⇒ Object

Supported Method Parameters

job = PWN::Cron.create( name: 'optional', schedule: 'required e.g. "0 * * * *" or "30m" or "every 2h"', prompt: 'optional - pwn-ai prompt to run', ruby: 'optional - ruby snippet to eval', script: 'optional - path to external script', delivery: 'log|stdout (default log)', enabled: true )



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pwn/cron.rb', line 50

public_class_method def self.create(opts = {})
  jobs = load_jobs
  id = SecureRandom.hex(6)
  name = opts[:name] || "job-#{id}"
  job = {
    id: id,
    name: name,
    schedule: opts[:schedule] || '0 * * * *',
    prompt: opts[:prompt],
    ruby: opts[:ruby],
    script: opts[:script],
    delivery: opts[:delivery] || 'log',
    enabled: opts.fetch(:enabled, true),
    created_at: Time.now.utc.iso8601,
    last_run: nil,
    last_status: nil
  }
  jobs[id] = job
  save_jobs(jobs: jobs)

  # Optionally install a crontab entry (user must have permission)
  install_crontab_entry(job: job) if opts[:install_crontab]
  sync_one_job(job: job) if cron_enabled? && !opts[:install_crontab]

  job
end

.cron_dirObject

Supported Method Parameters

dir = PWN::Cron.cron_dir



29
30
31
32
# File 'lib/pwn/cron.rb', line 29

public_class_method def self.cron_dir
  FileUtils.mkdir_p(CRON_DIR)
  CRON_DIR
end

.cron_enabled?Boolean

Returns:

  • (Boolean)


767
768
769
# File 'lib/pwn/cron.rb', line 767

public_class_method def self.cron_enabled?
  scheduler_config[:enabled] != false
end

.cron_to_calendar(opts = {}) ⇒ Object

Convert a 5-field cron expression into a portable calendar hash. Returns nil for relative schedules or expressions we cannot map.



889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
# File 'lib/pwn/cron.rb', line 889

public_class_method def self.cron_to_calendar(opts = {})
  schedule = opts[:schedule].to_s.strip
  return nil if relative_seconds(schedule: schedule)

  fields = schedule.split
  return nil unless fields.length == 5

  min, hour, mday, mon, wday = fields
  return nil unless min.match?(/\A\d+\z/) && hour.match?(/\A\d+\z/)
  return nil unless mon == '*'

  minute = min.to_i
  hr = hour.to_i
  return nil unless minute.between?(0, 59) && hr.between?(0, 23)

  if mday == '*' && wday == '*'
    { kind: :daily, minute: minute, hour: hr }
  elsif mday == '*' && wday.match?(/\A[0-7]\z/)
    wd = wday.to_i
    wd = 0 if wd == 7
    { kind: :weekly, minute: minute, hour: hr, wday: wd }
  elsif wday == '*' && mday.match?(/\A\d+\z/)
    { kind: :monthly, minute: minute, hour: hr, mday: mday.to_i }
  end
end

.crontab_available?Boolean

Returns:

  • (Boolean)


804
805
806
# File 'lib/pwn/cron.rb', line 804

public_class_method def self.crontab_available?
  !which_bin(name: 'crontab').empty?
end

.disable(opts = {}) ⇒ Object



159
160
161
# File 'lib/pwn/cron.rb', line 159

public_class_method def self.disable(opts = {})
  toggle(id: opts[:id], enabled: false)
end

.due?(opts = {}) ⇒ Boolean

Supported Method Parameters

PWN::Cron.due?( schedule: 'required - 5-field cron or relative ("30m", "every 2h")', last_run: 'optional - Time / iso8601 / nil', now: 'optional - Time (default Time.now)' )

Returns:

  • (Boolean)


338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/pwn/cron.rb', line 338

public_class_method def self.due?(opts = {})
  schedule = opts[:schedule].to_s.strip
  return false if schedule.empty?

  now = opts[:now] || Time.now
  last = parse_time(value: opts[:last_run])

  if (secs = relative_seconds(schedule: schedule))
    return true if last.nil?

    return (now - last) >= secs
  end

  fields = schedule.split
  return false unless fields.length == 5

  prev = last || (now - (24 * 60 * 60))
  # Walk minute-aligned slots from the minute AFTER last_run
  # (or the last 24h if never run) up to `now`.
  t = Time.at(((prev.to_i / 60) + 1) * 60)
  t = Time.local(t.year, t.month, t.day, t.hour, t.min, 0)
  limit = Time.local(now.year, now.month, now.day, now.hour, now.min, 0)
  safety = 0
  while t <= limit && safety < 10_080
    return true if cron_match?(fields: fields, time: t)

    t += 60
    safety += 1
  end
  false
rescue StandardError
  false
end

.due_jobs(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Cron.due_jobs(now: Time)



374
375
376
377
378
379
380
381
382
383
# File 'lib/pwn/cron.rb', line 374

public_class_method def self.due_jobs(opts = {})
  now = opts[:now] || Time.now
  load_jobs.each_with_object({}) do |(id, job), acc|
    next unless job.is_a?(Hash)
    next unless job[:enabled]
    next unless due?(schedule: job[:schedule], last_run: job[:last_run], now: now)

    acc[id] = job
  end
end

.enable(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Cron.enable/disable(id:)



155
156
157
# File 'lib/pwn/cron.rb', line 155

public_class_method def self.enable(opts = {})
  toggle(id: opts[:id], enabled: true)
end

.ensure_worker(opts = {}) ⇒ Object

PWN::Setup hook — start (or restart) the background worker after any setup action so YAML-only default jobs actually fire.



539
540
541
542
543
544
545
# File 'lib/pwn/cron.rb', line 539

public_class_method def self.ensure_worker(opts = {})
  start_worker(
    interval: opts[:interval] || DEFAULT_INTERVAL,
    restart: opts.fetch(:restart, false),
    foreground: false
  )
end

.helpObject

Display Usage for this Module



1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
# File 'lib/pwn/cron.rb', line 1520

public_class_method def self.help
  puts "USAGE:
    # Run cron dir and return its result
    #{self}.cron_dir

    # Run list and return its result
    #{self}.list

    # Run create and return its result
    #{self}.create(
      name: 'optional - binary or identifier name (defaults to job-)',
      schedule: 'required - required e.g. 0 * * * * or 30m or every 2h',
      prompt: 'optional - pwn-ai prompt to run',
      ruby: 'optional - ruby snippet to eval',
      script: 'optional - path to external script',
      delivery: 'optional - log|stdout (default log)',
      enabled: 'optional - enabled value consumed by #create',
      install_crontab: 'optional - install crontab value consumed by #create'
    )

    # Executes the job (for pwn-ai prompt it will use current active AI engine
    #{self}.run(
      id: 'required - id value consumed by #run'
    )

    # Run remove and return its result
    #{self}.remove(
      id: 'optional - id value consumed by #remove'
    )

    # Run enable and return its result
    #{self}.enable(
      id: 'optional - id value consumed by #enable'
    )

    # Run disable and return its result
    #{self}.disable(
      id: 'optional - id value consumed by #disable'
    )

    # Install a crontab line that invokes this job via pwn
    #{self}.install_crontab_entry

    # Run install defaults and return its result
    #{self}.install_defaults

    # Runtime paths follow cron_dir (and therefore a stubbed CRON_DIR)
    #{self}.jobs_file

    # Run pid file and return its result
    #{self}.pid_file

    # Run worker log and return its result
    #{self}.worker_log

    # Run due and return its result
    #{self}.due?(
      schedule: 'required - 5-field cron or relative (30m, every 2h)',
      last_run: 'optional - Time / iso8601 / nil',
      now: 'optional - Time (default Time.now)'
    )

    # Run due jobs and return its result
    #{self}.due_jobs(
      now: 'optional - now value consumed by #due_jobs (defaults to Time.now)'
    )

    # Fires every currently-due enabled job via run(). Returns the list
    #{self}.run_due(
      now: 'optional - now value consumed by #run_due (defaults to Time.now)',
      jobs: 'optional - jobs value consumed by #run_due'
    )

    # Run worker status and return its result
    #{self}.worker_status

    # Idempotent. With restart:true (default) a live worker is replaced
    #{self}.start_worker(
      interval: 'optional - interval value consumed by #start_worker',
      foreground: 'optional - foreground value consumed by #start_worker',
      restart: 'optional - restart value consumed by #start_worker (defaults to foreground)'
    )

    # Run stop worker and return its result
    #{self}.stop_worker

    # Single tick used by the loop AND by tests (no sleep)
    #{self}.tick(
      now: 'optional - now value consumed by #tick'
    )

    # Blocking poll loop. `once: true` runs a single tick then returns
    #{self}.worker_loop(
      interval: 'optional - interval value consumed by #worker_loop',
      once: 'optional - once value consumed by #worker_loop'
    )

    # any setup action so YAML-only default jobs actually fire
    #{self}.ensure_worker(
      interval: 'optional - interval value consumed by #ensure_worker (defaults to DEFAULT_INTERVAL)'
    )

    # Append an @reboot line so the worker comes back after a reboot
    #{self}.install_worker_crontab

    # Run os type and return its result
    #{self}.os_type

    # Run windows and return its result
    #{self}.windows?

    # Run scheduler file and return its result
    #{self}.scheduler_file

    # Run scheduler config and return its result
    #{self}.scheduler_config

    # Run cron enabled and return its result
    #{self}.cron_enabled?

    # Run persist scheduler config and return its result
    #{self}.persist_scheduler_config(
      backend: 'optional - backend value consumed by #persist_scheduler_config'
    )

    # Run apply native and return its result
    #{self}.apply_native?(
      apply: 'optional - apply value consumed by #apply_native?'
    )

    # Run which bin and return its result
    #{self}.which_bin(
      name: 'required - binary or identifier name'
    )

    # Run crontab available and return its result
    #{self}.crontab_available?

    # Run systemd user available and return its result
    #{self}.systemd_user_available?

    # Run schtasks available and return its result
    #{self}.schtasks_available?

    # Run scheduler backend and return its result
    #{self}.scheduler_backend(
      backend: 'optional - backend value consumed by #scheduler_backend',
      os: 'optional - os value consumed by #scheduler_backend'
    )

    # Run native unit dir and return its result
    #{self}.native_unit_dir(
      backend: 'optional - backend value consumed by #native_unit_dir'
    )

    # Run ruby lib dir and return its result
    #{self}.ruby_lib_dir

    # Run ruby eval argv and return its result
    #{self}.ruby_eval_argv(
      snippet: 'optional - snippet value consumed by #ruby_eval_argv'
    )

    # Run ruby eval shell and return its result
    #{self}.ruby_eval_shell(
      log: 'optional - log value consumed by #ruby_eval_shell'
    )

    # Convert a 5-field cron expression into a portable calendar hash
    #{self}.cron_to_calendar(
      schedule: 'optional - schedule value consumed by #cron_to_calendar'
    )

    # Run systemd on calendar and return its result
    #{self}.systemd_on_calendar(
      calendar: 'optional - calendar value consumed by #systemd_on_calendar',
      schedule: 'optional - schedule value consumed by #systemd_on_calendar'
    )

    # Run launchd calendar interval and return its result
    #{self}.launchd_calendar_interval(
      calendar: 'optional - calendar value consumed by #launchd_calendar_interval',
      schedule: 'optional - schedule value consumed by #launchd_calendar_interval'
    )

    # Run schtasks spec and return its result
    #{self}.schtasks_spec(
      calendar: 'optional - calendar value consumed by #schtasks_spec',
      schedule: 'optional - schedule value consumed by #schtasks_spec'
    )

    # Run install scheduler and return its result
    #{self}.install_scheduler(
      enabled: 'optional - enabled value consumed by #install_scheduler',
      backend: 'optional - optional force',
      apply: 'optional - optional (default true unless PWN_CRON_DIR is set',
      sync_jobs: 'optional - sync jobs value consumed by #install_scheduler'
    )

    # Run uninstall scheduler and return its result
    #{self}.uninstall_scheduler(
      backend: 'optional - backend value consumed by #uninstall_scheduler',
      persist: 'optional - persist value consumed by #uninstall_scheduler'
    )

    # Run sync scheduler and return its result
    #{self}.sync_scheduler

    # Run scheduler status and return its result
    #{self}.scheduler_status

    # Run install native job and return its result
    #{self}.install_native_job(
      job: 'optional - job value consumed by #install_native_job',
      backend: 'optional - backend value consumed by #install_native_job'
    )

    # Run remove native job and return its result
    #{self}.remove_native_job(
      job: 'optional - job value consumed by #remove_native_job',
      id: 'optional - id value consumed by #remove_native_job',
      name: 'optional - binary or identifier name',
      backend: 'optional - backend value consumed by #remove_native_job'
    )

    # Run sync native jobs and return its result
    #{self}.sync_native_jobs(
      backend: 'optional - backend value consumed by #sync_native_jobs'
    )

    # ---- crontab -------------------------------------------------------
    #{self}.install_crontab_worker

    # Run install crontab job and return its result
    #{self}.install_crontab_job(
      job: 'optional - job value consumed by #install_crontab_job'
    )

    # Run uninstall crontab and return its result
    #{self}.uninstall_crontab

    # Run uninstall crontab job and return its result
    #{self}.uninstall_crontab_job(
      job: 'optional - job value consumed by #uninstall_crontab_job'
    )

    # ---- systemd --user ------------------------------------------------
    #{self}.install_systemd_user_worker

    # Run install systemd user job and return its result
    #{self}.install_systemd_user_job(
      job: 'optional - job value consumed by #install_systemd_user_job'
    )

    # Run uninstall systemd user and return its result
    #{self}.uninstall_systemd_user

    # Run uninstall systemd user job and return its result
    #{self}.uninstall_systemd_user_job(
      job: 'optional - job value consumed by #uninstall_systemd_user_job'
    )

    # ---- launchd -------------------------------------------------------
    #{self}.install_launchd_worker

    # Run install launchd job and return its result
    #{self}.install_launchd_job(
      job: 'optional - job value consumed by #install_launchd_job'
    )

    # Run uninstall launchd and return its result
    #{self}.uninstall_launchd

    # Run uninstall launchd job and return its result
    #{self}.uninstall_launchd_job(
      job: 'optional - job value consumed by #uninstall_launchd_job'
    )

    # ---- schtasks (Windows) -------------------------------------------
    #{self}.install_schtasks_worker

    # Run install schtasks job and return its result
    #{self}.install_schtasks_job(
      job: 'optional - job value consumed by #install_schtasks_job'
    )

    # Run uninstall schtasks and return its result
    #{self}.uninstall_schtasks

    # Run uninstall schtasks job and return its result
    #{self}.uninstall_schtasks_job(
      job: 'optional - job value consumed by #uninstall_schtasks_job'
    )

    # Print the AUTHOR(S) string for this module.
    #{self}.authors
  "
  constants.sort
end

.install_crontab_entry(opts = {}) ⇒ Object

Install a crontab line that invokes this job via pwn (assumes /opt/pwn and the active rvm ruby@pwn gemset - user can edit crontab)



165
166
167
# File 'lib/pwn/cron.rb', line 165

public_class_method def self.install_crontab_entry(opts = {})
  install_native_job(opts.merge(backend: :crontab))
end

.install_crontab_job(opts = {}) ⇒ Object



1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# File 'lib/pwn/cron.rb', line 1091

public_class_method def self.install_crontab_job(opts = {})
  job = opts[:job]
  apply = apply_native?(opts)
  snippet = "require \"pwn\"; PWN::Cron.run(id: #{job[:id].to_s.inspect})"
  line = "#{job[:schedule]} #{ruby_eval_shell(snippet: snippet, log: File.join(cron_dir, 'cron.log'))}"
  block = "# pwn-cron #{job[:name]} (#{job[:id]})\n#{line}\n"
  write_native_preview(name: "job-#{job[:id]}.crontab", body: block)
  return { backend: :crontab, apply: false, line: line, id: job[:id] } unless apply && crontab_available?

  existing = crontab_read
  return { backend: :crontab, already: true, id: job[:id], line: line } if existing.include?(job[:id].to_s)

  crontab_write(body: "#{existing.rstrip}\n#{block}")
  { backend: :crontab, apply: true, id: job[:id], line: line }
end

.install_crontab_worker(opts = {}) ⇒ Object

---- crontab -------------------------------------------------------



1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
# File 'lib/pwn/cron.rb', line 1076

public_class_method def self.install_crontab_worker(opts = {})
  apply = apply_native?(opts)
  marker = 'PWN::Cron.ensure_worker'
  line = "@reboot #{ruby_eval_shell(snippet: 'require "pwn"; PWN::Cron.ensure_worker(restart: false)', log: worker_log)}"
  preview = "# pwn-cron worker\n#{line}\n"
  write_native_preview(name: 'worker.crontab', body: preview)
  return { backend: :crontab, apply: false, line: line } unless apply && crontab_available?

  existing = crontab_read
  return { backend: :crontab, already: true, line: line } if existing.include?(marker)

  crontab_write(body: "#{existing.rstrip}\n#{preview}")
  { backend: :crontab, apply: true, line: line }
end

.install_defaultsObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/pwn/cron.rb', line 263

public_class_method def self.install_defaults
  jobs = load_jobs
  names = jobs.values.map { |j| j[:name].to_s }
  seeded = []

  DEFAULT_JOBS.each do |spec|
    aliases = Array(spec[:aliases]).map(&:to_s)
    known = [spec[:name].to_s, *aliases]
    next if names.intersect?(known)

    seeded << create(
      name: spec[:name],
      schedule: spec[:schedule],
      ruby: spec[:ruby],
      delivery: 'log',
      enabled: spec.fetch(:enabled, true)
    )
    names << spec[:name].to_s
  end

  # P13 - disable the legacy alias when both occupy the 30 3 * * * slot.
  if names.include?('curriculum_offline_judge') && names.include?('offline_judge_nightly')
    begin
      dup = jobs.values.find { |j| j[:name].to_s == 'offline_judge_nightly' }
      disable(id: dup[:id]) if dup && dup[:enabled]
    rescue StandardError
      nil
    end
  end

  seeded
rescue StandardError => e
  warn("[PWN::Cron] install_defaults failed: #{e.class}: #{e.message}")
  []
end

.install_launchd_job(opts = {}) ⇒ Object



1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
# File 'lib/pwn/cron.rb', line 1250

public_class_method def self.install_launchd_job(opts = {})
  job = opts[:job]
  apply = apply_native?(opts)
  ints = launchd_calendar_interval(schedule: job[:schedule])
  return { backend: :launchd, id: job[:id], skipped: true, reason: :no_calendar } unless ints

  label = launchd_job_label(job: job)
  argv = ruby_eval_argv(snippet: "require \"pwn\"; PWN::Cron.run(id: #{job[:id].to_s.inspect})")
  plist = launchd_plist(label: label, argv: argv, keepalive: false, run_at_load: false, calendar: ints)
  path = write_unit_file(backend: :launchd, name: "#{label}.plist", body: plist, apply: apply)
  launchctl_load(path: path, label: label) if apply
  { backend: :launchd, id: job[:id], label: label, path: path, calendar: ints, apply: apply }
end

.install_launchd_worker(opts = {}) ⇒ Object

---- launchd -------------------------------------------------------



1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
# File 'lib/pwn/cron.rb', line 1236

public_class_method def self.install_launchd_worker(opts = {})
  apply = apply_native?(opts)
  label = LAUNCHD_WORKER_LABEL
  plist = launchd_plist(
    label: label,
    argv: ruby_eval_argv(snippet: 'require "pwn"; PWN::Cron.start_worker(restart: false, foreground: true)'),
    keepalive: true,
    run_at_load: true
  )
  path = write_unit_file(backend: :launchd, name: "#{label}.plist", body: plist, apply: apply)
  launchctl_load(path: path, label: label) if apply
  { backend: :launchd, label: label, path: path, apply: apply }
end

.install_native_job(opts = {}) ⇒ Object



1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
# File 'lib/pwn/cron.rb', line 1031

public_class_method def self.install_native_job(opts = {})
  job = opts[:job]
  return nil unless job.is_a?(Hash) && job[:id]

  backend = (opts[:backend] || scheduler_backend).to_sym
  apply = apply_native?(opts)
  case backend
  when :crontab then install_crontab_job(job: job, apply: apply)
  when :systemd_user then install_systemd_user_job(job: job, apply: apply)
  when :launchd then install_launchd_job(job: job, apply: apply)
  when :schtasks then install_schtasks_job(job: job, apply: apply)
  else
    { backend: backend, skipped: true, reason: :worker_only }
  end
end

.install_scheduler(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Cron.install_scheduler( enabled: true, backend: optional force, apply: optional (default true unless PWN_CRON_DIR is set), sync_jobs: true )



959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# File 'lib/pwn/cron.rb', line 959

public_class_method def self.install_scheduler(opts = {})
  enabled = opts.fetch(:enabled, true)
  return uninstall_scheduler(opts) unless enabled

  backend = scheduler_backend(opts)
  apply = apply_native?(opts)
  i[systemd_user launchd schtasks crontab].each do |other|
    next if other == backend

    uninstall_scheduler(backend: other, apply: apply, persist: false, stop: false)
  end

  persist_scheduler_config(enabled: true, backend: backend)
  result = case backend
           when :systemd_user then install_systemd_user_worker(apply: apply)
           when :launchd then install_launchd_worker(apply: apply)
           when :schtasks then install_schtasks_worker(apply: apply)
           when :crontab then install_crontab_worker(apply: apply)
           else
             { backend: :worker, persisted: false, note: 'in-process worker only' }
           end

  jobs_sync = if opts.fetch(:sync_jobs, true)
                sync_native_jobs(backend: backend, apply: apply)
              else
                []
              end
  { ok: true, backend: backend, apply: apply, worker: result, jobs: jobs_sync }
rescue StandardError => e
  warn("[PWN::Cron] install_scheduler failed: #{e.class}: #{e.message}")
  { ok: false, error: "#{e.class}: #{e.message}" }
end

.install_schtasks_job(opts = {}) ⇒ Object



1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
# File 'lib/pwn/cron.rb', line 1301

public_class_method def self.install_schtasks_job(opts = {})
  job = opts[:job]
  apply = apply_native?(opts)
  spec = schtasks_spec(schedule: job[:schedule])
  return { backend: :schtasks, id: job[:id], skipped: true, reason: :no_calendar } unless spec

  name = schtasks_job_name(job: job)
  tr = ruby_eval_shell(snippet: "require \"pwn\"; PWN::Cron.run(id: #{job[:id].to_s.inspect})")
  argv = ['schtasks', '/Create', '/F', '/TN', name, '/SC', spec[:sc], '/ST', spec[:st], '/RL', 'LIMITED', '/TR', tr]
  argv += ['/D', spec[:d]] if spec[:d]
  write_native_preview(name: "job-#{job[:id]}.schtasks.txt", body: argv.inspect)
  run_argv(argv: argv) if apply && schtasks_available?
  { backend: :schtasks, id: job[:id], name: name, spec: spec, apply: apply }
end

.install_schtasks_worker(opts = {}) ⇒ Object

---- schtasks (Windows) -------------------------------------------



1292
1293
1294
1295
1296
1297
1298
1299
# File 'lib/pwn/cron.rb', line 1292

public_class_method def self.install_schtasks_worker(opts = {})
  apply = apply_native?(opts)
  tr = ruby_eval_shell(snippet: 'require "pwn"; PWN::Cron.ensure_worker(restart: false)')
  argv = ['schtasks', '/Create', '/F', '/TN', SCHTASKS_WORKER_NAME, '/SC', 'ONLOGON', '/RL', 'LIMITED', '/TR', tr]
  write_native_preview(name: 'worker.schtasks.txt', body: argv.inspect)
  run_argv(argv: argv) if apply && schtasks_available?
  { backend: :schtasks, name: SCHTASKS_WORKER_NAME, tr: tr, apply: apply }
end

.install_systemd_user_job(opts = {}) ⇒ Object



1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
# File 'lib/pwn/cron.rb', line 1161

public_class_method def self.install_systemd_user_job(opts = {})
  job = opts[:job]
  apply = apply_native?(opts)
  cal = cron_to_calendar(schedule: job[:schedule])
  oncal = systemd_on_calendar(calendar: cal)
  return { backend: :systemd_user, id: job[:id], skipped: true, reason: :no_calendar } unless oncal

  svc = systemd_job_service(job: job)
  tmr = systemd_job_timer(job: job)
  snippet = "require \"pwn\"; PWN::Cron.run(id: #{job[:id].to_s.inspect})"
  exec_start = ruby_eval_shell(snippet: snippet)
  svc_body = "    [Unit]\n    Description=PWN cron job \#{job[:name]} (\#{job[:id]})\n\n    [Service]\n    Type=oneshot\n    ExecStart=\#{exec_start}\n    Environment=PWN_CRON_DIR=\#{cron_dir}\n  UNIT\n  tmr_body = <<~UNIT\n    [Unit]\n    Description=PWN cron timer \#{job[:name]} (\#{job[:schedule]})\n\n    [Timer]\n    OnCalendar=\#{oncal}\n    Persistent=true\n    Unit=\#{svc}\n\n    [Install]\n    WantedBy=timers.target\n  UNIT\n  svc_path = write_unit_file(backend: :systemd_user, name: svc, body: svc_body, apply: apply)\n  tmr_path = write_unit_file(backend: :systemd_user, name: tmr, body: tmr_body, apply: apply)\n  if apply\n    systemd_user(args: ['daemon-reload'])\n    systemd_user(args: ['enable', '--now', tmr])\n  end\n  { backend: :systemd_user, id: job[:id], service: svc, timer: tmr, on_calendar: oncal,\n    paths: [svc_path, tmr_path], apply: apply }\nend\n"

.install_systemd_user_worker(opts = {}) ⇒ Object

---- systemd --user ------------------------------------------------



1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'lib/pwn/cron.rb', line 1131

public_class_method def self.install_systemd_user_worker(opts = {})
  apply = apply_native?(opts)
  exec_start = ruby_eval_shell(
    snippet: 'require "pwn"; PWN::Cron.start_worker(restart: false, foreground: true)'
  )
  body = "    [Unit]\n    Description=PWN cron worker (evaluates ~/.pwn/cron/jobs.yml schedules)\n    After=default.target\n\n    [Service]\n    Type=simple\n    ExecStart=\#{exec_start}\n    Restart=always\n    RestartSec=5\n    Environment=PWN_CRON_DIR=\#{cron_dir}\n    WorkingDirectory=\#{ruby_lib_dir}\n\n    [Install]\n    WantedBy=default.target\n  UNIT\n  path = write_unit_file(backend: :systemd_user, name: SYSTEMD_WORKER_UNIT, body: body, apply: apply)\n  if apply\n    systemd_user(args: ['daemon-reload'])\n    systemd_user(args: ['enable', '--now', SYSTEMD_WORKER_UNIT])\n    enable_linger\n  end\n  { backend: :systemd_user, unit: SYSTEMD_WORKER_UNIT, path: path, apply: apply }\nend\n"

.install_worker_crontab(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Cron.install_worker_crontab

Append an @reboot line so the worker comes back after a reboot. Idempotent. Does not remove existing per-job crontab lines.



551
552
553
554
555
556
# File 'lib/pwn/cron.rb', line 551

public_class_method def self.install_worker_crontab(opts = {})
  install_crontab_worker(opts)
rescue StandardError => e
  warn("[PWN::Cron] install_worker_crontab failed: #{e.class}: #{e.message}")
  nil
end

.jobs_fileObject

Supported Method Parameters

PWN::Cron.jobs_file / PWN::Cron.pid_file / PWN::Cron.worker_log

Runtime paths follow cron_dir (and therefore a stubbed CRON_DIR).



316
317
318
# File 'lib/pwn/cron.rb', line 316

public_class_method def self.jobs_file
  File.join(cron_dir, 'jobs.yml')
end

.launchd_calendar_interval(opts = {}) ⇒ Object



929
930
931
932
933
934
935
936
937
# File 'lib/pwn/cron.rb', line 929

public_class_method def self.launchd_calendar_interval(opts = {})
  cal = opts[:calendar] || cron_to_calendar(schedule: opts[:schedule])
  return nil unless cal

  ints = { 'Hour' => cal[:hour], 'Minute' => cal[:minute] }
  ints['Weekday'] = cal[:wday] if cal[:kind] == :weekly
  ints['Day'] = cal[:mday] if cal[:kind] == :monthly
  ints
end

.listObject

Supported Method Parameters

jobs = PWN::Cron.list



36
37
38
# File 'lib/pwn/cron.rb', line 36

public_class_method def self.list
  load_jobs
end

.native_unit_dir(opts = {}) ⇒ Object



851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'lib/pwn/cron.rb', line 851

public_class_method def self.native_unit_dir(opts = {})
  backend = (opts[:backend] || scheduler_backend).to_sym
  sandboxed = !ENV['PWN_CRON_DIR'].to_s.empty? || !apply_native?(opts)
  return File.join(cron_dir, 'native') if sandboxed

  case backend
  when :launchd
    File.join(Dir.home, 'Library', 'LaunchAgents')
  when :systemd_user
    File.join(Dir.home, '.config', 'systemd', 'user')
  else
    File.join(cron_dir, 'native')
  end
end

.os_typeObject



721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/pwn/cron.rb', line 721

public_class_method def self.os_type
  t = begin
    PWN::Plugins::DetectOS.type
  rescue StandardError
    nil
  end
  return t if t

  host = RbConfig::CONFIG['host_os'].to_s.downcase
  return :windows if host.match?(/mswin|mingw|bccwin|wince|emc/)
  return :cygwin if host.include?('cygwin')
  return :osx if host.include?('darwin')
  return :linux if host.include?('linux')
  return :freebsd if host.include?('freebsd')
  return :netbsd if host.include?('netbsd')
  return :openbsd if host.include?('openbsd')

  :unknown
end

.persist_scheduler_config(opts = {}) ⇒ Object



771
772
773
774
775
776
777
778
779
780
# File 'lib/pwn/cron.rb', line 771

public_class_method def self.persist_scheduler_config(opts = {})
  cfg = scheduler_config.merge(
    enabled: opts.fetch(:enabled, cron_enabled?),
    backend: (opts[:backend] || scheduler_config[:backend])&.to_sym,
    updated_at: Time.now.utc.iso8601
  )
  FileUtils.mkdir_p(cron_dir)
  File.write(scheduler_file, YAML.dump(cfg))
  cfg
end

.pid_fileObject



320
321
322
# File 'lib/pwn/cron.rb', line 320

public_class_method def self.pid_file
  File.join(cron_dir, 'worker.pid')
end

.remove(opts = {}) ⇒ Object

rubocop:disable Naming/PredicateMethod



144
145
146
147
148
149
150
151
# File 'lib/pwn/cron.rb', line 144

public_class_method def self.remove(opts = {}) # rubocop:disable Naming/PredicateMethod
  id = opts[:id].to_s
  jobs = load_jobs
  gone = jobs.delete(id)
  save_jobs(jobs: jobs)
  remove_native_job(job: gone) if gone
  true
end

.remove_native_job(opts = {}) ⇒ Object



1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
# File 'lib/pwn/cron.rb', line 1047

public_class_method def self.remove_native_job(opts = {})
  job = opts[:job] || { id: opts[:id], name: opts[:name] }
  backend = (opts[:backend] || scheduler_backend).to_sym
  apply = apply_native?(opts)
  case backend
  when :crontab then uninstall_crontab_job(job: job, apply: apply)
  when :systemd_user then uninstall_systemd_user_job(job: job, apply: apply)
  when :launchd then uninstall_launchd_job(job: job, apply: apply)
  when :schtasks then uninstall_schtasks_job(job: job, apply: apply)
  else
    { backend: backend, skipped: true }
  end
end

.ruby_eval_argv(opts = {}) ⇒ Object



870
871
872
873
# File 'lib/pwn/cron.rb', line 870

public_class_method def self.ruby_eval_argv(opts = {})
  snippet = opts[:snippet].to_s
  [RbConfig.ruby, '-I', ruby_lib_dir, '-e', snippet]
end

.ruby_eval_shell(opts = {}) ⇒ Object



875
876
877
878
879
880
881
882
883
884
885
# File 'lib/pwn/cron.rb', line 875

public_class_method def self.ruby_eval_shell(opts = {})
  argv = ruby_eval_argv(opts)
  log = opts[:log]
  if windows?
    cmd = argv.map { |a| /[\s"]/.match?(a) ? "\"#{a.gsub('"', '""')}\"" : a }.join(' ')
    log ? "#{cmd} >> #{log} 2>&1" : cmd
  else
    cmd = Shellwords.join(argv)
    log ? "#{cmd} >> #{Shellwords.escape(log.to_s)} 2>&1" : cmd
  end
end

.ruby_lib_dirObject



866
867
868
# File 'lib/pwn/cron.rb', line 866

public_class_method def self.ruby_lib_dir
  File.expand_path('..', __dir__)
end

.run(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Cron.run(id: 'required or name') Executes the job (for pwn-ai prompt it will use current active AI engine via PWN::AI::* but without full REPL hook unless in pwn-ai).



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pwn/cron.rb', line 81

public_class_method def self.run(opts = {})
  id = opts[:id].to_s
  jobs = load_jobs
  job = jobs[id] || jobs.values.find { |j| j[:name] == id || j[:id] == id }
  raise "Job #{id} not found" unless job

  start = Time.now
  result = nil
  status = 'success'

  begin
    if job[:prompt]
      engine = begin
        PWN::Env[:ai][:active].to_s.downcase.to_sym
      rescue StandardError
        :grok
      end
      case engine
      when :grok
        result = PWN::AI::Grok.chat(request: job[:prompt], spinner: false)
      when :ollama
        result = PWN::AI::Ollama.chat(request: job[:prompt], spinner: false)
      when :openai
        result = PWN::AI::OpenAI.chat(request: job[:prompt], spinner: false)
      when :anthropic
        result = PWN::AI::Anthropic.chat(request: job[:prompt], spinner: false)
      when :gemini
        result = PWN::AI::Gemini.chat(request: job[:prompt], spinner: false)
      end
      result = begin
        result[:choices].last[:content]
      rescue StandardError
        result.to_s
      end
    elsif job[:ruby]
      result = eval(job[:ruby], TOPLEVEL_BINDING) # rubocop:disable Security/Eval
    elsif job[:script] && File.exist?(job[:script])
      result = `#{job[:script]} 2>&1`
    else
      result = 'No prompt/ruby/script defined'
    end

    if job[:delivery] == 'log'
      log_path = File.join(cron_dir, "#{job[:id]}.log")
      File.open(log_path, 'a') do |f|
        f.puts("[#{Time.now}] RUN #{job[:name]} (#{job[:id]})\n#{result}\n---")
      end
    end
  rescue StandardError => e
    status = 'error'
    result = "ERROR: #{e.class} - #{e.message}\n#{e.backtrace.first(5).join("\n")}"
  end

  job[:last_run] = Time.now.utc.iso8601
  job[:last_status] = status
  jobs[job[:id]] = job
  save_jobs(jobs: jobs)

  { job: job, result: result, duration: Time.now - start, status: status }
end

.run_due(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Cron.run_due(now: Time, jobs: optional Hash)

Fires every currently-due enabled job via run(). Returns the list of status:, duration: hashes (empty when nothing is due).



389
390
391
392
393
394
395
396
397
398
# File 'lib/pwn/cron.rb', line 389

public_class_method def self.run_due(opts = {})
  now = opts[:now] || Time.now
  jobs = opts[:jobs] || due_jobs(now: now)
  jobs.map do |id, _job|
    res = run(id: id)
    { id: id, status: res[:status], duration: res[:duration] }
  rescue StandardError => e
    { id: id, status: 'error', error: "#{e.class}: #{e.message}" }
  end
end

.scheduler_backend(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Cron.scheduler_backend(os: optional, backend: optional force)



824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
# File 'lib/pwn/cron.rb', line 824

public_class_method def self.scheduler_backend(opts = {})
  return opts[:backend].to_sym if opts[:backend]

  unless opts[:os]
    persisted = scheduler_config[:backend]
    return persisted if persisted
  end

  os = (opts[:os] || os_type).to_sym
  case os
  when :windows
    schtasks_available? ? :schtasks : :worker
  when :osx
    :launchd
  when :linux
    if systemd_user_available?
      :systemd_user
    elsif crontab_available?
      :crontab
    else
      :worker
    end
  else
    crontab_available? ? :crontab : :worker
  end
end

.scheduler_configObject



749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# File 'lib/pwn/cron.rb', line 749

public_class_method def self.scheduler_config
  path = scheduler_file
  return { enabled: true, backend: nil } unless File.exist?(path)

  raw = YAML.safe_load_file(
    path,
    permitted_classes: [Symbol, Time],
    symbolize_names: true
  ) || {}
  {
    enabled: raw.key?(:enabled) ? raw[:enabled] == true : true,
    backend: raw[:backend]&.to_sym,
    updated_at: raw[:updated_at]
  }
rescue StandardError
  { enabled: true, backend: nil }
end

.scheduler_fileObject



745
746
747
# File 'lib/pwn/cron.rb', line 745

public_class_method def self.scheduler_file
  File.join(cron_dir, 'scheduler.yml')
end

.scheduler_status(opts = {}) ⇒ Object



1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
# File 'lib/pwn/cron.rb', line 1018

public_class_method def self.scheduler_status(opts = {})
  backend = scheduler_backend(opts)
  {
    os: os_type,
    backend: backend,
    enabled: cron_enabled?,
    apply: apply_native?(opts),
    config: scheduler_config,
    worker: worker_status,
    unit_dir: native_unit_dir(backend: backend, apply: apply_native?(opts))
  }
end

.schtasks_available?Boolean

Returns:

  • (Boolean)


818
819
820
# File 'lib/pwn/cron.rb', line 818

public_class_method def self.schtasks_available?
  !which_bin(name: 'schtasks').empty?
end

.schtasks_spec(opts = {}) ⇒ Object



939
940
941
942
943
944
945
946
947
948
949
950
951
# File 'lib/pwn/cron.rb', line 939

public_class_method def self.schtasks_spec(opts = {})
  cal = opts[:calendar] || cron_to_calendar(schedule: opts[:schedule])
  return nil unless cal

  st = format('%<h>02d:%<m>02d', h: cal[:hour], m: cal[:minute])
  case cal[:kind]
  when :daily then { sc: 'DAILY', st: st }
  when :weekly
    days = %w[SUN MON TUE WED THU FRI SAT]
    { sc: 'WEEKLY', st: st, d: days[cal[:wday]] }
  when :monthly then { sc: 'MONTHLY', st: st, d: cal[:mday].to_s }
  end
end

.start_worker(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Cron.start_worker( interval: 60, foreground: false, restart: true )

Idempotent. With restart:true (default) a live worker is replaced so pwn setup always leaves a current worker running.



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/pwn/cron.rb', line 421

public_class_method def self.start_worker(opts = {})
  interval = (opts[:interval] || DEFAULT_INTERVAL).to_i
  interval = DEFAULT_INTERVAL if interval <= 0
  foreground = opts[:foreground] ? true : false
  restart = opts.fetch(:restart, true)

  st = worker_status
  return { started: false, already_running: true, pid: st[:pid], interval: interval } if st[:running] && !restart && !foreground

  stop_worker if st[:running] && (restart || foreground)

  if foreground
    write_pid(pid: Process.pid)
    begin
      worker_loop(interval: interval)
    ensure
      clear_pid(pid: Process.pid)
    end
    return { started: true, pid: Process.pid, foreground: true, interval: interval }
  end

  FileUtils.mkdir_p(cron_dir)
  spawn_env = ENV.to_h.merge('PWN_CRON_DIR' => cron_dir.to_s)
  pid = Process.spawn(
    spawn_env,
    RbConfig.ruby,
    '-I', File.expand_path('..', __dir__),
    '-e', worker_spawn_snippet(interval: interval),
    i[out err] => [worker_log, 'a'],
    in: File::NULL,
    pgroup: !windows?
  )
  Process.detach(pid)

  # The child writes the pidfile; wait briefly.
  waited = 0
  live = nil
  while waited < 50
    live = read_pid
    break if live && process_alive?(pid: live)

    sleep 0.1
    waited += 1
  end
  {
    started: !(live && process_alive?(pid: live)).nil?,
    pid: live,
    spawn_pid: pid,
    interval: interval,
    log: worker_log,
    pid_file: pid_file
  }
end

.stop_workerObject

Supported Method Parameters

PWN::Cron.stop_worker



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/pwn/cron.rb', line 477

public_class_method def self.stop_worker
  pid = read_pid
  killed = false
  if pid && process_alive?(pid: pid)
    begin
      Process.kill('TERM', pid)
      20.times do
        break unless process_alive?(pid: pid)

        sleep 0.05
      end
      Process.kill('KILL', pid) if process_alive?(pid: pid)
      killed = true
    rescue Errno::ESRCH, Errno::EPERM
      nil
    rescue StandardError
      windows_taskkill(pid: pid) if windows?
    end
  end
  FileUtils.rm_f(pid_file)
  { stopped: true, pid: pid, killed: killed }
end

.sync_native_jobs(opts = {}) ⇒ Object



1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'lib/pwn/cron.rb', line 1061

public_class_method def self.sync_native_jobs(opts = {})
  backend = (opts[:backend] || scheduler_backend).to_sym
  apply = apply_native?(opts)
  load_jobs.map do |_id, job|
    next unless job.is_a?(Hash)

    if job[:enabled]
      install_native_job(job: job, backend: backend, apply: apply)
    else
      remove_native_job(job: job, backend: backend, apply: apply)
    end
  end.compact
end

.sync_scheduler(opts = {}) ⇒ Object



1012
1013
1014
1015
1016
# File 'lib/pwn/cron.rb', line 1012

public_class_method def self.sync_scheduler(opts = {})
  return uninstall_scheduler(opts) unless opts.fetch(:enabled, cron_enabled?)

  install_scheduler(opts.merge(sync_jobs: true))
end

.systemd_on_calendar(opts = {}) ⇒ Object



915
916
917
918
919
920
921
922
923
924
925
926
927
# File 'lib/pwn/cron.rb', line 915

public_class_method def self.systemd_on_calendar(opts = {})
  cal = opts[:calendar] || cron_to_calendar(schedule: opts[:schedule])
  return nil unless cal

  hhmmss = format('%<h>02d:%<m>02d:00', h: cal[:hour], m: cal[:minute])
  case cal[:kind]
  when :daily then "*-*-* #{hhmmss}"
  when :weekly
    dow = %w[Sun Mon Tue Wed Thu Fri Sat][cal[:wday]]
    "#{dow} *-*-* #{hhmmss}"
  when :monthly then "*-*-#{format('%<d>02d', d: cal[:mday])} #{hhmmss}"
  end
end

.systemd_user_available?Boolean

Returns:

  • (Boolean)


808
809
810
811
812
813
814
815
816
# File 'lib/pwn/cron.rb', line 808

public_class_method def self.systemd_user_available?
  return false if which_bin(name: 'systemctl').empty?
  return false unless apply_native?

  system('systemctl', '--user', 'show-environment',
         out: File::NULL, err: File::NULL)
rescue StandardError
  false
end

.tick(opts = {}) ⇒ Object

Single tick used by the loop AND by tests (no sleep).



501
502
503
# File 'lib/pwn/cron.rb', line 501

public_class_method def self.tick(opts = {})
  run_due(now: opts[:now] || Time.now)
end

.uninstall_crontab(opts = {}) ⇒ Object



1107
1108
1109
1110
1111
1112
1113
1114
1115
# File 'lib/pwn/cron.rb', line 1107

public_class_method def self.uninstall_crontab(opts = {})
  apply = apply_native?(opts)
  return [:preview] unless apply && crontab_available?

  existing = crontab_read
  cleaned = strip_pwn_crontab(existing: existing)
  crontab_write(body: cleaned) if cleaned != existing
  [:crontab]
end

.uninstall_crontab_job(opts = {}) ⇒ Object



1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
# File 'lib/pwn/cron.rb', line 1117

public_class_method def self.uninstall_crontab_job(opts = {})
  job = opts[:job]
  apply = apply_native?(opts)
  FileUtils.rm_f(File.join(cron_dir, 'native', "job-#{job[:id]}.crontab"))
  return { backend: :crontab, apply: false, id: job[:id] } unless apply && crontab_available?

  existing = crontab_read
  cleaned = strip_pwn_crontab(existing: existing, id: job[:id].to_s)
  crontab_write(body: cleaned) if cleaned != existing
  { backend: :crontab, apply: true, id: job[:id], removed: true }
end

.uninstall_launchd(opts = {}) ⇒ Object



1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
# File 'lib/pwn/cron.rb', line 1264

public_class_method def self.uninstall_launchd(opts = {})
  apply = apply_native?(opts)
  labels = [LAUNCHD_WORKER_LABEL]
  load_jobs.each_value do |job|
    next unless job.is_a?(Hash)

    labels << launchd_job_label(job: job)
  end
  labels.each do |label|
    path = unit_file_path(backend: :launchd, name: "#{label}.plist", apply: apply)
    launchctl_unload(path: path, label: label) if apply
    rm_unit_file(backend: :launchd, name: "#{label}.plist", apply: apply)
  end
  labels
end

.uninstall_launchd_job(opts = {}) ⇒ Object



1280
1281
1282
1283
1284
1285
1286
1287
1288
# File 'lib/pwn/cron.rb', line 1280

public_class_method def self.uninstall_launchd_job(opts = {})
  job = opts[:job]
  apply = apply_native?(opts)
  label = launchd_job_label(job: job)
  path = unit_file_path(backend: :launchd, name: "#{label}.plist", apply: apply)
  launchctl_unload(path: path, label: label) if apply
  rm_unit_file(backend: :launchd, name: "#{label}.plist", apply: apply)
  { backend: :launchd, id: job[:id], removed: label }
end

.uninstall_scheduler(opts = {}) ⇒ Object



992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
# File 'lib/pwn/cron.rb', line 992

public_class_method def self.uninstall_scheduler(opts = {})
  backend = (opts[:backend] || scheduler_config[:backend] || scheduler_backend).to_sym
  apply = apply_native?(opts)
  persist_scheduler_config(enabled: false, backend: backend) unless opts[:persist] == false
  stop_worker if opts.fetch(:stop, true)

  removed = case backend
            when :systemd_user then uninstall_systemd_user(apply: apply)
            when :launchd then uninstall_launchd(apply: apply)
            when :schtasks then uninstall_schtasks(apply: apply)
            when :crontab then uninstall_crontab(apply: apply)
            else
              []
            end
  { ok: true, backend: backend, apply: apply, removed: removed }
rescue StandardError => e
  warn("[PWN::Cron] uninstall_scheduler failed: #{e.class}: #{e.message}")
  { ok: false, error: "#{e.class}: #{e.message}" }
end

.uninstall_schtasks(opts = {}) ⇒ Object



1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
# File 'lib/pwn/cron.rb', line 1316

public_class_method def self.uninstall_schtasks(opts = {})
  apply = apply_native?(opts)
  names = [SCHTASKS_WORKER_NAME]
  load_jobs.each_value do |job|
    next unless job.is_a?(Hash)

    names << schtasks_job_name(job: job)
  end
  names.each { |n| run_argv(argv: ['schtasks', '/Delete', '/F', '/TN', n]) if apply && schtasks_available? }
  names
end

.uninstall_schtasks_job(opts = {}) ⇒ Object



1328
1329
1330
1331
1332
1333
1334
1335
# File 'lib/pwn/cron.rb', line 1328

public_class_method def self.uninstall_schtasks_job(opts = {})
  job = opts[:job]
  apply = apply_native?(opts)
  name = schtasks_job_name(job: job)
  run_argv(argv: ['schtasks', '/Delete', '/F', '/TN', name]) if apply && schtasks_available?
  FileUtils.rm_f(File.join(cron_dir, 'native', "job-#{job[:id]}.schtasks.txt"))
  { backend: :schtasks, id: job[:id], removed: name }
end

.uninstall_systemd_user(opts = {}) ⇒ Object



1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
# File 'lib/pwn/cron.rb', line 1203

public_class_method def self.uninstall_systemd_user(opts = {})
  apply = apply_native?(opts)
  names = [SYSTEMD_WORKER_UNIT]
  load_jobs.each_value do |job|
    next unless job.is_a?(Hash)

    names << systemd_job_timer(job: job)
    names << systemd_job_service(job: job)
  end
  if apply
    names.each { |u| systemd_user(args: ['disable', '--now', u]) }
    systemd_user(args: ['daemon-reload'])
  end
  names.each { |n| rm_unit_file(backend: :systemd_user, name: n, apply: apply) }
  names
end

.uninstall_systemd_user_job(opts = {}) ⇒ Object



1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
# File 'lib/pwn/cron.rb', line 1220

public_class_method def self.uninstall_systemd_user_job(opts = {})
  job = opts[:job]
  apply = apply_native?(opts)
  svc = systemd_job_service(job: job)
  tmr = systemd_job_timer(job: job)
  if apply
    systemd_user(args: ['disable', '--now', tmr])
    systemd_user(args: ['disable', '--now', svc])
    systemd_user(args: ['daemon-reload'])
  end
  rm_unit_file(backend: :systemd_user, name: svc, apply: apply)
  rm_unit_file(backend: :systemd_user, name: tmr, apply: apply)
  { backend: :systemd_user, id: job[:id], removed: [svc, tmr] }
end

.which_bin(opts = {}) ⇒ Object



790
791
792
793
794
795
796
797
798
799
800
801
802
# File 'lib/pwn/cron.rb', line 790

public_class_method def self.which_bin(opts = {})
  name = opts[:name].to_s
  return '' if name.empty?

  exts = windows? ? %w[.exe .bat .cmd] + [''] : ['']
  ENV.fetch('PATH', '').split(File::PATH_SEPARATOR).each do |dir|
    exts.each do |ext|
      cand = File.join(dir, "#{name}#{ext}")
      return cand if File.file?(cand) && File.executable?(cand)
    end
  end
  ''
end

.windows?Boolean

Returns:

  • (Boolean)


741
742
743
# File 'lib/pwn/cron.rb', line 741

public_class_method def self.windows?
  os_type == :windows
end

.worker_logObject



324
325
326
# File 'lib/pwn/cron.rb', line 324

public_class_method def self.worker_log
  File.join(cron_dir, 'worker.log')
end

.worker_loop(opts = {}) ⇒ Object

rubocop:disable Naming/PredicateMethod



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/pwn/cron.rb', line 508

public_class_method def self.worker_loop(opts = {}) # rubocop:disable Naming/PredicateMethod
  interval = (opts[:interval] || DEFAULT_INTERVAL).to_i
  interval = DEFAULT_INTERVAL if interval <= 0
  once = opts[:once] ? true : false
  trap_shutdown! unless once

  loop do
    begin
      tick
    rescue StandardError => e
      warn("[PWN::Cron] worker tick failed: #{e.class}: #{e.message}")
    end
    break if once
    break if @worker_stop
    break if File.exist?(pid_file) && read_pid != Process.pid

    slept = 0
    while slept < interval
      break if @worker_stop

      step = [1, interval - slept].min
      sleep(step)
      slept += step
    end
    break if @worker_stop
  end
  true
end

.worker_statusObject

Supported Method Parameters

PWN::Cron.worker_status



402
403
404
405
406
407
408
409
410
411
# File 'lib/pwn/cron.rb', line 402

public_class_method def self.worker_status
  pid = read_pid
  running = pid && process_alive?(pid: pid)
  {
    running: running ? true : false,
    pid: running ? pid : nil,
    pid_file: pid_file,
    log: worker_log
  }
end