forked from maccman/juggernaut
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:Yoomee/juggernaut
- Loading branch information
Showing
3 changed files
with
68 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,44 @@ | ||
if File.exists?("node.pid") | ||
puts "Already runnning with pid #{File.read("node.pid")}" | ||
def get_pid(command) | ||
%x{ps -ef | grep \"#{command}\"}.split("\n").each do |line| | ||
if !line.match(/grep/) && res = line.strip.match(/^\S*\s+(\d+)\s/) | ||
return res[1] | ||
end | ||
end | ||
nil | ||
end | ||
|
||
def get_child_process(ppid) | ||
%x{ps -ef | grep #{ppid}}.split("\n").each do |line| | ||
if !line.match(/grep/) && res = line.strip.match(/^.*\s+(\d+)\s+#{ppid}\s/) | ||
return res[1] | ||
end | ||
end | ||
nil | ||
end | ||
|
||
def pid_file_path | ||
"/var/run/juggernaut.pid" | ||
end | ||
|
||
def root_path | ||
File.dirname(__FILE__) + "/../" | ||
end | ||
|
||
def delete_pid_file | ||
%x{rm #{pid_file_path}} if File.exists?(pid_file_path) | ||
end | ||
|
||
if pid = get_pid("node server.js") | ||
File.open(pid_file_path, 'w') {|f| f.write(pid)} | ||
puts "Already runnning with pid #{pid}." | ||
else | ||
pid = IO.popen("node server.js &> node.log").pid | ||
File.open("node.pid", 'w') {|f| f.write(pid)} | ||
puts "Started node server with pid #{pid}." | ||
delete_pid_file | ||
%x{mkdir -p /var/run} | ||
ppid = IO.popen("cd #{root_path} && node server.js &> /var/log/node.log").pid | ||
if (pid = get_child_process(ppid)) | ||
File.open(pid_file_path, 'w') {|f| f.write(pid)} | ||
puts "Started node server with pid #{pid}." | ||
else | ||
puts "Failed to start node server." | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,27 @@ | ||
pid = File.read("node.pid") | ||
%x{ps -ef | grep #{pid}}.split("\n").each do |line| | ||
if !line.match(/grep/) && res = line.match(/.*\s(\d+)\s#{pid}\s/) | ||
cpid = res[1] | ||
%x{kill #{cpid}} | ||
def get_pids(command) | ||
pids = [] | ||
%x{ps -ef | grep \"#{command}\"}.split("\n").each do |line| | ||
if !line.match(/grep/) && res = line.strip.match(/^\S*\s+(\d+)\s/) | ||
pids << res[1] | ||
end | ||
end | ||
pids | ||
end | ||
%x{kill #{pid}} | ||
%x{rm node.pid} | ||
puts "Stopped node server." | ||
|
||
def pid_file_path | ||
"/var/run/juggernaut.pid" | ||
end | ||
|
||
def delete_pid_file | ||
%x{rm #{pid_file_path}} if File.exists?(pid_file_path) | ||
end | ||
|
||
(pids = get_pids("node server.js")).each do |pid| | ||
%x{kill #{pid}} | ||
end | ||
if pids.empty? | ||
puts "node server isn't running." | ||
else | ||
puts "Stopped node server." | ||
end | ||
delete_pid_file |