Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a program in /home/me/bin/foo, typically compiled from :

// includes
...
#define PATHNAME "/home/me/bin/bar.txt"

int main ()
{
   FILE* f;
   f = fopen(PATHNAME, "wb"); // I skip the possible error for freshness

   fputs(getenv("HOME"), f);

   fclose(f);

   return 0;
}

When I run the program from the directory in non-privileges user mode, the file bar.txt is created with /home/me written inside, that works fine..

.. until now my upstart configuration file in /etc/init looks like the following :

start on started tty2
exec /home/me/bin/foo

when I restart tty2, the file /home/me/bin/bar.txt is well created but nothing is written inside.

I've tried to write something else than getenv("HOME"), like a basic string but still nothing.

Why is that happening ? what should I do ?

update.1 : After few hours I just noticed that my pointer to the file was not handled properly.. Anyway except that little misunderstanding, that changes didn't solve my problem. I still have nothing in the file when I write getenv("HOME"). Why ? the process is executed as the root and getenv("HOME") is intended to write at least '/root'

share|improve this question
add comment

1 Answer 1

Commands executed by Upstart do not have a HOME environment variable.

I used upstart-socket-bridge on an unrelated project to print the environment variables (u-s-b adds a few special variables)

/etc/init/socket-test.conf:

description "upstart-socket-bridge test"
start on socket PROTO=inet PORT=34567 ADDR=127.0.0.1
setuid my_username
exec /bin/sh /tmp/socket-server

/tmp/socket-server:

#!/bin/sh
outfile=/tmp/outfile
date > $outfile            # Timestamp
printenv >> $outfile
exit 0

Trigger the job:

$ nc localhost 34567

And the output list of environment variables looks like:

$ cat /tmp/outfile
Wed Dec 11 07:23:50 CST 2013
UPSTART_INSTANCE=
PORT=34567
PROTO=inet
UPSTART_JOB=socket-test
UPSTART_FDS=12
TERM=linux
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
UPSTART_EVENTS=socket
PWD=/
ADDR=127.0.0.1
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.