简体   繁体   中英

Running node in background from expect script

I am trying to run several node services from the expect script, so to run them one by one, I need to run them in background. Running it in from console works fine (processes are up and started in background), like:

>node /path/to/service1/myService1.js &> /var/log/myService1.log &
>node /path/to/service2/myService2.js &> /var/log/myService2.log &

but running my expect script, which is like:

#!/usr/bin/expect

set customer_root_dir "/path"
set customer_app_dir "/to"
set services {"service1" "service2"}
set service_names {"myService1" "myService2"}


# Set the maximum match length
match_max [ expr 32 * 1024 ]

foreach service $services service_name $service_names {
    spawn node $customer_root_dir/$customer_app_dir/$service/$service_name.js &> /var/log/$service_name.log &
    sleep 2
}

puts "All services are running!"

I see that they are started not in background.. and all what comes after service file (eg service1.js ), I mean &> /var/log/myService1.log & , is ignored! If I type something like spawn node $customer_root_dir/$customer_app_dir/$service/$service_name.js abracadabra expect script will run it as just spawn node $customer_root_dir/$customer_app_dir/$service/$service_name.js

What I tried?

  • I tried to replace spawn with exec
  • I tried send "pawn node $customer_root_dir/$customer_app_dir/$service/$service_name.js &> /var/log/$service_name.log &\\r" but still have no luck.
  • I also tried nohup but the processes did not start and /var/log doesn't contain my log files.

How can I solve my problem?

This should work:

set js  $customer_root_dir/$customer_app_dir/$service/$service_name.js
set log /var/log/$service_name.log

exec nohup node $js < /dev/null >& $log &

Note that the Tcl redirection to write stdout and stderr to a file is >& (see exec man page). Also redirecting all stdio channels to disconnect from the terminal.

spawn is not the right command: you don't need to interact with the process, so just exec it and forget about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM