2

Let me elaborate: I am trying to spawn the catkin_init_workspace from PHP, using the proc_open like so:

touch( "$dir/stderr.txt" );
chmod( "$dir/stderr.txt", 0755 );
$fp = fopen("$dir/stderr.txt", "w");
fclose($fp);

$descr = array(
                0 => array("pipe", 'r'), // stdin
                1 => array("pipe", 'w'), // stdout
                2 => array("file", "$dir/stderr.txt", "w")to file
              );

$pid = proc_open( "catkin_init_workspace", $descr, $pipes, $dir );

if (!is_resource( $pid) ) 
    throw new Exception ( "`catkin_init_workspace` exec failed");

else if ( is_resource( $pid ) )
{   
    fclose( $pipes[1] );
    $retval = proc_close( $pid );
}

The above code has worked with CMake, with GCC and other applications. However, when I try this with catkin_init_workspace, I get:

sh: 1: catkin_init_workspace: not found

Now, as far as I understand, catkin_init_workspace is a python script at:

/opt/ros/indigo/bin/catkin_init_workspace

I tried invoking it directly by using the absolute path, but that didn't work.

As a user, everything works fine. But not when I am executing via www-data, the user/group setup for Apache2.

ROS tutorial explain that I need to setup my environment variables, by running

source /opt/ros/indigo/setup.bash

Which I also tried doing via PHP, right before I invoke the proc_open, but to no avail. My understanding is that I need to setup the environment variables correctly.

Doing

export | grep ROS

shows:

declare -x ROSLISP_PACKAGE_DIRECTORIES="/home/alex/Projects/ros_ws/devel/share/common-lisp"
declare -x ROS_DISTRO="indigo"
declare -x ROS_ETC_DIR="/opt/ros/indigo/etc/ros"
declare -x ROS_MASTER_URI="http://localhost:11311"
declare -x ROS_PACKAGE_PATH="/home/alex/Projects/ros_ws/src:/opt/ros/indigo/share:/opt/ros/indigo/stacks"
declare -x ROS_ROOT="/opt/ros/indigo/share/ros"
declare -x ROS_TEST_RESULTS_DIR="/home/alex/Projects/ros_ws/build/test_results"

Are those the environment variables I need to setup for www-data to correctly invoke catkin?

If so, how do I pass as an env array to PHP's proc_open, those variables?

Ælex
  • 14,432
  • 20
  • 88
  • 129

1 Answers1

3

As you already figured out, source /opt/ros/indigo/setup.bash has to be called in advance, otherwise your environment is not set up to find the ROS commands.

When you did this in PHP, I guess you used something like an additional proc_open or exec call or something similar before you call proc_open("catkin_init_workspace", ...)?
By doing this, the environment is probably only set up for this single call and it is not kept until you run catkin_init_workspace in another proc_open-call.

Possible Solution

I cannot test this here right now (no PHP installed), but the following should work:

  1. Create a simple bash script with the following content:

    #!/bin/bash
    source /opt/ros/indigo/setup.bash
    catkin_init_workspace
    
  2. In PHP, call this script instead of catkin_init_workspace
luator
  • 4,769
  • 3
  • 30
  • 51
  • Yes that would make sense. I used shell_exec to call the setup.bash script, but then used proc_open to call catkin_init_workspace. I guess one solution is to do what you suggest, although to be honest, I would prefer to know how to setup the environment for proc_open, since it gives me a lot of control over spawned processes. – Ælex May 20 '15 at 16:33
  • @Alex: I see your point. Maybe it is possible to somehow source the `setup.bash` for the www-data user before starting apache. I have to admit that I don't know _how_ to do this, though. – luator May 20 '15 at 19:39