init.d script is not executed

12,862

Solution 1

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

Solution 2

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 &
Share:
12,862

Related videos on Youtube

eclipse
Author by

eclipse

Updated on September 18, 2022

Comments

  • eclipse
    eclipse almost 2 years

    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)?

    • SAGAR Nair
      SAGAR Nair almost 8 years
      Kindly post the output ls -lht /etc/init.d/webserver
    • Zanna
      Zanna almost 8 years
      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 ~
    • Monty Harder
      Monty Harder almost 8 years
      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
    Monty Harder almost 8 years
    The spelling of the name of that character is tilde, not tilde.
  • SAGAR Nair
    SAGAR Nair almost 8 years
    Its a typing mistake. Thanks for mentioning. I 've edited it.