0

In my Debian /etc/rc.local, I've put 2 scripts (one for Wifi AP, the other for VPN client-connection):

sh /bootscripts/vpn.sh &
sh /bootscripts/wifiap.sh &

#test:
date >> /home/user24/Desktop/logdate

exit 0

Both scripts are executable (rwxr-xr-x), and their shebang line is #!bin/bash.

But they don't run. Where am I wrong?

  • Why do you source the scripts in the background that is counter-intuitive to me? Have you tried just running them in the background without using source? – Anthon Aug 25 '15 at 13:03
  • I m doing it now. I thought source is needed cause I call a script in another script. – ArchiT3K Aug 25 '15 at 13:04
  • @all FYI: When you call 'source' (or its alias '.'), you insert the script in the current bash process. So you could read variables set by the script. When you call 'sh', you initiate a fork (sub-process) that runs a new session of /bin/sh, which is usually a symbolic link to bash. In this case, environment variables set by the sub-script would be dropped when the sub-script finishes. – ArchiT3K Aug 25 '15 at 13:05
  • No source is only necessary if you want to have the statements in the script to have effect in rc.local itself (ie. set variables, etc.). – Anthon Aug 25 '15 at 13:05
  • @Anthon Thanks. Allright, I have editted my post with a date test. Date works. But Vpn doesnt, nor wifiAP even with 'sh'. – ArchiT3K Aug 25 '15 at 13:06
1

Since /etc/rc.local is executed at the end of each multiuser runlevel, it's not the correct place to add start scripts. I recommend to not use /etc/rc.local in any way. It's a reclit for early *nix times. Instead of that, create a startup script in /etc/init.d/name which accepts start and stop arguments to start or stop the deamon, process or the job:

#! /bin/sh
# /etc/init.d/name
#

case "$1" in
  start)
    echo "Starting name"
    your_service --with --parameters
    ;;
  stop)
    echo "Stopping name"
    kill your_service
    ;;
  *)
    echo "Usage: /etc/init.d/name {start|stop}"
    exit 1
    ;;
esac

exit 0

Also there is a skeleton script in /etc/init.d/skeleton for this.

After you created that scripts, set the permissons:

chmod 755 /etc/init.d/name

Now, add them to the boot seqence:

update-rc.d name defaults

This will create the necessary links in the /etc/rc*.d/ directories.

  • Great, but my service are 2 loooon scripts, how do I manage this fact that way ? – ArchiT3K Aug 25 '15 at 14:01
  • @Strukt what are loooon scripts? – chaos Aug 25 '15 at 14:38
  • I mean I must call .sh ... well, I need to make 2 services, and call start/stop in my primary service intented for boot. Thank you, you lighted it all! – ArchiT3K Aug 25 '15 at 14:40
  • unix.stackexchange.com/a/480897/5132 /etc/init.d/skeleton is no more, and that has not been the template style since 2014. – JdeBP Nov 10 '18 at 4:52

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.