1

I am using Debian Stretch with systemd version 231-9.

I need to run some shell scripts and non-daemon programs in background even when I logout.

I used to make simple init.d scripts with LSB header and then case with start option that simply executes "my_script.sh >> /var/log/my_script.log 2>&1 &" to run my_script.sh in background but now Systemd kills them once I close the terminal no matter what I try: &, nohup, setsid, disown.

Can you help me to give a recipe for a proper init.d script that makes any script or non-daemon program run as daemon?

Thank you.

migrated from serverfault.com Nov 3 '16 at 18:42

This question came from our site for system and network administrators.

  • Did Debian change that "linger" thing yet? There was a pretty big brouhaha when Debian changed systemd's KillUserProcesses default to kill everything, but I thought they caved in and changed it back. – DerfK Nov 3 '16 at 19:52
  • @DerfK Nope, from man logind.conf: KillUserProcesses= ... Defaults to "yes". – spatar Nov 3 '16 at 19:57
  • Do you want these "things" start automatically during bootup (when exactly?) and run irrespective of any user sessions, or do you want to start them manually and keep them running after you log out? Do these "things" require any interaction when they start or at any later time? – Ferenc Wágner Nov 4 '16 at 7:23
  • @FerencWágner I want it to start at bootup (when entering certain runlevel), and also be able to stop/start manually using service command. – spatar Nov 10 '16 at 0:25
2

If your things are owned by the root user, then /etc/rc.local:

screen -dmS ThingOne /path/to/thing-one
screen -dmS ThingTwo /path/to/thing-two

Or, alternatively, for any user (including root), in the cron table:

@reboot screen -dmS ThingOne /path/to/thing-one
# et cetera
  • I need a proper init.d script, not a workaround with rc.local or cron. – spatar Nov 3 '16 at 19:09
0

This is how you can set a script as daemon :

#! /bin/sh

### BEGIN INIT INFO
# Provides:          foobar
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: foobar
# Description:       more foo for your bars
### END INIT INFO

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting foobar "

    # example 1 - system service
    # /usr/bin/foobar --config /etc/foo.conf start

    # example 2 - run script as user
    # su --login mkaz --command "/home/mkaz/bin/my-script --cmd-args"

    ;;
  stop)
    echo "Stopping foobar"

    # example 1
    # /usr/bin/foobar --config /etc/foo.conf stop

    ;;
  *)
    echo "Usage: /etc/init.d/foobar {start|stop}"
    exit 1
    ;;
esac

exit 0

Then move your script to init.d folder & set it as executable

sudo mv foobar /etc/init.d/       # move to init.d
sudo chmod 755 /etc/init.d/foobar # make executable

If you want to launch your script at startup :

update-rc.d foobar defaults 

If you want to remove your script from startup :

update-rc.d -f foobar remove

If you want to start your script manually :

service foobar start

Source : https://debian-administration.org/article/28/Making_scripts_run_at_boot_time_with_Debian

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.