Skip to main content

Daemonize a command and e-mail the results

Project description

Project Status: Active - The project has reached a stable, usable state and is being actively developed. https://travis-ci.org/jwodder/daemail.svg?branch=master https://codecov.io/gh/jwodder/daemail/branch/master/graph/badge.svg https://img.shields.io/pypi/pyversions/daemail.svg MIT License https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg

GitHub | PyPI | Issues | Changelog

daemail (pronounced “DEE-mayl”, like “e-mail” but with a D) is a Python script built on top of python-daemon for running a normally-foreground command in the background and e-mailing its output (by default, the combined stdout and stderr) once it’s done.

Installation

daemail requires Python 3.5 or higher. Just use pip for Python 3 (You have pip, right?) to install daemail and its dependencies:

python3 -m pip install daemail

Usage

daemail [-C|--chdir <directory>]
        [-D|--dead-letter <mbox>]
        [-e|--encoding <encoding>]
        [-E|--stderr-encoding <encoding>]
        [--foreground|--fg]
        [-f|--from|--from-addr <address>]
        [-F|--failure-only]
        [-l|--logfile <logfile>]
        [-M|--mime-type|--mime <mime-type>]
        [-n|--nonempty]
        [--no-stdout]
        [--no-stderr]
        [-S|--split]
        [--stdout-filename <filename>]
        [-Z|--utc]
        -t|--to|--to-addr <address>  [-t|--to|--to-addr <address> ...]
        [<send options>]
        <command> [<arg> ...]

where <send options> is one of:

-s|--sendmail <command>  # default

--mbox <mbox>

--smtp-host <host>
    [--smtp-port <port>]
    [--smtp-username <username>]
    [--smtp-password <password> | --smtp-password-file <file> | --netrc | --netrc-file <file>]
    [--smtp-ssl | --smtp-starttls]

Options

  • -C <directory>, --chdir <directory> — Change to <directory> after daemonizing but before running <command>; defaults to the current directory

  • -D <mbox>, --dead-letter <mbox> — If an error occurs when trying to send, append the e-mail (including a description of the error) to the file <mbox>; defaults to dead.letter. If the file already exists, it must be a valid mbox file.

  • -e <encoding>, --encoding <encoding> — Expect the stdout (and stderr, if --split is not in effect) of <command> to be in the given encoding; defaults to the preferred encoding returned by Python’s locale.getpreferredencoding. If decoding fails, the output will be attached to the e-mail as an application/octet-stream file named “stdout”.

    When --mime-type or --stdout-filename is also given, this option has no effect other than to set the default value for --stderr-encoding.

  • -E <encoding>, --stderr-encoding <encoding> — Expect the stderr of <command> to be in the given encoding; defaults to the value specified via --encoding or its default. If decoding fails, the stderr output will be attached to the e-mail as an application/octet-stream file named “stderr”.

    This option only has an effect when --split is given, either implicitly or explicitly.

  • --foreground, --fg — Run everything in the foreground instead of daemonizing. Note that command output will still be captured rather than displayed.

  • -f <address>, --from <address>, --from-addr <address> — Set the From: address of the e-mail. The address may be given in either the form “address@example.com” or “Real Name <address@example.com>.” If not specified, daemail will not set the From: header and will expect the mail command or SMTP server to do it instead.

  • -F, --failure-only — Only send an e-mail if the command failed to run or exited with a nonzero status

  • -l <logfile>, --logfile <logfile> — If an unexpected & unhandleable fatal error occurs after daemonization, append a report to <logfile>; defaults to daemail.log

    • Such an error is a deficiency in the program; please report it!

  • -s <command>, --sendmail <command> — Send e-mail by passing the message on stdin to <command> (executed via the shell, in the directory specified with --chdir or its default); default command: sendmail -i -t. This is the default if neither --mbox nor --smtp-host is specified.

  • --mbox <mbox> — “Send” e-mail by appending it to the mbox file <mbox>

  • -M <mime-type>, --mime-type <mime-type>, --mime <mime-type> — Attach the standard output of <command> to the e-mail as an inline attachment with the given MIME type. The MIME type may include parameters, e.g., --mime-type "text/html; charset=utf-16". If --stdout-filename is not also supplied, the attachment is named “stdout”. Implies --split.

  • -n, --nonempty — Do not send an e-mail if the command exited successfully and both the command’s stdout & stderr were empty or not captured

  • --no-stdout — Don’t capture the command’s stdout; implies --split

  • --no-stderr — Don’t capture the command’s stderr; implies --split

  • --smtp-host <host> — Send e-mail via SMTP, connecting to the given host. --smtp-host may be accompanied by the following options:

    • --smtp-port <port> — Connect to <host> on the given port; defaults to 25, or to 465 if --smtp-ssl is specified

    • --smtp-username <username> — Authenticate to the SMTP server using the given username. If a username is supplied (either on the command line or in a netrc file) but no password is, daemail will prompt the user for the SMTP password before daemonizing.

    • --smtp-password <password> — Authenticate to the SMTP server using the given password

    • --smtp-password-file <file> — Authenticate to the SMTP server using the contents of the given file (after removing the final line ending) as the password

    • --netrc — Fetch the SMTP username and/or password from ~/.netrc. If --smtp-username specifies a different username for the host than is given in the netrc file, the netrc file is ignored.

    • --netrc-file <file> — Like --netrc, but use the given file instead of ~/.netrc

    • --smtp-ssl — Use the SMTPS protocol to communicate with the server

    • --smtp-starttls — Use the SMTP protocol with the STARTTLS extension to communicate with the server

  • -S, --split — Capture the command’s stdout and stderr separately rather than as a single stream

  • --stdout-filename <filename> — Attach the standard output of <command> to the e-mail as an inline attachment with the given filename. If --mime-type is not also supplied, the MIME type of the attachment is deduced from the file extension, falling back to application/octet-stream for unknown extensions. Implies --split.

  • -t <address>, --to <address>, --to-addr <address> — Set the recipient of the e-mail. The address may be given in either the form “address@example.com” or “Real Name <address@example.com>.”

    • This option is required. It may be given multiple times in order to specify multiple recipients.

  • -Z, --utc — Show start & end times in UTC instead of local time

Caveats

  • Input cannot be piped to the command, as standard input is closed when daemonizing. If you really need to pass data on standard input, run a shell, e.g.:

    daemail bash -c 'command < file'

    or:

    daemail bash -c 'command | other-command'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

daemail-0.6.0.tar.gz (31.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

daemail-0.6.0-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file daemail-0.6.0.tar.gz.

File metadata

  • Download URL: daemail-0.6.0.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/46.0.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.9

File hashes

Hashes for daemail-0.6.0.tar.gz
Algorithm Hash digest
SHA256 e8e7fc7cde148941804a2fee6484a56fe3af1704b2e04b6ef0e4f194c90a3011
MD5 ade6fb0683c1617a3218080fde278178
BLAKE2b-256 78961839db44bf610feb6c9d8350f9517f08c4d7fbce6f7d31467fe2c63047c5

See more details on using hashes here.

File details

Details for the file daemail-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: daemail-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/46.0.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.9

File hashes

Hashes for daemail-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32d29a69e1324826d89fa38203f81750af9a801a24775c457e798bd3b6e065e5
MD5 ba97ec76d876c00c12b9d4b1ed718b28
BLAKE2b-256 e8dcd217bc51b1d7b274d82cde54f27d8d0a210e9151beb5798c484372a4e025

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page