Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(cron): join crontab lines back to their metadata (#117)

Juan Denis719d29c8a568
diff --git a/backend/app/services/cron_service.py b/backend/app/services/cron_service.py
index f16e9d86..e43a03f4 100644
--- a/backend/app/services/cron_service.py
+++ b/backend/app/services/cron_service.py
@@ -30,6 +30,11 @@ SHIM_PATH = os.path.join(
_UNSET = object()
+# Marker comment written above every ServerKit-managed crontab line. The first
+# token after the colon is the job's metadata id — the id, not the display
+# name, is what lets list/edit/delete find the line again (#117).
+_MARKER_RE = re.compile(r'^#\s*ServerKit Job:\s*(\S+)')
+
def _auto_capture_cron(action):
"""Best-effort local CRON checkpoint without burdening utility callers.
@@ -179,23 +184,52 @@ class CronService:
try:
# Get current user's crontab
current = cls._read_crontab()
+ meta_jobs = metadata.get('jobs', {})
+ matched_ids = set()
if current is not None:
lines = current.strip().split('\n')
+ # Id carried over from a "# ServerKit Job: <id>" marker
+ # comment to the job line right below it (#117).
+ pending_id = None
for i, line in enumerate(lines):
line = line.strip()
- # Skip empty lines and comments
- if not line or line.startswith('#'):
+ if not line:
+ pending_id = None
+ continue
+ if line.startswith('#'):
+ marker = _MARKER_RE.match(line)
+ pending_id = marker.group(1) if marker else None
continue
# Parse cron line
job = cls._parse_cron_line(line, i)
if job:
- # Add metadata if available
- job_id = job.get('id', str(i))
- if job_id in metadata.get('jobs', {}):
- job.update(metadata['jobs'][job_id])
+ # Join the crontab line back to its metadata: by the
+ # marker comment's id, or — for lines written before
+ # the marker carried the id (#117) — by matching
+ # schedule + command against the metadata store.
+ job_id = pending_id if pending_id in meta_jobs else None
+ if job_id is None:
+ job_id = cls._find_metadata_match(
+ meta_jobs, job['schedule'], job['command'],
+ exclude=matched_ids)
+ if job_id is not None:
+ job.update(meta_jobs[job_id])
+ job['id'] = job_id
+ job['source'] = 'serverkit'
+ matched_ids.add(job_id)
jobs.append(job)
+ pending_id = None
+
+ # Metadata-only jobs: disabled ones (their crontab line is
+ # commented out, so the scan above never sees them) and jobs
+ # whose line was removed by hand. Listing them keeps them
+ # editable/deletable from the panel.
+ for job_id, job_data in meta_jobs.items():
+ if job_id not in matched_ids:
+ jobs.append({'id': job_id, **job_data,
+ 'source': 'serverkit'})
except subprocess.SubprocessError as e:
return {'success': False, 'error': str(e), 'jobs': []}
@@ -253,6 +287,35 @@ class CronService:
'source': 'crontab'
}
+ @classmethod
+ def _find_metadata_match(cls, meta_jobs: Dict, schedule: str,
+ command: str, exclude=()) -> Optional[str]:
+ """Metadata id for a crontab line, matched by schedule + command.
+
+ Fallback join for lines whose marker comment predates carrying the id
+ (#117). Matches the bare command or its serverkit-cron-run wrapped
+ form. `exclude` holds ids already claimed by earlier lines so two
+ identical entries can't collapse onto one."""
+ for job_id, data in meta_jobs.items():
+ if job_id in exclude or data.get('schedule') != schedule:
+ continue
+ bare = data.get('command', '')
+ if not bare:
+ continue
+ if command == bare or command.endswith(f"{SHIM_PATH} {job_id} -- {bare}"):
+ return job_id
+ return None
+
+ @classmethod
+ def _line_is_job(cls, line: str, schedule: str, command: str,
+ job_id: str) -> bool:
+ """Is this crontab line the given job's entry (bare or shim-wrapped,
+ active or commented out)?"""
+ if not (schedule and command):
+ return False
+ return (f"{schedule} {command}" in line
+ or f"{SHIM_PATH} {job_id} -- {command}" in line)
+
@classmethod
def _describe_schedule(cls, schedule: str) -> str:
"""Generate human-readable description of cron schedule."""
@@ -357,8 +420,16 @@ class CronService:
if cls.is_linux():
current_crontab = cls._read_crontab() or ''
- # Add comment and new job
- comment = f"# ServerKit Job: {name or job_id}"
+ # Marker comment + new job. The comment must carry the job_id —
+ # it is the join key list/edit/delete use to find this line again;
+ # writing the display name instead orphaned the job (#117). The
+ # name rides along for `crontab -e` readability, newline-stripped
+ # so user input can't inject crontab lines.
+ comment = f"# ServerKit Job: {job_id}"
+ if name:
+ clean_name = re.sub(r'\s+', ' ', str(name)).strip()
+ if clean_name:
+ comment += f" ({clean_name})"
new_line = f"{schedule} {command}"
new_crontab = f"{current_crontab.rstrip()}\n{comment}\n{new_line}\n"
@@ -407,7 +478,11 @@ class CronService:
command = job_data.get('command', '')
schedule = job_data.get('schedule', '')
- # Filter out the job and its comment
+ # Filter out the job and its marker comment. The line is
+ # matched bare or shim-wrapped (tracked jobs, #117); when a
+ # legacy marker comment carries the name instead of the id,
+ # dropping the comment sitting right above the removed line
+ # keeps it from being orphaned.
new_lines = []
skip_next = False
for line in lines:
@@ -417,7 +492,9 @@ class CronService:
if f"# ServerKit Job:" in line and job_id in line:
skip_next = True
continue
- if command and schedule and f"{schedule} {command}" in line:
+ if cls._line_is_job(line, schedule, command, job_id):
+ if new_lines and _MARKER_RE.match(new_lines[-1].strip()):
+ new_lines.pop()
continue
new_lines.append(line)
@@ -449,11 +526,10 @@ class CronService:
lines = current.split('\n')
command = job_data.get('command', '')
schedule = job_data.get('schedule', '')
- job_line = f"{schedule} {command}"
new_lines = []
for line in lines:
- if job_line in line:
+ if cls._line_is_job(line, schedule, command, job_id):
if enabled:
# Remove leading # if present
new_lines.append(line.lstrip('# '))
diff --git a/backend/tests/test_cron_crontab_join.py b/backend/tests/test_cron_crontab_join.py
new file mode 100644
index 00000000..df22dd0b
--- /dev/null
+++ b/backend/tests/test_cron_crontab_join.py
@@ -0,0 +1,158 @@
+"""Crontab ↔ metadata join (#117).
+
+Creating a named cron job on Linux wrote the display name into the marker
+comment and re-listed jobs under synthetic `cron_<index>` ids, so the metadata
+(name, description) never joined back and every mutation route answered "Job
+not found". These tests drive the full Linux path against an in-memory
+crontab: create → list → edit → run-id stability → delete, plus the rescue
+path for jobs created before the marker carried the id.
+"""
+import pytest
+
+from app.services.cron_service import CronService, SHIM_PATH
+
+
+def linux_cron(tmp_path, monkeypatch):
+ """CronService wired to a fake Linux host: in-memory crontab, tmp metadata."""
+ import app.services.cron_service as mod
+ monkeypatch.setattr(mod, 'JOBS_FILE', str(tmp_path / 'cron_jobs.json'))
+ monkeypatch.setattr(CronService, 'is_linux', classmethod(lambda cls: True))
+
+ store = {'crontab': ''}
+ monkeypatch.setattr(CronService, '_read_crontab',
+ classmethod(lambda cls: store['crontab']))
+
+ def _install(cls, content):
+ store['crontab'] = content
+ return None
+
+ monkeypatch.setattr(CronService, '_install_crontab',
+ classmethod(_install))
+ return store
+
+
+CMD = '/usr/local/bin/backup.sh --full'
+SCHEDULE = '0 3 * * *'
+
+
+def _add(name='Nightly Backup'):
+ result = CronService.add_job(SCHEDULE, CMD, name=name)
+ assert result['success']
+ return result['job_id']
+
+
+def test_marker_comment_carries_job_id(linux_cron):
+ job_id = _add()
+ assert f"# ServerKit Job: {job_id} (Nightly Backup)" in linux_cron['crontab']
+ assert f"{SCHEDULE} {CMD}" in linux_cron['crontab']
+
+
+def test_marker_name_cannot_inject_crontab_lines(linux_cron):
+ _add(name='evil\n* * * * * /bin/rm -rf /')
+ comment_lines = [l for l in linux_cron['crontab'].split('\n')
+ if l.startswith('# ServerKit Job:')]
+ assert len(comment_lines) == 1
+ assert '\n* * * * *' not in comment_lines[0]
+ assert '/bin/rm' not in linux_cron['crontab'].replace(comment_lines[0], '')
+
+
+def test_list_joins_name_via_marker_id(linux_cron):
+ """The #117 repro: a freshly created named job must list under its real id
+ with its name, not as an unnamed cron_<index> row."""
+ job_id = _add()
+ jobs = CronService.list_jobs()['jobs']
+ assert len(jobs) == 1
+ assert jobs[0]['id'] == job_id
+ assert jobs[0]['name'] == 'Nightly Backup'
+ assert jobs[0]['source'] == 'serverkit'
+
+
+def test_listed_id_reaches_update_and_delete(linux_cron):
+ _add()
+ listed_id = CronService.list_jobs()['jobs'][0]['id']
+
+ assert CronService.update_job(listed_id, name='Renamed')['success']
+ assert CronService.get_job(listed_id)['name'] == 'Renamed'
+
+ assert CronService.remove_job(listed_id)['success']
+ assert CMD not in linux_cron['crontab']
+ assert '# ServerKit Job:' not in linux_cron['crontab']
+ assert CronService.list_jobs()['jobs'] == []
+
+
+def test_legacy_named_marker_rescued_by_schedule_command_match(linux_cron):
+ """Jobs written before the fix carry the NAME in the marker comment; the
+ schedule+command fallback must still join them to their metadata."""
+ job_id = _add()
+ # Rewrite the crontab the way the pre-fix code did.
+ linux_cron['crontab'] = (f"# ServerKit Job: Nightly Backup\n"
+ f"{SCHEDULE} {CMD}\n")
+
+ jobs = CronService.list_jobs()['jobs']
+ assert len(jobs) == 1
+ assert jobs[0]['id'] == job_id
+ assert jobs[0]['name'] == 'Nightly Backup'
+
+
+def test_legacy_remove_drops_orphan_marker_comment(linux_cron):
+ job_id = _add()
+ linux_cron['crontab'] = (f"# ServerKit Job: Nightly Backup\n"
+ f"{SCHEDULE} {CMD}\n")
+
+ assert CronService.remove_job(job_id)['success']
+ assert CMD not in linux_cron['crontab']
+ assert '# ServerKit Job:' not in linux_cron['crontab']
+
+
+def test_disabled_job_stays_listed(linux_cron):
+ """toggle_job comments the line out; the job must not vanish from the
+ admin list (it comes back from metadata, disabled)."""
+ job_id = _add()
+ assert CronService.toggle_job(job_id, False)['success']
+ assert f"# {SCHEDULE} {CMD}" in linux_cron['crontab']
+
+ jobs = CronService.list_jobs()['jobs']
+ assert len(jobs) == 1
+ assert jobs[0]['id'] == job_id
+ assert jobs[0]['enabled'] is False
+
+ assert CronService.toggle_job(job_id, True)['success']
+ lines = linux_cron['crontab'].split('\n')
+ assert f"{SCHEDULE} {CMD}" in lines
+
+
+def test_tracked_shim_line_matches_toggle_and_remove(linux_cron):
+ job_id = _add()
+ assert CronService.set_tracking(job_id, True)['success']
+ assert f"{SHIM_PATH} {job_id} -- {CMD}" in linux_cron['crontab']
+
+ # The wrapped line still joins back to its metadata id on list.
+ jobs = CronService.list_jobs()['jobs']
+ assert len(jobs) == 1
+ assert jobs[0]['id'] == job_id
+ assert jobs[0]['command'] == CMD # metadata's bare command, not the shim
+
+ assert CronService.toggle_job(job_id, False)['success']
+ active = [l for l in linux_cron['crontab'].split('\n')
+ if l.strip() and not l.strip().startswith('#')]
+ assert active == []
+
+ assert CronService.remove_job(job_id)['success']
+ assert CMD not in linux_cron['crontab']
+
+
+def test_foreign_crontab_lines_left_untouched(linux_cron):
+ linux_cron['crontab'] = "MAILTO=root\n15 2 * * * /opt/other/task.sh\n"
+ job_id = _add()
+
+ jobs = CronService.list_jobs()['jobs']
+ by_id = {j['id']: j for j in jobs}
+ assert job_id in by_id
+ foreign = [j for j in jobs if j['id'] != job_id]
+ assert len(foreign) == 1
+ assert foreign[0]['command'] == '/opt/other/task.sh'
+
+ assert CronService.remove_job(job_id)['success']
+ assert '15 2 * * * /opt/other/task.sh' in linux_cron['crontab']
+ assert 'MAILTO=root' in linux_cron['crontab']