2

I have a script that I want to run at startup and on shutdown.

I have placed the file in /etc/init.d and named it testscript.sh

Then I changed permissions sudo chmod 755 testscript.sh

Then I made link ln -s /etc/init.d/testscript.sh /etc/rc2.d/S99testscript.sh

So far so good, now the script runs at startup. I tried doing the same but making the links in rc0.d and rc6.d but that didn't work, the script still won't run at reboot/shutdown.

What am I doing wrong?

I'm running Ubuntu on putty client.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Ribban
  • 21
  • 1
  • 3
  • 1
    Try renaming your script to `K99testscript.sh`. `S` stands for *start* scripts, thus they are run when system starts, `K` stands for *kill* scripts, thus they are run when system is shut down. Also I would recommend to create `Systemd` unit files (if you are using new version of Ubuntu) rather than `SysV` scripts. – NarūnasK Mar 10 '17 at 21:45
  • That didn't work. I've also noted that the files in rc0.d and rc6.d are shown i red while the linked file in rc2.d are shown is sky blue. Does that have something to do with it? As I understand, the linked files should be shown i sky blue and archived files in red? – Ribban Mar 10 '17 at 22:39
  • A link showing as red (depending on what kind of red) is generally a broken link (i. e. a link that is not in fact pointing to a file or directory that actually exists). You can demonstrate this for yourself with `ln -s /nosuchpath ./broken-link; ls -l ./broken-link; rm ./broken-link`. – DopeGhoti Mar 10 '17 at 23:17
  • 2
    What you are doing wrong is not telling people in the question what version of Ubuntu this is. Ubuntu has not used the `rc` system that you are fiddling with since 2006. For over a decade [it has only supported what you are playing with using two distinct sets of backwards-compatibility shims.](http://askubuntu.com/a/632969/43344) If you are new to this stuff, and using an Ubuntu version from the past decade or so, then do not begin with `rc` scripts at all. – JdeBP Mar 11 '17 at 09:23
  • I'm running Ubuntu 16.04.2. I have tried putting symlinks in all rc*.d dircetorys but still no startup or shutdown script. I have followed the instructions written by NarunasK. I have do it this way, it's part of a lab assignment. – Ribban Mar 11 '17 at 21:05
  • If it's a lab assignment then it needs to be updated. – roaima Mar 13 '17 at 14:43

1 Answers1

2

Put your script into the /etc/init.d directory.

Set up executable bit: sudo chmod +x /etc/init.d/your_script.sh

Check what's your runlevel:

# runlevel
N 5

Create Start symlink in the appropriate runlevel dir. (5):

sudo ln -s /etc/init.d/your_script.sh /etc/rc5.d/Syour_script.sh

Create Kill symlink in the shutdown runlevel dir. (0):

sudo ln -s /etc/init.d/your_script.sh /etc/rc0.d/Kyour_script.sh

To do it properly read here.

NarūnasK
  • 2,276
  • 4
  • 25
  • 35
  • If I remember correctly, the program `update-rc.d` was used for ubuntu to create those symlinks for SysVinit. – Michael D. Mar 11 '17 at 01:02