3
  • Startup scripts (on boot)
  • Processes running in (or sent to) the background (when using SSH -fN for example)
  • Services
  • etc.

What $path variable will they be using? What credentials? Anything else I should know?

Tried googling it, and found a lot of fragmented info in forums but nothing giving a clear/complete picture of what's going on.

FYI I'm running Raspbian which is pretty much the same as Debian.

2 Answers 2

3

Depending on the phase in which the processes were booted they might not have any environment variables at all, inherited them from the parent process or using variables which were in the startup (init.d) script.

You can find the environment variables the process has by examining /proc/$PID_OF_PROCESS/environ:

root@frisbee:~# xargs -n 1 -0 < /proc/1243/environ UPSTART_INSTANCE= UPSTART_JOB=rsyslog TERM=linux PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin UPSTART_EVENTS=filesystem PWD=/ 

You can find a lot of information about a process by examining it's /proc/ directory. For example:

  • The current working directory of the process is symlinked to by /proc/$PID/cwd:

    root@frisbee:~# ls -l /proc/1243/cwd lrwxrwxrwx 1 root root 0 apr 30 11:20 /proc/1243/cwd -> / 
  • The binary from which the process was exec()'ed is symlinked by /proc/$PID/exe:

    root@frisbee:~# ls -l /proc/1243/exe lrwxrwxrwx 1 root root 0 apr 30 11:19 /proc/1243/exe -> /usr/sbin/rsyslogd 
  • The file descriptors (files + network connections + interprocess communication sockets) can be found in /proc/$PID/fd/:

    root@frisbee:/proc/1243/fd# ls -l total 0 lrwx------ 1 root root 64 apr 30 11:20 0 -> socket:[12362] l-wx------ 1 root root 64 apr 30 11:20 1 -> /var/log/syslog l-wx------ 1 root root 64 apr 30 11:20 2 -> /var/log/kern.log lr-x------ 1 root root 64 apr 30 11:20 3 -> /proc/kmsg l-wx------ 1 root root 64 apr 30 11:20 4 -> /var/log/auth.log l-wx------ 1 root root 64 apr 30 11:39 5 -> /var/log/ufw.log 

You can find as which user the process is running by examining ps output:

root@frisbee:/proc/1243# ps -fp 1243 UID PID PPID C STIME TTY TIME CMD syslog 1243 1 0 11:19 ? 00:00:01 rsyslogd -c5 

Anything else I should know?

Lots, but you have to define those things yourself and find them or ask another question about it.

2
  • Thanks for the info this will really help me understand the working environement of all the processes, exactly what I needed. Commented Apr 30, 2014 at 14:43
  • I just finished testing the commands you pointed out... I'm just amazed. This is going to be SOOOO useful. Thanks for going the extra mile in your answer. I'd +2 if I could. Commented Apr 30, 2014 at 15:40
2

Startup scripts (on boot)

Processes started by init usually have little or no pre-existing environment at all, although it depends on how they are invoked.

If they are invoked directly, i.e., forked and executed by the init daemon, $PATH will not be set. If they are invoked via the shell, $PATH may be set depending on the shell and how it is configured, but generally it still won't be set, which is why init scripts usually contain full paths.

Environment variables such as $PATH are generally set in shell scripts which may be automatically sourced when the shell starts; for an explanation of how bash, e.g., does this, see INVOCATION in man bash. For shells invoked non-interactively as sh (i.e., most init scripts), there are no files automatically sourced.

Environment variables are also inherited, but unless init (the first process on the system) sets some, then its children (such as executed init scripts) won't inherit anything.

If a specific boot service needs the environment set up a certain way, it is the responsibility of that service to set up such an environment for itself. Traditionally this is done by initializing variables in an init script.

Processes running in (or sent to) the background

Processes inherit their environment from their parent (the process which started them). Sending them to the background does not change this.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.