3

I have an /etc/init.d script that starts my webserver. I made it executable and added it (update-rc.d webserver defaults). When I execute the script from the command line it works fine (./webserver).

But after rebooting my system my webserver is not running.

#!/bin/sh

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

cd ~/projects/webserver
./.build/debug/webserver &
exit 0

Is the user directory a problem (I am logged in as root)?

  • 1
    Kindly post the output ls -lht /etc/init.d/webserver – SAGAR Nair Aug 25 '16 at 14:58
  • 4
    Tilde expansion is done in reference to the current user. I don't see how it can work if the script runs before login (loading the env). Use the full path instead of ~ – Zanna Aug 25 '16 at 15:17
  • You never mentioned whether the links to it, /etc/rc.d/*/S*webserver exist. This is important because the scripts in /etc/init.d are not directly executed at boot/shutdown. The S* scripts are run at boot and the K* scripts at shutdown. – Monty Harder Aug 25 '16 at 20:43
5

The type of tilde expansion you want is performed in reference to the current user of the shell. For example, when you log in as root ~ is /root. If the script runs before you log in, then ~ cannot be expanded to the directory you want.

Use the full path to the file in your script.

For future reference, here are some useful examples of using tilde expansion in scripts

| improve this answer | |
3

Well there is a problem in your script. When you write script better use aboslute path instead of tilde(~). Thats could be the culprit that made the script unusable. If you want to change dir, give the full path like /home/scripts/projects/ instead of ~/projects .It makes a lot more difference.

To achieve your goal, there are several ways. Being a Senior Systems Engineer , I use different methodology to achieve such things which depends on the situations. Well here I list some of many ways.

1) Basic way what you tied and not succeeded .

sudo su
chmod a+x /etc/init.d/webserver
update-rc.d webserver defaults

2) Add a cron job at reboots.

sudo su
crontab -e
@reboot /home/path/to/script.sh

3) Add your commands to rc.local file before the last line exit 0.

sudo su
nano /etc/rc.local
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
cd /absolute/path/projects/webserver
./.build/debug/webserver &
| improve this answer | |
  • The spelling of the name of that character is tilde, not tilde. – Monty Harder Aug 25 '16 at 20:43
  • Its a typing mistake. Thanks for mentioning. I 've edited it. – SAGAR Nair Aug 25 '16 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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