Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

variable

Posted on 2011-03-10
6
Medium Priority
?
239 Views
Last Modified: 2012-05-11
i am calling getenv() in my C++ code

If I call C++ executable directly then getenv() it reads the value correctly

However if I call the executable from within /etc/init.d/myDaemon  
"myDaemon "  then calls C++ executable, in this case getenv() does not read the value.
I have to manually set the env variable in /etc/init.d/my.exe in order my C++ executable to read it.

not sure why it behaves this way

0
Comment
Question by:learningunix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
parnasso earned 1000 total points
ID: 35097647
The reason is, with all likelyhood, the environment variable you are trying to read belongs to a certain user, different than the one that is executing your daemon. Therefore your deamon can not access via getenv() to the value you want.

Maybe changing your deamon to start as a different user or setting that environment variable to the user that its starting the deamon.
0
 
LVL 86

Expert Comment

by:jkr
ID: 35097668
How are you starting the executable that calls 'getenv()'? if you are using 'system()', this call will start a new shell and tyour changes to the environment are null and void, meaning the process won't find the environment variable you have set before:
NAME
     system -- pass a command to the shell

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <stdlib.h>

     int
     system(const char *string);

DESCRIPTION
     The system() function hands the argument string to the command inter-
     preter sh(1).  The calling process waits for the shell to finish execut-
     ing the command, ignoring SIGINT and SIGQUIT, and blocking SIGCHLD.

     If string is a NULL pointer, system() will return non-zero if the command
     interpreter sh(1) is available, and zero if it is not.

Open in new window


You should rather use one of the 'exec*()' functions, i.e.
void run_progrem(const char* pCmd) {

  /* Fork a child */

  if ((pid = fork()) == 0) {

      /* If execl() returns at all, there was an error. */

      if (execlp(pCmd, pCmd, NULL)) { /* Bye bye! */
        perror("execl");
        exit(128);
      }
    }
}

Open in new window

0
 

Author Comment

by:learningunix
ID: 35097827
can i set that env variable in bash script ?
export VAR=<value>

how to read the value in bash script?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:learningunix
ID: 35098008
In the myDaemon bash script if I do
echo $VAR

it did not print anything. meaning the bash script is somehow not  able to read the env variable
0
 
LVL 86

Expert Comment

by:jkr
ID: 35098030
You can do that in a bash script, sure - but this variable will only be valid in the same "shell", as soon as you start another one that is unrelated to the one you use 'export', the change will be lost.
0
 

Author Closing Comment

by:learningunix
ID: 35099715
thx
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question