TheRed
TheRed

Reputation: 327

BASH script: how to use variable for $HOME in rc.local on start up

I am executing a shell script which is referenced in rc.local. The file is git-versioned and needs to change into the respective home directory of each user. Absolute paths work fine, but those are different from machine to machine. $HOME or ~/ are not defined yet, how can I change into the main user's directory in rc.local on start-up?

This is the working script:

cd /home/username/rightDIR

php -S localhost:8000

This script starts the server but fails to move into the right directory beforehand:

cd $HOME/rightDIR

php -S localhost:8000

Upvotes: 1

Views: 1087

Answers (1)

Jay
Jay

Reputation: 3950

cd /home/*/rightDIR && php -S localhost:8000 & should work if only one user has a rightDIR in their home folder (otherwise the first match wins).

The && chains the commands together. If the cd command fails, the command after won't be started.

The & at the end starts the command as shell job in the background, so that the shell can continue and run any commands that come after this line in rc.local

If startup hangs anyway, consider adding nohup left of php instead, in order to detach the command from the shell.

Upvotes: 1

Related Questions