# right way to set PATH for startup scripts



## amarat (Sep 23, 2009)

I wonder is there a right way to set PATH environment variable for daemons we are starting via /usr/local/etc/rc.d/something startup scripts? each time I portupgrade apache I add the following line to /usr/local/etc/rc.d/apache:

set PATH=/usr/local/bin:/usr/local/sbin:$PATH

and it would be great if there exists a way to set this PATH globally, and be sure that I don't forget to add this line to distribution startup script


----------



## phoenix (Sep 23, 2009)

The global path is set in */etc/rc* but you probably don't want to edit that directly, as it will be overwritten everytime you upgrade the OS.  And you probably shouldn't be messing with the path for the system RC scripts.  There are binaries installed under */usr/local/*{sbin|bin} that have the same names as ones under */{sbin|bin}* and */usr/*{sbin|bin}.

Setting it in the individual script that needs it is a cleaner solution.

May I ask why the Apache RC script needs a different PATH?


----------



## amarat (Sep 23, 2009)

I know that messing with system-wide scripts is a bit of pain, so I'd rather prefer something like rc.conf variable, but, alas, there's no easy and elegant way to set machine-wide startup PATH, unfortunately. even login.conf doesn't work 

setting a different path for apache and its children is my compatibility issue, I have some bad-formed code, that spawns processes by their short name and I haven't a chance to change this code. 

Probably the question should be asked as: why user changed rc-script is deleted, while any other user-changed files are preserved?


----------



## Christopher (Sep 23, 2009)

Although I don't have an elegant solution to rc.d scripts needing a bigger $PATH than whats setup by /etc/rc, only a sloppy one of putting 


```
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:
```

in /etc/rc.conf.  Ugly.

I have ran into a similar problem with sloppy programmers calling binaries from web server scripts that are not in the default $PATH.  I fixed that by the below shell code section.


```
mkdir -p /usr/local/etc/apache22/envvars.d/
echo 'export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:' > /usr/local/etc/apache22/envvars.d/path.env
chmod 755 /usr/local/etc/apache22/envvars.d/path.env
```

The rc.d script for apache22 is kind enough to execute /usr/local/sbin/envvars, which will then source ${ENVVARS_DIR}/*.env into the rc.d scripts' environment.  Handy.


----------



## amarat (Sep 24, 2009)

I see. so, I'm doomed to make such changes every time I upgrade apache %) at least, while I'm bound to 1.3.x. Thanks phoenix, Christopher


----------



## Schroter (Aug 29, 2021)

Hi, I also have a similar problem I tried to set a path variable. But it only works for the current session. I have type the full path after a restart of computer. I am using tcsh on FreeBSD 13. Please find the below code for your kind perusal.

```
set path = ($path /usr/home/schroter/julia-1.6.2/bin/)
```
After the above code typed on terminal (tcsh) echo $path shows the path variable added. I could just run julia from home folder and start application. But after a restart it does not work and echo $path shows the path variable missing I have to type the full path to start application. Please help.shows the path variable added. After the above code typed on terminal (tcsh) I could just run julia from home folder and start application. But after a restart it does not work and echo $path shows the path variable missing I have to type the full path to start application. Please help.
Thanks & Best Regards
Schroter


----------



## tingo (Aug 29, 2021)

Schroter you need to put that path command in the startup script for your shell. For [t]csh, this is normally $HOME/.cshrc.
Example from one of my machines:

```
root@proxy:~ # grep path $HOME/.cshrc
set path = (/sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin $HOME/bin)
```
HTH


----------



## Schroter (Aug 30, 2021)

tingo said:


> Schroter you need to put that path command in the startup script for your shell. For [t]csh, this is normally $HOME/.cshrc.
> Example from one of my machines:
> 
> ```
> ...


Hi, thanks for the reply. 
I did the following;
I added `/usr/home/schroter/julia-1.6.2/bin` to `~/.cshrc` and it looks like below

```
set path = (/sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin $HOME/bin /usr/home/schroter/julia-1.6.2/bin)
```
Afterwards I restarted the computer and typed `julia` on home prompt and it said command not found.
Please let me know.
Thanks & Best Regards
Schroter


----------



## eternal_noob (Aug 30, 2021)

Schroter said:


> code typed on terminal (tcsh) echo $path shows the path variable added.


Are you sure that you're using tcsh?
What's the output of `echo $SHELL`?

If it's /bin/sh, you're supposed to add pathes in .shrc

If you want to change the shell of your normal user, see https://docs.freebsd.org/en/books/handbook/basics/#changing-shells


----------



## Schroter (Aug 30, 2021)

eternal_noob said:


> That's because your normal user doesn't use cshrc as a shell by default.
> 
> What's the output of `echo $SHELL`?
> If it's /bin/sh, you're supposed to add pathes in .shrc
> ...


Hi thanks,

In normal user mode;
`echo $SHELL` gives me:
`/bin/tcsh`. 

In root user mode;
`echo $SHELL` gives me:
`/bin/csh`. 

I deliberately changed shell to `tcsh` during FreeBSD 13 installation. Since, I was told it is better than other shells.
Thanks & Best Regards
Schroter


----------



## Schroter (Sep 1, 2021)

tingo said:


> Schroter you need to put that path command in the startup script for your shell. For [t]csh, this is normally $HOME/.cshrc.
> Example from one of my machines:
> 
> ```
> ...


Hi Thanks for the advice. I while I had added it to the ~/.cshrc file  I had not removed the # symbol at the beginning of the statement that I why it did not work. Now it works fine. Sorry for the confusion.
Thanks & Best Regards
Schroter


----------

