配置: puma_server_conf.rb
#!/usr/bin/env pumaapplication_path = '/srv/rorapps/discount_service'directory application_pathenvironment 'development'daemonize truepidfile "#{application_path}/tmp/pids/puma_4000.pid"state_path "#{application_path}/tmp/pids/puma_4000.state"stdout_redirect "#{application_path}/log/puma_4000.stdout.log", "#{application_path}/log/puma_4000.stderr.log"port 4000workers 8
啟動開關:puma.sh
#! /usr/bin/env bashcurrent_path=`cd "$(dirname "$0")"; pwd`. $current_path/function.shpuma_file=$current_path/../puma_conf/puma_service_4000.rbpuma_pid=$current_path/../../tmp/pids/puma_4000.pidecho "######### info #############"echo "PUMA DISCOUNT SERVICE 4000"echo "## $puma_file ## $puma_pid ##"echo "############################"case "$1" in start) puma -C $puma_file echo "puma_service_4000 start ............... $(command_status)" ;; status) check_run_status_from_pid_file $puma_pid 'puma' ;; stop) kill -s SIGTERM `cat $puma_pid` echo "puma_service_4000 stop ................ $(command_status)" ;; restart) # $0 stop # sleep 1 # $0 start kill -s SIGUSR2 `cat $puma_pid` ;; *) echo "tip:(start|stop|restart|status)" exit 5 ;;esacexit 0
初始檔案:
MRS_DATA_PATH=`ruby $current_path/parse_property.rb MRS_DATA_PATH`rails_env=`ruby $current_path/parse_property.rb RAILS_ENV`processor_pid=$MRS_DATA_PATH/pids/sidekiq.pidlog_file=$MRS_DATA_PATH/logs/sidekiq.logstart:create_file $processor_pidcreate_file $log_file
parse_property.rb
require "yaml"yaml: MRS_DATA_PATH : $HOME/DISCOUNT_SERVICE_DATA RAILS_ENV : developmentconfig = YAML.load_file(File.expand_path("../property.yaml",__FILE__))key = ARGV[0]value = config[key]value = value.gsub(/\/$/,"")if "MRS_DATA_PATH" == key `mkdir -p #{value}/logs` `mkdir -p #{value}/sockets` `mkdir -p #{value}/pids`endputs `echo #{value}`
工具檔案 function
#! /usr/bin/env bashfunction assert_process_from_name_not_exist(){ local pid pid=$(ps aux|grep $1|grep -v grep|awk '{print $2}') if [ "$pid" ];then echo "已經有一個 $1 進程在運行" exit 5 fi}function assert_process_from_pid_file_not_exist(){ local pid; if [ -f $1 ]; then pid=$(cat $1) if [ $pid ] && [ "$(ps $pid|grep -v PID)" ]; then echo "$1 pid_file 中記錄的 pid 還在運行" exit 5 fi fi}function check_run_status_from_pid_file(){ local pid; local service_name; service_name=$2 if [ -f $1 ]; then pid=$(cat $1) fi if [ $pid ] && [ "$(ps $pid|grep -v PID)" ]; then echo -e "$service_name [\e[1;32mrunning\e[0m]" else echo -e "$service_name [\e[1;31mnot running\e[0m]" fi}function get_sh_dir_path(){ echo -n $(cd "$(dirname "$0")"; pwd)}function command_status(){ if [ $? == 0 ];then echo -e "[\e[1;32msuccess\e[0m]" else echo -e "[\e[1;31mfail\e[0m]" fi}function create_file(){ local file_name; file_name=$1 if [ -d file_name ]; then echo "Directory Exists!" else touch file_name fi}