12

the title pretty much sums it up.

how do I add a startup.sh and shutdown.sh of my server app to the startup and shutdown sequence of the Debian OS?

0
16

On Debian, service & application boot and shutdown scripts should be placed into /etc/init.d/.

Debian provides an example script /etc/init.d/skeleton that you can modify to your taste for your particular application.

Once it's in place, call:

# update-rc.d YOURAPP defaults

To have Debian add the /etc/rc?.d/ symlinks for you.

If you prefer a menu or graphical interface, take a look at the sysv-rc-conf or ksysv package.

4

You can call your script from /etc/rc.local, prior to the line that says exit 0

As for shutting down your app, I would suggest putting a script in /etc/rc0.d. You need to name it with an uppercase K, and then a 2 digit number which specifies the order these scripts are run, and then a name. It will be called with a parameter "stop", but you can ignore this for a simple stop script.

2
  • Why would you stuff around with rc.local when you've already got to fiddle with /etc/rcX.d anyway? – womble May 22 '09 at 1:30
  • rc.local is the proper place for simple startup stuff - don't need to deal with start/stop parameters, etc. But it doesn't have the ability to shut-down a process. The rc0.d stuff is a bit of a hack. – Brent May 22 '09 at 4:44
3

For startup have a look at this.

For shutdown, create a link / copy your script into one of the /etc/rcX.d folders where X is the runlevel that you want it to execute at. 0 is the shutdown runlevel for Debian I believe.

1
  • This is the shutdown info I needed. Thanks for this, @squillman. – harperville May 27 '15 at 13:46
3

Since Debian Jessie, the old init program has been replace by systemd!

Check it out yourself. Run: ls -l /sbin/init and see what it's pointing to. These days it points to systemd (/sbin/init -> /lib/systemd/systemd), a newer and better initialization program.


SO THE CHOSEN AND OTHER ANSWERS HERE ARE NOW OUTDATED!!!


*While you can still install scripts the old way with System-V tools, it's not a good idea generally speaking.

man systemd.service says:*

If a service is requested under a certain name but no unit configuration file is found, systemd looks for a SysV init script by the same name ... and dynamically creates a service unit from that script. This is useful for compatibility with SysV. Note that this compatibility is quite comprehensive but not 100%.


For newer Debian systems (i.e. Jessie, Stretch, Buster, etc...)

It's easier than you think. (-:

Here's the new and preferred method to install boot-up or shutdown programs.

With systemd you first want to create a unit file. A unit file is mostly declarations, not code.

Then you'll use the systemctl command to enable or start that unit.

systemd does much of the work for you, making it easy, for example, to have a critical program automatically re-start if it crashes or is otherwise killed. Also it shuts down your program where and when it should by default with no extra work on your part.


Start to learn about systemd here:

From Debian and elsewhere and etc and etc.


Man pages to start with:

man systemd.unit -- About unit files in general
man systemd.service -- About service unit files, e.g. daemons, and single run programs.

man systemctl -- Command line user interface
man journalctl -- View log of what systemd has done

man systemd -- About the init program itself


There are also a variety of other types of unit files, e.g.

man systemd.target - for groups and common synced targets.

Once you've digested the basics above, then dig in with man -k systemd to find other related man pages.



Whatever you do, on Debian do not use any of these:

  • update-rc.d --install and remove System-V style init script links
  • sysv-rc-conf -- Run-level configuration for SysV like init script links
  • runlevel -- Print previous and current SysV runlevel
  • BUM -- Boot Up Manager - a graphical runlevel editor
  • systemadm -- Graphical frontend for the systemd system and service manager
    (BTW, the author told me by email that it's too broken now.)


AN EXAMPLE:

This unit file starts up the NoIP.com daemon when I boot-up, and shuts it down when I shutdown.

This daemon, from time to time, sends my current IP address to update my DDNS (Dynamic DNS (Domain Name Server)) provider's data base, thus keeping my domain name pointing at my computer wherever it travels to.

This unit files is located on my system in this settings file: /etc/systemd/system/noip2.service

Here's what's in the unit file:

#   Comments can only go at the beginning of the line!

[Unit]
Description=Start the NoIP IP update daemon.  This runs every 30 minutes and reports our current IP to NoIP.com to update Love2d.ddns.net.
Documentation=https://no-ip.com/
Documentation=file:///nobak/Installers/NoIP/noip-2.1.9-1/README.FIRST


[Service]
# 'forking' because process returns after starting daemon (traditional unix daemon).
Type=forking

# This program runs and returns, leaving the running daemon
ExecStart=/usr/local/bin/noip2

# Be in no hurry to start this.  Max nice is +19.
Nice=15

# If it dies for any reason, then restart it
Restart=always


[Install]
# Installs a hook to use this unit file when the system boots or shuts down
WantedBy=multi-user.target

Manually run a unit file, (e.g. for testing):

  • start with $ sudo systemctl start noip2.
  • restart with $ sudo systemctl restart noip2.
  • stop with $ sudo systemctl stop noip2.

Configure system to automatically run a unit file when starting up or shutting down:

  • begin using with $ sudo systemctl enable noip2.
  • end using with $ sudo systemctl disable noip2.

View log

  • $ sudo journalctl -u noip2
2

The easiest is to modify existing scripts, works in my system, used for sounds :)

  • startup, modify /etc/init.d/rc.local, add your sh at the end of the file, before : (it will execute it right before login "screen")
  • shutdown, modify /etc/init.d/halt, add your sh at the beginning of the file, just after initial comments (it will be executed before actual shutdown command, that is called by this script file
  • reboot, same as shutdown, but in /etc/init.d/reboot

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.