C++ getenv doesnt update

I'm trying to make a program that a bash script runs. I want the bash script to be able to change the state of the c++ program, and the only thing I could find was to use environment variables. Thing is, its seems getenv only gets the value at the time when the program was run.


export BLINK=1 ./blink & sleep 5s unset BLINK


int main(int args, char **argv) { char *blink = getenv("BLINK"); while(blink && blink[0] == '1') { std::cout << getenv("BLINK") << std::endl; usleep(500000); } return 1; }

So what this does is run the blink program, wait 5 seconds then unset the environment. The C++ program however always sees the enviorment value as 1 and never stops. How do I get an updated environment variable while the program is running? Or is there a better way to have a bash script control the state of a c++ program.

EDIT I should note, I do not want to just kill the process either because it has to turn off hardware when it ends.

-------------Problems Reply------------

It is not possible to modify program environment after it is started. You have to use another method of interprocess communication. The simplest one is to register handler for some signal to your app (e.g. SIGUSR1), and then send it using kill -SIGUSR1 <pid> command.

There are also other solutions available, e.g. create named pipe (using pipe shell command), and check periodically if someone wrote something to it. If yes, exit loop.

You can also use sockets if you want, but this could be more complicated.

The C++ programs environment is not changing when you change it in bash - since when you start your program it gets a copy of the parent's environment and has no access to the parent process's environment.

You can use a file to easily share data, however.


echo "1" > blink.txt
./blink &
sleep 5s
echo "0" > blink.txt


#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <unistd.h>
using namespace std;

string getfile(const string& filename) {
ifstream in(filename.c_str(), ios_base::binary);
in.exceptions(ios_base::badbit | ios_base::failbit | ios_base::eofbit);
return string(istreambuf_iterator<char>(in), istreambuf_iterator<char>());

int main(int argc, char* argv[]) {
string blink = getfile("blink.txt");
while(blink[0] == '1')
std::cout << getfile("blink.txt") << std::endl;

return 1;

Category:c# Views:1 Time:2018-11-05
Tags: linux bash getenv

Related post

  • Android - Custom Widget doesnt update 2010-05-11

    I'm trying to make a widget to my app, but it doesnt update. I just need to change the textview text and open an activity when a press a button, but none of them works... the code public void onUpdate(Context context, AppWidgetManager appWidgetManage

  • Gridview doesnt update after the delete command 2011-07-12

    Basically, I use objectdatasource with gridview.. Everything works, but the gridview, doesnt update as soon as I click the delete button. This forces me to use Server.Transfer on the gridviews events: protected void GridView1_RowDeleted(object sender

  • LG Optimus 7: No new email notifications with Exchange. No email icon in lock screen and live tile doesnt update with no. of unread emails?? 2012-03-26

    Hi, I have set my WP7 up to my exchange 2010 email. However for some reason it doesnt notify me when i get a new email, or display any email icon in the lock screen and the live tile doesnt update with the no. of unread emails. The emails are being d

  • f1 2011 gfwl doesnt update AT ALL 2013-10-08

    f1 2011 doesnt update gfwl for me. ive tried all kinds of fixes including opening ports, downloading manually, changing ip, restarting, trying again, basically been at it for 5 hours. this isnt why i paid so much for this game. i shoukldnt have to do

  • Hi my samsung focus doesnt update to the new mango software.. It stays stuck in error code 8000405 can anyone help?? 2013-12-31

    Hi my samsung focus doesnt update to the new mango software.. It stays stuck in error code 8000405 can anyone help?? --------------Solutions------------- At what point does it fail? Check out this thread to see if the solution given helps you out: ht

  • pdo prepared statement doesnt update 2011-04-08

    This doesnt seem to update the record. Can anyone see why? try { $status = 'OK'; $messId = 179981; #die(var_dump($messId, $this->campaignId, $this->userId, $status)); $stmt = $this->dbh->prepare("UPDATE tbl_inbound_responses SET status =

  • ListBox DataBound to ObservableCollection in Windows Phone MVVM doesnt update after Add 2011-12-20

    I have an ObservableCollection in the ViewModel that add new entries when an ApplicationBar button is pressed in the View. The ListBox which is bound to this ObservableCollection doesnt show the new/updated entry, it does show the items of the collec

  • WPF Dependency Properties Settings binding doesnt update 2012-02-16

    Ive already been through about 30 different posts and changed how I handle this and it gets closer but still wont work. I have a Custom Button control in a library, that I am using in Main application. The button displays, handles mouse overs and suc

  • First column in spreadsheet doesnt update on second spreadsheet using "=Sheet!A1" 2012-05-15

    The first column of spreadsheet A should also be on spreadsheet B within the same file, to do this I use "=Sheet!A1" Now that works fine, but if after applying the formulas I edit the original first column again, the second spreadsheet doesnt automat

  • Battery indicator doesnt update charge win 8.1 2015-01-31

    have a new laptop with win 8.1 pre installed. the first night the laptop charged as normal. after hitting 100% unplugged to drain the battery. reconnected around 15% and shutdown, left it charging overnight. Next day the % charged not increased at al

  • Subscribed Calendar doesnt update in a timely manner. 2015-03-01

    I have SUBSCRIBED to my google calendar so I can also view events in my Hotmail calendar. After I initially subscribe, all the items in my Google calendar appear in the Hotmail calendar as I expect. But when I add a new item in my Google calendar, th

  • Changing database collation doesnt update collation of varchar/text fields in the DB (MS SQL) 2009-05-05

    I changed the collation of the database. All of the text/varchar columns before the change were set to database default. When the change was made to the DB collation, I would have expected the columns that were set to database default to remain datab

  • MVC - Ajax form - return partial view doesnt update in target 2009-07-03

    I have an index view that I want to update automatically as the user types in a client id. I got something similiar to work (only it was updating just a label) - but this will not work. What happens is the partial is just rendered by itself (not in p

  • nHibernate one-to-many inserts but doesnt update 2009-11-14

    Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has anyone ever had a one-to-many where the child object ge

  • django select widget doesnt update when i pass the choices into it 2010-11-17

    so im trying to update in my view a select widget as part of a form. i'e seen tons of stuff on how to do it, i've followed it and got nearly there. i've got a bit of code below which is called to populate the select with the choices and it does, but

  • Javascript doesnt update 2011-02-01

    I have a function that passes a parameter which is a function call and then i use setTimeout to call this passed function call. now i tried two methods with setTimout to raise the event and i used function.call(). When this passed parameter function

  • updatepanel doesnt update correctly, must click twice to update 2011-02-18

    I am using an update panel and when I click a button it will update all the panels. updapanel1.update() is very simple, but my data is not updating unless I hit the button twice. My gridviews shows data for a selected user, and then all the grids ref

  • CPAN::Mini::Inject doesnt update 03modlist.data.gz 2011-04-06

    I have an intention of creating a empty local CPAN directory, and then inject the modules i need with the right version numbers. So, i create a CPAN structure with CPAN::Mini like this: my $local = '/tmp/CPAN'; my $minicpan = CPAN::Mini->new( remo

  • Oracle entity in VS entity framework doesnt update the primary key in code 2011-09-08

    I have an entity called Tree, which has a primary key called Id (with StoreGeneratedPattern = Identity) that when using the following code, correctly inserts into the database. using(TestContainer tss = new TestContainer()) { Tree tree = new Tree() {

  • knockoutjs and jQuery mobile - checkbox doesnt update value 2011-09-19

    When binding a checkbox to the checked binding and using jQuery mobile the observable does not get updated when the enhanced checkbox is checked. see here I have looked at the code for both libraries and it seems that knockoutjs is bound to the click

  • android custom list view adapter doesnt update number of views in getView 2012-01-07

    I have a listView with a custom adapter. I have an edit text view in my activity where the user can type to filter the content on the list view. Lets say I have two items on the list view. After doing the filter, the ArrayList is reduced to one eleme

  • iPhone CAKeyframeAnimation doesnt update position property? 2012-02-02

    I have written animation for layer, that moves layer from one position to another: - (void) animateCentreRightTransition: (int) transition { float scaleFactor[2] = {1, 0.8}; if (transition == WatchTransitionClockwisePortrait || transition == WatchTra

  • adapter.notifyDataSetChange() doesnt update my adapter 2012-03-19

    This is my code: public class ListasCompra extends ListActivity { private ArrayList<Lista> listaCompras = null; private ListaAdapter adaptador = null; private static ListasCompra instancia = null; private static Context context = null; private

  • what to do when the windows doesnt update properly and it shows an error after it restarts and reverts the changes ? 2012-06-02

    My computer shows 20 updates to be installed. When I click on update, It installs successfully and asks me to restart my computer. When I restart, it shows an error and reverts the changes. When I check for the updates it shows the same updates as in

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.210 (s). 11 q(s)