Converting cron to launchd - MAILTO

2020-07-17 05:45发布

问题:

Migrating from an old mac to a new one.

Trying to migrate the user crontab I had on the old mac.

Now I know I can probably get cron to run by creating /etc/crontab - but given that apple has deprecated it in favour of launchd - I thought I'd take a swing at migrating my crontab to launchd plist files. Always fun to learn something new :)

The crontab isn't that hard - I tend to set up shell scripts that do the work and then just call them at scheduled times.

I can call these fine from the plist at the same times - no issues.

But - at the top of my crontab I had MAILTO=my-gmail-email-address - so that instead of sending mail to the local user on the box's mail spool it sent them to gmail for me.

I can't see that you can configure this for a launchd plist file.

So - before I go modify all my scripts to redirect all output to tmp files then mail the tmp file (would have to do this in quite a few scripts) - is there a way to do this at the launchd level?

回答1:

I'm afraid launchd(8) does not support mailing of stdout/stderr out of the box. Here's a workaround:

Redirect output via StandardOutPath and StandardErrorPath to files in a dedicated directory. Then setup a job which mails every file in this directory. Trigger this job with the QueueDirectories key. Make sure this script removes the files after mailing them.



回答2:

one way to convert to launchd is the lazy way (how i do it)

You pay $0.99 for Lingon from the app store; then you can just fill out a few fields and it makes the launchd...

otherwise: a launchd would look like this

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>name of launchd</string>
    <key>ProgramArguments</key>
    <array>
        <string>[terminal command to execute]</string>
    </array>
    <key>RunAtLoad</key>
    <[(true)/(false)]/>
    <key>StartCalendarInterval</key>
    <dict>
        [any of these options are removable]
        [for everyday remove the Day and Weekday tags]
        <key>Day</key>
    <integer>[day of the month]</integer>
    <key>Weekday</key>
    <integer>[day of the week 0-7 - (sunday=0);]</integer>
        <key>Hour</key>
        <integer>[hour of day (military time)]</integer>
        <key>Minute</key>
        <integer>[minute]</integer>
    </dict>
</dict>
</plist>

any of the [] are either comments (must be removed) or a list of options

Use of the launchctl command would be used to mount the launchd