@@ -141,8 +141,9 @@ export async function readCronFilesDocker(): Promise<string> {
141141 try {
142142 const filePath = path . join ( crontabDir , file ) ;
143143 const content = await fs . readFile ( filePath , "utf-8" ) ;
144+ allCronContent += `# User: ${ file } \n` ;
144145 allCronContent += content ;
145- allCronContent += "\n" ;
146+ allCronContent += "\n\n " ;
146147 } catch ( fileError ) {
147148 console . error ( `Error reading crontab for user ${ file } :` , fileError ) ;
148149 }
@@ -157,18 +158,61 @@ export async function readCronFilesDocker(): Promise<string> {
157158
158159export async function writeCronFilesDocker ( cronContent : string ) : Promise < boolean > {
159160 try {
160- const userCrontabPath = `/host/cron/crontabs/root` ;
161- const content = cronContent + "\n" ;
161+ // Parse the cron content and distribute to appropriate user crontabs
162+ const lines = cronContent . split ( "\n" ) ;
163+ const userCrontabs : { [ key : string ] : string [ ] } = { } ;
164+ let currentUser = "root" ; // Default to root user
165+ let currentContent : string [ ] = [ ] ;
162166
163- try {
164- await execAsync ( `chown root:root ${ userCrontabPath } ` ) ;
165- await execAsync ( `chmod 666 ${ userCrontabPath } ` ) ;
166- await fs . writeFile ( userCrontabPath , content ) ;
167- await execAsync ( `chown 1000:105 ${ userCrontabPath } ` ) ;
168- await execAsync ( `chmod 600 ${ userCrontabPath } ` ) ;
169- } catch ( error ) {
170- console . error ( `Failed to write crontab:` , error ) ;
171- return false ;
167+ for ( const line of lines ) {
168+ if ( line . startsWith ( "# User:" ) ) {
169+ // Save previous user's content
170+ if ( currentUser && currentContent . length > 0 ) {
171+ userCrontabs [ currentUser ] = [ ...currentContent ] ;
172+ }
173+ currentUser = line . substring ( 8 ) . trim ( ) ;
174+ currentContent = [ ] ;
175+ } else if ( line . startsWith ( "# System Crontab" ) ) {
176+ // Save previous user's content
177+ if ( currentUser && currentContent . length > 0 ) {
178+ userCrontabs [ currentUser ] = [ ...currentContent ] ;
179+ }
180+ currentUser = "system" ;
181+ currentContent = [ ] ;
182+ } else if ( currentUser && line . trim ( ) ) {
183+ currentContent . push ( line ) ;
184+ }
185+ }
186+
187+ // Save the last user's content
188+ if ( currentUser && currentContent . length > 0 ) {
189+ userCrontabs [ currentUser ] = [ ...currentContent ] ;
190+ }
191+
192+ // Write to appropriate crontab files
193+ for ( const [ username , cronJobs ] of Object . entries ( userCrontabs ) ) {
194+ if ( username === "system" ) {
195+ const systemContent = cronJobs . join ( "\n" ) + "\n" ;
196+ try {
197+ await fs . writeFile ( "/host/crontab" , systemContent ) ;
198+ } catch ( error ) {
199+ console . error ( "Failed to write system crontab:" , error ) ;
200+ return false ;
201+ }
202+ } else {
203+ const userCrontabPath = `/host/cron/crontabs/${ username } ` ;
204+ const userContent = cronJobs . join ( "\n" ) + "\n" ;
205+ try {
206+ await execAsync ( `chown root:root ${ userCrontabPath } ` ) ;
207+ await execAsync ( `chmod 666 ${ userCrontabPath } ` ) ;
208+ await fs . writeFile ( userCrontabPath , userContent ) ;
209+ await execAsync ( `chown 1000:105 ${ userCrontabPath } ` ) ;
210+ await execAsync ( `chmod 600 ${ userCrontabPath } ` ) ;
211+ } catch ( error ) {
212+ console . error ( `Failed to write crontab for user ${ username } :` , error ) ;
213+ return false ;
214+ }
215+ }
172216 }
173217
174218 return true ;
0 commit comments