@@ -198,7 +198,15 @@ COMMANDS = { 'sim' =>
198
198
class Cmd
199
199
200
200
def killProcess ( pid , name , timeout )
201
- Process . kill ( "-INT" , pid )
201
+ if RbConfig ::CONFIG [ 'host_os' ] =~ /mswin|mingw|cygwin/
202
+ # On Windows, the Ruby signal machinery does not properly work,
203
+ # so let's just use system level commands, see
204
+ # https://blog.simplificator.com/2016/01/18/how-to-kill-processes-on-windows-using-ruby/
205
+ print ( "Calling Process.kill INT with pid #{ pid } \n " )
206
+ system ( "taskkill /pid #{ pid } " )
207
+ else
208
+ Process . kill ( "-INT" , pid )
209
+ end
202
210
203
211
sleepSecs = 0.001
204
212
iterations = ( timeout / sleepSecs ) . to_i
@@ -220,7 +228,11 @@ class Cmd
220
228
221
229
if killedPid != pid
222
230
puts "Escalating to SIGKILL on [#{ name } ]"
223
- Process . kill ( "-KILL" , pid )
231
+ if RbConfig ::CONFIG [ 'host_os' ] =~ /mswin|mingw|cygwin/
232
+ system ( "taskkill /f /pid #{ pid } " )
233
+ else
234
+ Process . kill ( "-KILL" , pid )
235
+ end
224
236
end
225
237
end
226
238
@@ -380,6 +392,8 @@ class Cmd
380
392
end # parse()
381
393
382
394
def execute ( args )
395
+ # The parse method modifies args
396
+ original_args = args . clone ( )
383
397
options = parse ( args )
384
398
385
399
library_name_path = Pathname . new ( LIBRARY_NAME )
@@ -527,41 +541,51 @@ See https://github.com/gazebosim/gz-sim/issues/44 for more info."
527
541
exit ( -1 )
528
542
end
529
543
530
- if plugin . end_with? ".dll"
531
- puts "`ign gazebo` currently only works with the -s argument on Windows.
532
- See https://github.com/gazebosim/gz-sim/issues/168 for more info."
533
- exit ( -1 )
534
- end
535
-
536
- serverPid = Process . fork do
537
- ENV [ 'RMT_PORT' ] = '1500'
538
- Process . setpgid ( 0 , 0 )
539
- Process . setproctitle ( 'gz sim server' )
540
- Importer . runServer ( parsed ,
541
- options [ 'iterations' ] , options [ 'run' ] , options [ 'hz' ] ,
542
- options [ 'initial_sim_time' ] , options [ 'levels' ] ,
543
- options [ 'network_role' ] , options [ 'network_secondaries' ] ,
544
- options [ 'record' ] , options [ 'record-path' ] ,
545
- options [ 'record-resources' ] , options [ 'log-overwrite' ] ,
546
- options [ 'log-compress' ] , options [ 'playback' ] ,
547
- options [ 'physics_engine' ] ,
548
- options [ 'render_engine_server' ] ,
549
- options [ 'render_engine_server_api_backend' ] ,
550
- options [ 'render_engine_gui' ] ,
551
- options [ 'render_engine_gui_api_backend' ] ,
552
- options [ 'file' ] , options [ 'record-topics' ] . join ( ':' ) ,
553
- options [ 'wait_gui' ] ,
554
- options [ 'headless-rendering' ] , options [ 'record-period' ] ,
555
- options [ 'seed' ] )
556
- end
544
+ if not( plugin . end_with? ".dylib" or plugin . end_with? ".dll" )
545
+ # Not on Windows or macOS, Process.fork works fine
546
+ serverPid = Process . fork do
547
+ ENV [ 'RMT_PORT' ] = '1500'
548
+ Process . setpgid ( 0 , 0 )
549
+ Process . setproctitle ( 'gz sim server' )
550
+ Importer . runServer ( parsed ,
551
+ options [ 'iterations' ] , options [ 'run' ] , options [ 'hz' ] ,
552
+ options [ 'initial_sim_time' ] , options [ 'levels' ] ,
553
+ options [ 'network_role' ] , options [ 'network_secondaries' ] ,
554
+ options [ 'record' ] , options [ 'record-path' ] ,
555
+ options [ 'record-resources' ] , options [ 'log-overwrite' ] ,
556
+ options [ 'log-compress' ] , options [ 'playback' ] ,
557
+ options [ 'physics_engine' ] ,
558
+ options [ 'render_engine_server' ] ,
559
+ options [ 'render_engine_server_api_backend' ] ,
560
+ options [ 'render_engine_gui' ] ,
561
+ options [ 'render_engine_gui_api_backend' ] ,
562
+ options [ 'file' ] , options [ 'record-topics' ] . join ( ':' ) ,
563
+ options [ 'wait_gui' ] ,
564
+ options [ 'headless-rendering' ] , options [ 'record-period' ] ,
565
+ options [ 'seed' ] )
566
+ end
557
567
558
- guiPid = Process . fork do
559
- ENV [ 'RMT_PORT' ] = '1501'
560
- Process . setpgid ( 0 , 0 )
561
- Process . setproctitle ( 'gz sim gui' )
562
- Importer . runGui ( options [ 'gui_config' ] , options [ 'file' ] ,
563
- options [ 'wait_gui' ] , options [ 'render_engine_gui' ] ,
564
- options [ 'render_engine_gui_api_backend' ] )
568
+ guiPid = Process . fork do
569
+ ENV [ 'RMT_PORT' ] = '1501'
570
+ Process . setpgid ( 0 , 0 )
571
+ Process . setproctitle ( 'gz sim gui' )
572
+ Importer . runGui ( options [ 'gui_config' ] , options [ 'file' ] ,
573
+ options [ 'wait_gui' ] , options [ 'render_engine_gui' ] ,
574
+ options [ 'render_engine_gui_api_backend' ] )
575
+ end
576
+ else
577
+ server_args = original_args . clone ( )
578
+ # Add -s after sim to spawn a server
579
+ server_args . insert ( 1 , "-s" )
580
+ # Prepend gz script location
581
+ server_args . prepend ( $PROGRAM_NAME)
582
+ serverPid = Process . spawn ( "ruby" , *server_args )
583
+ gui_args = original_args . clone ( )
584
+ # Add -g after sim to spawn a server
585
+ gui_args . insert ( 1 , "-g" )
586
+ # Add gz script location
587
+ gui_args . prepend ( $PROGRAM_NAME)
588
+ guiPid = Process . spawn ( "ruby" , *gui_args )
565
589
end
566
590
567
591
Signal . trap ( "INT" ) {
0 commit comments