The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   can't send email via script using launchd? (http://hintsforums.macworld.com/showthread.php?t=94302)

mnewman 09-25-2008 10:24 PM

Quote:

Originally Posted by tw (Post 495510)
my guess would be that mail triggers the postfix master directly, rather than waiting for launchd to do it.

Right you are. I just sent mail from the command line and the mail went out immediately.

hayne 09-26-2008 12:28 AM

Quote:

Originally Posted by tw (Post 495510)
my guess would be that mail triggers the postfix master directly, rather than waiting for launchd to do it. that would make the pfm a child process of mail, rather than of launchd. but I can't seem to find any documentation of the inner workings of mail to confirm that.

Use the Source Luke !
Quote:

Originally Posted by from mail_cmds-22/mail/send.c
Code:

        /*
        * Fork, set up the temporary mail file as standard
        * input for "mail", and exec with the user list we generated
        * far above.
        */
        pid = fork();
        if (pid == -1) {
                warn("fork");
                savedeadletter(mtf);
                goto out;
        }
        if (pid == 0) {
                sigset_t nset;
                (void)sigemptyset(&nset);
                (void)sigaddset(&nset, SIGHUP);
                (void)sigaddset(&nset, SIGINT);
                (void)sigaddset(&nset, SIGQUIT);
                (void)sigaddset(&nset, SIGTSTP);
                (void)sigaddset(&nset, SIGTTIN);
                (void)sigaddset(&nset, SIGTTOU);
                prepare_child(&nset, fileno(mtf), -1);
               
                if ((cp = value("sendmail")) != NULL)
                        cp = expand(cp);
                else
                        cp = _PATH_SENDMAIL;
                execv(cp, namelist);
               

                warn("%s", cp);
                _exit(1);
        }
        if (value("verbose") != NULL)
                (void)wait_child(pid);
        else
                free_child(pid);
out:
        (void)Fclose(mtf);
}


The 'fork', followed soon after by an 'exec' is the way that C programs start a child process. You see above that the child process that is 'exec'd is _PATH_SENDMAIL
which is defined in Libc-498/include/paths.h as:
#define _PATH_SENDMAIL "/usr/sbin/sendmail"

AngelHide 10-02-2008 11:36 AM

Hello,

I have the same problem, my script doesn't send e-mail as root with launchd !!
I wrote this in my script but it didn't work.
<key>AbandonProcessGroup</key>
<true/>
:(

tw 10-04-2008 05:19 PM

Quote:

Originally Posted by AngelHide (Post 496509)
Hello,

I have the same problem, my script doesn't send e-mail as root with launchd !!
I wrote this in my script but it didn't work.
<key>AbandonProcessGroup</key>
<true/>
:(

first, that should go in the launchd plist file, not in the script. beyond that, though, we'd need more information. can you show us part of your script, or your plist file?

AngelHide 10-10-2008 08:39 AM

Hello,

Well sorry for the mistake but I wrote it in the launchd plist file not in the script.

Code:

<?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>test</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/scripts/test.sh</string>
        </array>
        <key>StartCalendarInterval</key>
        <dict>
                <key>Hour</key>
                <integer>4</integer>
                <key>Minute</key>
                <integer>0</integer>
        </dict>
        <key>AbandonProcessGroup</key>
        <true/>
</dict>
</plist>

and the test script is :

Code:

#!/bin/sh
echo testing | mail -s 'this is a test' "myemail")

I have Leopard Server 10.5.5.
I make the plist file with lingon but it doesn't send e-mails.

rtoads 10-26-2008 01:25 PM

Launchd is for the birds
 
Quote:

Originally Posted by AngelHide (Post 497702)
and the test script is :

Code:

#!/bin/sh
echo testing | mail -s 'this is a test' "myemail")

I have Leopard Server 10.5.5.
I make the plist file with lingon but it doesn't send e-mails.

Your plist file looks correct. I bet the only problem is that you didn't unload the previous plist definition lacking the crucial AbandonProcessGroup key and reload the new one. One of the things that drives me nuts about launchd is that it's almost impossible to figure out which instance of a plist definition is loaded, so I find myself unloading and reloading the job to be absolutely sure. Lingon isn't much help here, suggesting that you either log out and log in OR reboot the server, but not telling you which or why. (Reboot the server? What is this, Windows?)

Some of you Unix gurus out there may see some benefit in dumping cron for this obtuse and difficult scheduler, but the very fact that it is bloody difficult to schedule a simple "email me" script underscores the fact that something that used to be *really* simple is now a big PITA. That is not progress. This discussion is the ONLY place on the web that I was able to turn up the source of this problem. I see there is an Apple technote on it as well, but it should be in the launchd man page. Well, maybe it is, but I wouldn't know:

myserver:~ admin$ man launchd
No manual entry for launchd
myserver:~ admin$ man launchctl
No manual entry for launchctl


