返回首页 - Notes - 2016

简化系统控制


.services.rb脚本文件

platform, service, action = ARGV
platform, service, action = platform.downcase, service.downcase, action.downcase

actions = %w[ start restart stop up reup down ]
unless actions.include?(action)
  puts "操作非法 [ #{actions.join(' ')} ]"
  exit
end

if %w[ start up ].include?(action)
  cmd = "sudo service #{service} start"
  cmd = "launchctl load -w /usr/local/opt/#{service}/homebrew.mxcl.#{service}.plist" if platform == 'mac'
elsif %w[ restart reup ].include?(action)
  cmd = "sudo service #{service} restart"
  if platform == 'mac'
    cmd = "launchctl unload -w /usr/local/opt/#{service}/homebrew.mxcl.#{service}.plist"
    cmd << " && launchctl load -w /usr/local/opt/#{service}/homebrew.mxcl.#{service}.plist"
  end
elsif %w[ stop down ].include?(action)
  cmd = "sudo service #{service} stop"
  cmd = "launchctl unload -w /usr/local/opt/#{service}/homebrew.mxcl.#{service}.plist" if platform == 'mac'
end

system cmd

.bashrc配置文件

if [ `uname` == 'Darwin' ]; then
  platform='Mac'
elif [[ `uname` =~ ^CYGWIN ]]; then
  platform='Cygwin'
else
  platform='Linux'
fi

s () {
  ruby ~/.services.rb $platform $1 $2
}

使用方法


date:2016-09-03