27

I have a Debian server and I just need to run a script at startup.

I read that: https://www.debian-administration.org/article/28/Making_scripts_run_at_boot_time_with_Debian

I got now: insserv: warning: script ' missing LSB tags and overrides

so it looks like I have to add now:

### BEGIN INIT INFO
# Provides:          scriptname
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

It looks now crazy: like 30 lines of script just to run a program at startup.

Is there a tool that allows to do that a simply way ?

Regards

wurtel
  • 3,806
  • 12
  • 15
yarek
  • 797
  • 4
  • 12
  • 21
  • What kind of a script it is? Have you tried to run it from /etc/rc.local? You can put the path to the script in rc.local file and it will be executed at boot time. – Diamond Nov 11 '15 at 17:33
  • a simple script that starts a node process, starts a php etc... – yarek Nov 11 '15 at 17:34
  • I count 9 lines, not 30. And they're comments, not shell script (and not SSH script). – wurtel Nov 12 '15 at 13:27
  • 1
    They are LSB headers (see https://wiki.debian.org/LSBInitScripts) and an LSB init script boilerplate is north of 70 lines (see things like https://github.com/fhd/init-script-template/blob/master/template). If you want make a single command run by hand to be handled for you at boot by the computer through some file in /etc, this seems like quite an elaborate ceremony! – kristopolous Jun 10 '16 at 10:47

3 Answers3

36

Consider using /etc/rc.local (executed as root) or crontab (executed as a user of your choice).

Two examples:

  • /etc/rc.local

    #!/bin/sh -e
    #(Multiple lines of comments removed.)
    /usr/local/bin/your-script.sh
    exit 0
    
  • crontab (edited via, for example, crontab -e)

    #(Multiple lines of comments removed.)
    @reboot /usr/local/bin/your-script.sh
    

If your script needs to run continuously in the background, I would advise against using rc.local or crontab, and instead write a proper (or multiple) init.d script(s). This way you / your system is able to cleanly restart/reload/start/stop etc. the daemons.

The LSB tags provide some value: "By documenting the run-time dependencies for init.d scripts, it becomes possible to verify the current boot order, order the boot using these dependencies, and run boot scripts in parallel to speed up the boot process." For more details, head over to the Debian wiki.

By the way, the missing headers: It's a warning, so actually, it's up to you, how and what to do with this.

TZubiri
  • 190
  • 9
gxx
  • 5,483
  • 2
  • 21
  • 42
  • I like the crontab version.. but I don't think @reboot works on debian ! – yarek Nov 11 '15 at 18:01
  • @yarek: It works, using this myself (for scripts, which are just scripts). – gxx Nov 11 '15 at 18:07
  • 2
    it works, but i think it is only for root user, so you have to do `@reboot root /usr/local/bin/your-script.sh` – Froggiz Nov 11 '15 at 18:15
  • btw i was talking about debian ! – Froggiz Nov 11 '15 at 18:30
  • I am still quite confused indeed: Does cron enable to run anyscript when server restarts, not opening sessions as root. – yarek Nov 11 '15 at 20:30
  • @yarek @Froggiz: I've just tested this, using `cron 3.0pl1-124` (this is out of Debian wheezy/main): Works as expected. – gxx Nov 11 '15 at 21:34
  • @Froggiz Just came across this again. `@reboot` works for all users, not just `root`. – gxx May 11 '16 at 00:12
  • In my experience on Debian Jessie, @reboot ONLY works on an actual system REBOOT ie shutdown -r now; It does not work from a cold start or system recovering after a power outage. – John Mar 06 '19 at 23:05
  • @John Works for me, IIRC, no difference between warm or cold reboot. But don't have a jessie system anymore at hand. – gxx Mar 07 '19 at 09:38
0

supervisord is also an option. You will write again some lines in order to start your node.js and PHP stuff, but they will be fewer.

adamo
  • 6,867
  • 3
  • 29
  • 58
  • Except you then need cron or something else to start supervisord in the first place – InterLinked Jul 17 '20 at 12:57
  • The Debian installer does this for you. I am under the impression that the OP do not want to write the 30+ lines themselves. Not that 30+ is an overkill by itself if done by somebody else. – adamo Jul 21 '20 at 09:26
  • Interesting, I'm using Debian 9 and I had to manually setup supervisord to start a startup. However, I didn't have any issues once I'd done that. Comes back online now after a reboot – InterLinked Jul 21 '20 at 14:12
  • I just tried with a vagrant debian/stretch64 box: `sudo apt-get install supervisor` installs and starts supervisord. Maybe you installed it via pip? – adamo Jul 22 '20 at 13:00
0

You can use monitd for this, just write some monit definition and it will start daemons for you. But why you don't use proper way with LSB scripts? It's not so hard to write it (you're writting it just once) and it is the cleanest and most valuable way how to achieve this.

Ondra Sniper Flidr
  • 2,623
  • 11
  • 18