def main(): name = 'ip-streamer' fullname = 'IP Streamer v2' version = ipstreamer.__version__ runtime_directory = '/var/run/ip-streamer' system_config_directory = '/etc/vice' default_pid = os.path.join(runtime_directory, 'server.pid') default_config = ipstreamer.cfg_filename if os.path.isfile(ipstreamer.cfg_filename) else os.path.join(system_config_directory, ipstreamer.cfg_filename) parser = argparse.ArgumentParser(description='VICE ip streamer to control mumudvb.') parser.add_argument('--version', action='version', version='%s %s' % (fullname, version)) parser.add_argument('--no-fork', action='store_false', dest='fork', default=True, help='run the process in the foreground') parser.add_argument('-c', '--config-file', dest='config_file', default=default_config, help='configuration file', metavar='FILE') parser.add_argument('-p', '--pid', dest='pid_file', default=default_pid, help='PID file', metavar='FILE') parser.add_argument('-d', '--debug', help='Run in debug mode', action='store_true', default=False, dest='debug') args = parser.parse_args() debug = args.debug path, cfg_file = os.path.split(args.config_file) if path: system_config_directory = path process.system_config_directory = system_config_directory ipstreamer.cfg_filename = process.config_file(cfg_file) # when run in foreground, do not require root access because of PID file in /var/run if args.fork: try: process.runtime_directory = runtime_directory process.daemonize(args.pid_file) except ProcessError, e: log.fatal("Cannot start %s: %s" % (fullname, e)) sys.exit(1) log.start_syslog(name)
def start_log(): log.start_syslog('openxcap') if Logging.directory: if not os.path.exists(Logging.directory): os.mkdir(Logging.directory) handler = RotatingFileHandler(os.path.join(Logging.directory, 'access.log'), 'a', 2*1024*1024, 5) handler.addFilter(IsAccessLog()) log.logger.addHandler(handler) for handler in log.logger.handlers: if isinstance(handler, log.SyslogHandler): handler.addFilter(IsNotAccessLog())
# These log lines will go to stdout. log.msg("Starting %s. Check syslog to see what's going on next." % name) log.msg("Use `watch -n .1 ls /tmp' to see how the pid file is created and deleted.") # Set the process to run in the background and create a pid file. # If daemonize is called without arguments or pidfile is None no pid file # will be created. pidfile = process.runtime_file('%s.pid' % name) try: process.daemonize(pidfile) except ProcessError, e: log.fatal(str(e)) sys.exit(1) # process was succesfully put in the background. Redirect logging to syslog log.start_syslog(name) # This log line will go to syslog log.msg('application started (running in the background)') # Add a signal handler for SIGUSR1 process.signals.add_handler(signal.SIGUSR1, signal_handler) # Add another signal handler for SIGUSR1. Mutliple handlers can be added # for a given signal by different components/modules/threads of the # application. The only limitation is that the first handler must be added # from the main thread. process.signals.add_handler(signal.SIGUSR1, signal_handler2) log.msg("sending SIGUSR1 to self") os.kill(os.getpid(), signal.SIGUSR1) log.msg("sleeping for 3 seconds")
log.msg( "Use `watch -n .1 ls /tmp' to see how the pid file is created and deleted." ) # Set the process to run in the background and create a pid file. # If daemonize is called without arguments or pidfile is None no pid file # will be created. pidfile = process.runtime_file('%s.pid' % name) try: process.daemonize(pidfile) except ProcessError, e: log.fatal(str(e)) sys.exit(1) # process was successfully put in the background. Redirect logging to syslog log.start_syslog(name) # This log line will go to syslog log.msg('application started (running in the background)') # Add a signal handler for SIGUSR1 process.signals.add_handler(signal.SIGUSR1, signal_handler) # Add another signal handler for SIGUSR1. Multiple handlers can be added # for a given signal by different components/modules/threads of the # application. The only limitation is that the first handler must be added # from the main thread. process.signals.add_handler(signal.SIGUSR1, signal_handler2) log.msg("sending SIGUSR1 to self") os.kill(os.getpid(), signal.SIGUSR1) log.msg("sleeping for 3 seconds")
message = "%s..." % message[:137] self._api.PostUpdate(message) except twitter.TwitterError, e: log.error("Twitter Error: %s" % e.message) else: con.execute("INSERT INTO twitts(id, content) VALUES(?, ?)", [twitt_id, message]) con.close() except sqlite.Error, e: log.fatal("SQLite error: %s" % str(e)) sys.exit(1) if __name__ == "__main__": if not Config.consumer_key or not Config.consumer_secret or not Config.access_token_key or not Config.access_token_secret: log.fatal("Please, fill the configuration file") sys.exit(1) usage = "usage: %prog [options]" parser = OptionParser(usage=usage) parser.add_option('-t', dest='tag', help='Hashtag to search for') options, args = parser.parse_args() if options.tag: log.start_syslog("twitterbot") bot = TwitterBot() bot.start(options.tag) else: parser.print_help() sys.exit(1)