1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#--
# This file is part of Sonic Pi: http://sonic-pi.net
# Full project source: https://github.com/samaaron/sonic-pi
# License: https://github.com/samaaron/sonic-pi/blob/master/LICENSE.md
#
# Copyright 2013, 2014, 2015, 2016 by Sam Aaron (http://sam.aaron.name).
# All rights reserved.
#
# Permission is granted for use, copying, modification, and
# distribution of modified versions of this work as long as this
# notice is included.
#++
require 'tmpdir'
require 'fileutils'
require_relative "../core"
require_relative "../lib/sonicpi/util"
require 'sys-proctable'
include SonicPi::Util
tmp_dir = Dir.tmpdir
#f = File.open("log_path/spawn.log", 'a')
pids_store = tmp_dir + "/sonic-pi-pids"
unless File.exists? pids_store
log_process_info "No pids store found here: #{pids_store}\n"
log_process_info "Exiting\n"
exit
end
os = case RUBY_PLATFORM
when /.*arm.*-linux.*/
:raspberry
when /.*linux.*/
:linux
when /.*darwin.*/
:osx
when /.*mingw.*/
:windows
else
:unknown
end
if ARGV.empty?
pids = Dir.entries(pids_store) - [".", ".."]
else
pids = ARGV
end
log_process_info "\n\nClearing pids: #{pids.inspect}\n"
if pids.empty?
log_process_info "No pids to clear :-)\n"
exit
end
pids.each do |pid|
log_process_info "\nClearing [#{pid}]"
pid = Integer(pid)
pid_path = "#{pids_store}/#{pid}"
begin
orig_cmdline = File.readlines(pid_path)[0]
rescue
log_process_info " -- unable to read original cmdline for pid: #{pid}"
if File.exists? pid_path
FileUtils.rm pid_path
end
next
end
log_process_info " -- command #{orig_cmdline}"
if File.exists? pid_path
log_process_info " -- removing #{pid_path}"
FileUtils.rm pid_path
end
begin
info = Sys::ProcTable.ps(pid: pid)
raise unless info
rescue
log_process_info " -- unable to get ProcTable info for: #{pid}"
log_process_info " -- process: #{pid} not running"
next
end
# Don't kill process unless the command line arguments match
next unless info.cmdline.strip == orig_cmdline.strip
if os == :windows
# We're on Windows, so go straight for the jugular
log_process_info " -- force killing #{pid}"
begin
Process.kill(9, pid)
log_process_info " -- killed #{pid}"
rescue Exception => e
log_process_info " -- Could not kill #{pid} - perhaps already killed?"
end
else
next if pid == 0
cnt = 0
begin
8.times do
if cnt < 3
Process.kill(15, pid)
log_process_info " -- politely killing #{pid}"
else
Process.kill(9, pid)
log_process_info " -- force killing #{pid}"
end
sleep 0.5
cnt += 1
end
log_process_info " -- unable to kill #{pid}"
rescue Errno::ESRCH => e
if cnt == 0
log_process_info " -- process #{pid} already stopped"
else
log_process_info " -- killed #{pid}"
end
rescue Exception => e
log_process_info " -- error killing process #{pid} - #{e.class}, #{e.message}\n#{e.backtrace.inspect}"
end
end
end
log_process_info "\nFinished clearing pids\n\n"
|