Skip to content

Instantly share code, notes, and snippets.

@t-oginogin
Last active August 29, 2015 14:12
Show Gist options
  • Save t-oginogin/49a257dd0e5802e3400d to your computer and use it in GitHub Desktop.
Save t-oginogin/49a257dd0e5802e3400d to your computer and use it in GitHub Desktop.
Railsから複数行のLinuxコマンドを非同期で実行させる ref: http://qiita.com/t_oginogin/items/10da086566a092845af4
:
def self.start(daemon, args) #:nodoc:
:
fork do
Process.setsid
exit if fork
open(daemon.pid_file, 'w') { |f| f << Process.pid }
:
end
:
end
:
$ echo "sleep 100000" > ~/bin/test_command
$ chmod +x ~/bin/test_command
$ ps aux | grep sleep
ogi 3954 0.0 0.0 2432784 504 s001 R+ 4:17PM 0:00.00 grep sleep
ogi 3838 0.0 0.0 2432764 468 ?? S 4:08PM 0:00.00 sleep 100000
$ ps -p 4043 -o "pgid"
PGID
2947
$ kill -- -2947
`kill -TERM -2947`
$ rails runner JobTask.execute
"4308"
$ ps aux | grep test_command
ogi 4317 0.0 0.0 2423368 216 s001 R+ 4:45PM 0:00.00 grep test_command
ogi 4308 0.0 0.0 2442580 568 ?? S 4:44PM 0:00.00 sh -c test_command > test.log 2>&1 & echo $!
$ ps aux | grep sleep
ogi 4324 0.0 0.0 2432784 472 s001 R+ 4:45PM 0:00.00 grep sleep
ogi 4309 0.0 0.0 2432764 468 ?? S 4:44PM 0:00.00 sleep 100000
$ rails runner "JobTask.cancel(4308)"
$
$ ps aux | grep test_command
ogi 4456 0.0 0.0 2424588 388 s001 R+ 4:48PM 0:00.00 grep test_command
$ ps aux | grep sleep
ogi 4467 0.0 0.0 2432784 508 s001 R+ 4:50PM 0:00.00 grep sleep
system_command "kill -TERM -#{pgid}"
p "canceled!"
$ rails runner JobTask.execute
"4520"
$ ps -p 4520 -o "pgid"
PGID
4449
$ ps 4449
PID TT STAT TIME COMMAND
4449 ?? Ss 0:05.35 spring app | background_linux_command | started 8 mins ago | development mode
$ rails runner JobTask.execute
"4877"
$ ps -p 4877 -o "pgid"
PGID
4875
$ ps 4875
PID TT STAT TIME COMMAND
$ rails c
Loading development environment (Rails 4.1.8)
irb(main):001:0> JobTask.execute
=> 5003
irb(main):002:0> "5005"
irb(main):003:0* exit
$ ps -p 5005 -o "pgid"
PGID
5003
$ ps 5003
PID TT STAT TIME COMMAND
5003 ?? Z 0:00.00 (ruby)
$ rails runner "JobTask.cancel(4877)"
"canceled!"
$ ps aux | grep test_command
ogi 4951 0.0 0.0 2432784 488 s001 R+ 5:18PM 0:00.00 grep test_command
$ ps aux | grep sleep
ogi 4958 0.0 0.0 2432784 500 s001 R+ 5:18PM 0:00.00 grep sleep
$ rails runner JobTask.execute
"5507"
$ ps aux | grep test_command
ogi 5522 0.0 0.0 2432784 496 s001 R+ 5:49PM 0:00.00 grep test_command
ogi 5507 0.0 0.0 2442580 568 ?? S 5:49PM 0:00.00 sh -c tmp/test_command > tmp/test.log 2>&1 & echo $!
$ ps aux | grep sleep
ogi 5529 0.0 0.0 2424588 404 s001 R+ 5:49PM 0:00.00 grep sleep
ogi 5508 0.0 0.0 2432764 468 ?? S 5:49PM 0:00.00 sleep 100000
$ rails runner "JobTask.cancel(5507)"
"canceled!"
$ ps aux | grep test_command
ogi 5572 0.0 0.0 2442000 616 s001 S+ 5:50PM 0:00.00 grep test_command
$ ps aux | grep sleep
ogi 5579 0.0 0.0 2432784 512 s001 R+ 5:50PM 0:00.00 grep sleep
$ ls tmp/
cache test.log test_command
$ cat tmp/test_command
echo "start sleep"
sleep 100000
echo "end sleep"
$ cat tmp/test.log
start sleep
$ rails runner JobTask.execute
3377
$ ps aux | grep test_command
ogi 3388 0.0 0.0 2432784 580 s001 R+ 3:37PM 0:00.00 grep test_command
ogi 3378 0.0 0.0 2442580 580 ?? S 3:35PM 0:00.00 sh -c test_command > test.log 2>&1 &
$ rails runner JobTask.execute
"3836"
$ ps aux | grep test_command
ogi 3847 0.0 0.0 2432784 524 s001 R+ 4:08PM 0:00.00 grep test_command
ogi 3836 0.0 0.0 2442580 580 ?? S 4:08PM 0:00.00 sh -c test_command > test.log 2>&1 &
$ rails runner "JobTask.cancel(3836)"
$ ps aux | grep test_command
ogi 3944 0.0 0.0 2423368 184 s001 R+ 4:14PM 0:00.00 grep test_command
class JobTask
class << self
def execute
execute_job
end
def cancel(pid)
command = "ps -p #{pid} -o \"pgid\""
pgid = system_command(command).lines.to_a.last.lstrip.chomp
if pgid =~ /[0-9]/
system_command "kill -TERM -#{pgid}"
p "canceled!"
else
Rails.logger.error 'Process was not found'
end
end
private
def create_command
command = <<-"EOS"
echo "start sleep"
sleep 100000
echo "end sleep"
EOS
script_path = "tmp/test_command"
File.open(script_path, "w", 0755) do |f|
f.write command
end
# execute background and return pid
"#{script_path} > tmp/test.log 2>&1 & echo $!"
end
def execute_job
begin
fork do
Process.setsid
pid = system_command(create_command).lstrip.chomp
if pid =~ /[0-9]/
p pid
else
Rails.logger.error 'command has not pid'
end
end
rescue => e
Rails.logger.error e.message
end
end
def system_command(command)
`#{command}`
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment