diff --git a/app/_utils/system/cron.ts b/app/_utils/system/cron.ts index e83654e..32f1a93 100644 --- a/app/_utils/system/cron.ts +++ b/app/_utils/system/cron.ts @@ -166,13 +166,20 @@ export async function deleteCronJob(id: string): Promise { let jobIndex = 0; let targetJobIndex = parseInt(id.replace("unix-", "")); - lines.forEach((line) => { + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; const trimmedLine = line.trim(); - if (!trimmedLine) return; + if (!trimmedLine) continue; - if (trimmedLine.startsWith("#")) { - currentComment = trimmedLine; + if (trimmedLine.startsWith("# User:") || trimmedLine.startsWith("# System Crontab")) { + cronEntries.push(trimmedLine); + } else if (trimmedLine.startsWith("#")) { + if (i + 1 < lines.length && !lines[i + 1].trim().startsWith("#") && lines[i + 1].trim()) { + currentComment = trimmedLine; + } else { + cronEntries.push(trimmedLine); + } } else { if (jobIndex !== targetJobIndex) { const entryWithComment = currentComment @@ -183,7 +190,7 @@ export async function deleteCronJob(id: string): Promise { jobIndex++; currentComment = ""; } - }); + } const newCron = cronEntries.join("\n") + "\n"; await writeCronFiles(newCron); diff --git a/app/_utils/system/docker.ts b/app/_utils/system/docker.ts index c48f1bf..fff3079 100644 --- a/app/_utils/system/docker.ts +++ b/app/_utils/system/docker.ts @@ -158,22 +158,19 @@ export async function readCronFilesDocker(): Promise { export async function writeCronFilesDocker(cronContent: string): Promise { try { - // Parse the cron content and distribute to appropriate user crontabs const lines = cronContent.split("\n"); const userCrontabs: { [key: string]: string[] } = {}; - let currentUser = "root"; // Default to root user + let currentUser = "root"; let currentContent: string[] = []; for (const line of lines) { if (line.startsWith("# User:")) { - // Save previous user's content if (currentUser && currentContent.length > 0) { userCrontabs[currentUser] = [...currentContent]; } currentUser = line.substring(8).trim(); currentContent = []; } else if (line.startsWith("# System Crontab")) { - // Save previous user's content if (currentUser && currentContent.length > 0) { userCrontabs[currentUser] = [...currentContent]; } @@ -184,12 +181,10 @@ export async function writeCronFilesDocker(cronContent: string): Promise 0) { userCrontabs[currentUser] = [...currentContent]; } - // Write to appropriate crontab files for (const [username, cronJobs] of Object.entries(userCrontabs)) { if (username === "system") { const systemContent = cronJobs.join("\n") + "\n";