Stdout for a subprocess when launching with --screen
I have the following three lines buried within a node (let's call it network_scanner.py
):
cmd = ["sudo", "iwlist", "wlan0", "scan"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
iwlist_output = proc.communicate()[0].decode('utf-8')
If I call my launch file (roslaunch network_scanner network_scanner.launch --screen
), iwlist_output
is completely empty.
If I run the node directly (rosrun network_scanner network_scanner.py
), I get expected output in iwlist_output
.
I'm assuming the problem is that proc
is unable to provide output to stdout
for some reason when I roslaunch
my node. Shouldn't --screen
fix that?
Edit: Question assumed that there was an issue with stdout
, but that wasn't the issue. The problem was that cmd
wasn't being properly called because of permissions. This post provides a solution.
--screen
redirects the output of your node; it shouldn't affect things that your node is running within subprocess.I've seen some of the wifi scanning commands return different results on consecutive runs, so you may want to try multiple runs to confirm that this isn't just random behavior
I haven't encountered this particular issue, but I've successfully used
commands.getstatusoutput(my_cmd_string)
from thecommands
module within a ROS node without any problems. However, "Using thesubprocess
module is preferable to using thecommands
module."