简体   繁体   中英

crontab not getting programatically places from a mac application

I have a piece of code in my application which i use to create a crontab. When i compile and run it from my xcode, the code works perfectly fine without errors and places a cronjob at the time specified.

 NSDictionary *error = [NSDictionary new];
 NSString *script =  @"do shell script \" crontab -l > mycron ; echo '30 11 * * * cd /Applications/PagePlannerMac && ./JunkDataDeleteScript.sh >> /Applications/PagePlannerMac/scriptlogs.log' >> mycron ; crontab mycron ; rm mycron\" ";
                    
 NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:script];
                        if ([appleScript executeAndReturnError:&error]) {
                            NSLog(@"success!");
                        } else {
                            NSLog(@"failure!");
                        }

Basically what the code does is it executes a shell command to create the cron.

When i compile and get the dmg file of my application, install and test. The cron does not execute, there are no errors or crashes. Not sure what is causing it to fail. Any help is apprecitated.

Make a shell script:

#!/bin/sh
( \
    cd /Applications/PagePlannerMac && \
        /path/to/JunkDataDeleteScript.sh | tee -a \
        >> /Applications/PagePlannerMac/scriptlogs.log && \
        crontab -l && echo '30 11 * * * /path/to/this/script.sh' \
) | sort | uniq | crontab

Add the script the first time to crontab:

30 11 * * * /path/to/this/script.sh

When it runs, the following happens:

  • Change directory (cd) to /Applications/PagePlannerMac .
  • Run the JunkDataDeleteScript.sh .
  • Use tee to make a log file /Applications/PagePlannerMac/scriptlogs.log .
  • Dump the current crontab file to stdout .
  • Then add the crontab line for 11:30 am daily.
  • Capture the JunkDataDeleteScript.sh output, current crontab , and the echo to stdout .
  • Pass that into sort and uniq to remove duplicates.
  • Finally pass that into crontab to replace the existing crontab .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM