Reputation: 1515
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?
spawn
with exec
send "pawn node $customer_root_dir/$customer_app_dir/$service/$service_name.js &> /var/log/$service_name.log &\r"
but still have no luck. nohup
but the processes did not start and /var/log
doesn't contain my log files.How can I solve my problem?
Upvotes: 0
Views: 609
Reputation: 246807
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.
Upvotes: 3