Gee thanks, Apple.

NightFlight 09-14-2010 09:26 AM

Everything is easier once you know how.
 
Quote:

Lingon isn't much help here, suggesting that you either log out and log in OR reboot the server, but not telling you which or why. (Reboot the server? What is this, Windows?)
Lingon isn't Apple. Reading about launchctl leads you directly to using launchctl unload/load [-w]. If you are tired of typing the command repeatedly, you can either create an alias or simply add a bash function to your ~/.bashrc.


Quote:

Some of you Unix gurus out there may see some benefit in dumping cron for this obtuse and difficult scheduler, but the very fact that it is bloody difficult to schedule a simple "email me" script underscores the fact that something that used to be *really* simple is now a big PITA. That is not progress.
launchd is a huge advance, but it is not primarily a scheduler. It by happenstance... does schedule and a whole lot more. I'll admit, at first it was a bit daunting and it can create a few "gotchas", but over all it's a better tool. Besides, there's nothing stopping you from installing crond.:rolleyes:

myserver:~ admin$ man launchd
No manual entry for launchd
myserver:~ admin$ man launchctl
No manual entry for launchctl



myserver:~ admin$ man launchd.plist
launchd.plist(5) BSD File Formats Manual launchd.plist(5)

NAME
launchd.plist -- System wide and per-user daemon/agent configuration files

DESCRIPTION
This document details the parameters that can be given to an XML property list that can be loaded into launchd with launchctl.


Dropping launchd into google brings the following link up: http://developer.apple.com/macosx/launchd.html

Quote:

Gee thanks Apple.
Once you lift the skirt of the OS, your on your own. If it gets a bit hard and you have trouble keeping up that's your problem, not theirs. They keep the GUI and the basic tools simple, and that's whats supported. :D

At least the unix layer is accessible. If it wasn't, this would all be moot.

jweisbin 09-29-2010 05:08 PM

Quote:

Originally Posted by edjusted (Post 495161)
I'm trying to send an email via shell script. e.g. my shell script is something like this:
#!/bin/sh
echo testing | mail -s 'this is a test' user@domain.com

My launchdaemon runs the script: /usr/local/scripts/testscript.sh

I *know* the script is working, because I added some debugging (I added a line: echo is this working and sure enough, "is this working" is showing up in the console) but the email part kicks off an error message about 30 seconds later: Stray process with PGID equal to this dead job: PID ### PPID 1 sendmail

The script *works* when I run it with a cron job. But why wouldn't it work as a launchd item? And why is it I'm only having problems with the email segment of my scripts? I've tried other scripts and they all seem to work fine.

I have the same problem, but it only started in OS X Server 10.6 (a clean install), in 10.5.8 this was not a problem. The plists and the scripts are exactly the same between the two. They are loaded in launchctl and running, but just won't send email when run from launchd. When run as a script (they are meant to run as root) they run fine. I do not get any "stray process" messages at all. I do get this message, not sure if its related:

sandboxd[49674] krb5kdc(73) deny mach-lookup com.apple.CoreServices.coreservicesd

Here's the script This is only the portion, the other part does other stuff and is working, so it's not permission of the script):

#!/bin/sh
# email subject
SUBJECT="test script got an error"
# Email To ?
EMAIL="abc@xxxyyyzzz.com"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "test script got an error" > $EMAILMESSAGE
# send an email using /bin/mail
/usr/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

Here is the plist, in /Library/LauchDaemons:

-rw-r--r--@ 1 root wheel 509 Sep 29 17:00 com.humanworldwide.test.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.humanworldwide.test</string>
<key>LowPriorityIO</key>
<false/>
<key>Program</key>
<string>/Users/humanadmin/Desktop/scripts/test.sh</string>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>5</integer>
<key>Minute</key>
<integer>02</integer>
</dict>
</dict>
</plist>

jweisbin 09-30-2010 09:13 AM

I have the same problem, however only on Snow Leopard Server - not on Leopard Server. The AbandonProcessGroup key does seem to fix the problem, but why this should only happen on SL for me is a mystery.

c-l 10-20-2010 11:45 AM

I had the same problem and solved it with that solution and additionally adapting the postfix.plist (/System/Library/LaunchDaemons/org.postfix.master.plist):

Code:

<?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>AbandonProcessGroup</key>
        <true/>
        <key>Label</key>
        <string>org.postfix.master</string>
        <key>OnDemand</key>
        <false/>

        <key>Program</key>
        <string>/usr/libexec/postfix/master</string>
        <key>ProgramArguments</key>
        <array>
                <string>master</string>
        </array>
        <key>QueueDirectories</key>
        <array>
                <string>/var/spool/postfix/maildrop</string>
        </array>
      <key>RunAtLoad</key>
        <true/>

</dict>
</plist>

HTH


All times are GMT -5. The time now is 05:45 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.