Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add script to wrap cron jobs.

  • Loading branch information...
commit 1c1863e464b719fe5f60253c7b326995470687a6 1 parent e59b61d
Neil Williams authored May 04, 2012

Showing 1 changed file with 67 additions and 0 deletions. Show diff stats Hide diff stats

  1. 67  scripts/wrap-job
67  scripts/wrap-job
... ...
@@ -0,0 +1,67 @@
  1
+#!/usr/bin/python
  2
+"""
  3
+Wrap a command, setuid/setgid, change directory to the reddit
  4
+code, and send output to syslog.
  5
+
  6
+The following environment variables may be used to control
  7
+the environment the wrapped command runs in:
  8
+
  9
+REDDIT_USER
  10
+
  11
+    The user to run the job as. Defaults to "reddit".
  12
+
  13
+REDDIT_GROUP
  14
+
  15
+    The group to run the job as. Defaults to $REDDIT_USER.
  16
+
  17
+REDDIT_ROOT
  18
+
  19
+    The root directory of the reddit repo. Should contain
  20
+    r2/, scripts/ etc.
  21
+
  22
+REDDIT_LOG_FACILITY
  23
+
  24
+    The syslog facility to write messages to.
  25
+"""
  26
+
  27
+import os
  28
+import sys
  29
+import grp
  30
+import pwd
  31
+import syslog
  32
+import subprocess
  33
+
  34
+
  35
+# drop permissions
  36
+user = os.environ.get("REDDIT_USER", "reddit")
  37
+group = os.environ.get("REDDIT_GROUP", user)
  38
+uid = pwd.getpwnam(user).pw_uid
  39
+gid = grp.getgrnam(group).gr_gid
  40
+os.setgroups([])
  41
+os.setgid(gid)
  42
+os.setuid(uid)
  43
+
  44
+# change directory to the reddit code root
  45
+root = os.environ.get("REDDIT_ROOT", "/opt/reddit/lib/public")
  46
+r2_root = os.path.join(root, "r2")
  47
+os.chdir(r2_root)
  48
+
  49
+# configure syslog
  50
+job_name = os.environ.get("UPSTART_JOB", "-".join(sys.argv[1:]))
  51
+facility = getattr(syslog, "LOG_" + os.environ.get("REDDIT_LOG_FACILITY", "CRON"))
  52
+syslog.openlog(ident=job_name, facility=facility)
  53
+
  54
+# run the wrapped command
  55
+child = subprocess.Popen(sys.argv[1:],
  56
+                         stdout=subprocess.PIPE,
  57
+                         stderr=subprocess.STDOUT)
  58
+
  59
+# write out to syslog
  60
+for line in child.stdout:
  61
+    line = line.rstrip('\n')
  62
+    syslog.syslog(syslog.LOG_NOTICE, line)
  63
+    print line
  64
+
  65
+# our success depends on our child's success
  66
+child.wait()
  67
+sys.exit(child.returncode)

0 notes on commit 1c1863e

Please sign in to comment.
Something went wrong with that request. Please try again.