[HOW TO] Run your own script on system startup

Share your awesome tips and tricks here.

Moderator: Lillian.W@AST

rezza
Posts: 22
youtube meble na wymiar Warszawa
Joined: Wed Apr 05, 2017 8:48 pm

Re: [HOW TO] Run your own script on system startup

Post by rezza »

hello.

I manually execute my sh script correctly. but after copying it inside usr/local/etc/unit.d and rename it with SXXghhhh.sh, it won t run at NAS start-up...
Can someone help me ?

thanks

rezza
User avatar
father.mande
Posts: 1810
Joined: Sat Sep 12, 2015 2:55 am
Location: La Rochelle (France)

Re: [HOW TO] Run your own script on system startup

Post by father.mande »

Hi,

For all problem with init scripts remember :

init script are executed by init so not with all the capabilities of a shell and without a full shell environment
... so be sure to use Full Path-name, to set the full environment (all variables, alias, etc.), to be sure that program you use are present ... ex. if you target an APKG (Entware bash for ex.) ... ALL APKG are started AFTER the end of the init script series ... so not ready when init script start ... and a little more (so think to set debug (like -x/-v/-f by set instruction in shell) and log all stderr stdout )

It's same for other O.S. managed task (ex. cron @reboot for ex., at command, etc. )

summary :
do as if NO environment exist when you start your script ...
always verify that target (for ex. Python apkg or what-else) are already enable and active (so best is to generate a loop on task of file or link or ... but as usual not infinite ... use timeout delay and write log message in case )
remember that su (even sudo but a little more difficult to prepare) are provide in A.D.M.

Philippe.
AS6602T / AS5202T /AS5002T / AS1002T / FS6706T
rezza
Posts: 22
Joined: Wed Apr 05, 2017 8:48 pm

Re: [HOW TO] Run your own script on system startup

Post by rezza »

HELLO.

My script (inside init.d) was correctly launched at start-up but prevent the file /usr/etc/emboard.conf to do its normal job. The consequence was that I couldn't apply any hardware energy settings with ADM as well as with SSH.
After removing the script from usr/local//etc/init.d, the problem disappeared...

A possible clue : Inside My script, I used a TCPDUMP function which has been installed with Entware OPKG...

Any idea ?

thanks

regards

RezzA
User avatar
father.mande
Posts: 1810
Joined: Sat Sep 12, 2015 2:55 am
Location: La Rochelle (France)

Re: [HOW TO] Run your own script on system startup

Post by father.mande »

Hi,
rezza wrote:HELLO.
My script (inside init.d) was correctly launched at start-up but prevent the file /usr/etc/emboard.conf to do its normal job. The consequence was that I couldn't apply any hardware energy settings with ADM as well as with SSH.
After removing the script from usr/local//etc/init.d, the problem disappeared...
A possible clue : Inside My script, I used a TCPDUMP function which has been installed with Entware OPKG...
Any idea ?
thanks
regards
RezzA
First be sure to use full path-name or to add /opt/bin:/opt/sbin in PATH and export it
Second add a loop (not infinite ... but for a correct delay) to test if Entware is up or not yet
... ex. use apkg to test the status or test if path to /opt/bin/tcpdump or /opt/bin/opkg exist ... add a small delay (to be sure all is set up) ... then run the command ...

Philippe.
AS6602T / AS5202T /AS5002T / AS1002T / FS6706T
ndl101
Posts: 57
Joined: Sun Jul 11, 2021 4:32 pm

Re: [HOW TO] Run your own script on system startup

Post by ndl101 »

As a part of doing things with bind mounts, I made a generic init script template I thought I would share. I have attempted to document it to the best of my abilities and I hope it is sufficient.
  1. fill out the template functions in the script to reflect your use case.
  2. scp the script to your NAS

    Code: Select all

    $ scp X00script-name <nas-admin-username>@<your-nas-ip>:/usr/local/etc/init.d/X00script-name
    
  3. ssh to your NAS and make the make it executable

    Code: Select all

    $ sudo chmod +x /usr/local/etc/init.d/X00script-name
    
  4. enable the bind mount

    Code: Select all

    $ sudo /usr/local/etc/init.d/X00script-name start
    
  5. ensure that it worked as expected
  6. profit
The template

Code: Select all

#!/bin/sh
#
# Author: ndl101/Nicolas Damgaard Larsen
# DESCRIPTION GOES HERE
#

SERVICE_NAME="SERVICE_NAME_GOES_HERE"
LOG_FILE="/var/log/custom-services.log"

updateLog() {
  # Updates the "log" with the given state. See /var/log/boot.log for examples.
  # NOTE: this function will purposely "fail" silently if "/usr/bin/confutil" is
  # unavailable or if it is called with insufficient arguments.
  # Call the function: updateLog action KEY VALUE
  # Example:
  #   updateLog "set" "progress" "done"
  confUtil="/usr/bin/confutil"
  ( [ -f $confUtil ] && [ "$#" -eq 3 ] ) && {
    echo "updating log"
    /usr/bin/confutil "-$1" $LOG_FILE $SERVICE_NAME "$2" "$3"
  }

}

die() {
  # Use this function to exit the script with the default error exit code.
  # Optionally provide an error message to be "echo'ed".
  #
  # Examples:
  #   die "Something went wrong, exiting..."
  #   die
  [ ! -z "$1" ] && echo "$1"
  updateLog "set" "progress" "failed"
  exit 1
}

# Should you need to add custom functions to the init script, you can add
# them like this.
#aCustomFunction() {
#  #This function is called below under the example "myCustomCMD" pattern in the
#  # case (switch) satement.
#  echo "doing custom things"
#}

doStart() {
  # Implement stating the service here.
  # This function is being called under the "start" pattern in the case (switch)
  # statement below. Should you have no use for this template function either
  # leave it as is for consistency or alternatively delete this definition and
  # remove the call to it below.
  return #  Delete this line when implementing this function.
}

doStop() {
  # Implement stopping the service here.
  # This function is being called under the "start" pattern in the case (switch)
  # statement below. Should you have no use for this template function either
  # leave it as is for consistency or alternatively delete this definition and
  # remove the call to it below.
  return #  Delete this line when implementing this function.
}

doReload() {
  # Implement reloading the service here.
  # This is called under the "reload" pattern in the case (switch)
  # statement below. Should you have no use for this template function either
  # leave it as is for consistency or alternatively delete this definition and
  # remove the call to it below.
  # NOTE: reloading is different from restarting. Restarting should actually
  # stop and start the service, whereas reloading should neither stops nor start
  # the service but reload e.g. a configuration or other actions while the
  # service is still running.
  return #  Delete this line when implementing this function.
}

case "$1" in

# Should you need to call your service's init script with other parameters than
# start|stop|restart|reload, you can add them like this.
#  myCustomCMD)
#    doing stuff here
#    doing more stuff here
#    aCustomFunction
#    ;;

  start)
    echo "Starting $SERVICE_NAME"
    doStart
    updateLog "set" "progress" "started"
    ;;

  stop)
    echo "Stopping $SERVICE_NAME"
    doStop
    updateLog "set" "progress" "stopped"
    ;;

  restart)
    "$0" stop
    "$0" start
    ;;

  reload)
    echo "reloading $SERVICE_NAME"
    updateLog "set" "reloading" "in-progress"
    doReload
    updateLog "del" "reloading"
    ;;
  
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
  
esac

exit $?


I made it long as I lacked the time to make it short.

---
Help to self-help:
How to ask (good) questions in a forum
---
General information
Location: Denmark
OS: Ubuntu 20.04
NAS: Lockerstor 4 (AS6604T)
SpeedSnnop
Posts: 5
Joined: Fri Nov 05, 2021 7:01 am

Re: [HOW TO] Run your own script on system startup

Post by SpeedSnnop »

Good afternoon, my name is Saul, I currently own an Asustor Lockerstor 6604T.
I have rclone configured with 2 drive units mounted, but when I restart nas or it turns off these disappear, having to execute the rclone mount command again.
I have tried to create a script as you comment here in init.d, but the address that comes out to me in /volume1/.@plugins/etc/init.d/, there I create a file S86mount.sh and inside I put the following commands:
#! / bin / sh
### script mount drive Adrisamina in plex

rclone mount PLEX: / volume1 / PELIS --allow-other &
rclone mount SERIES: / volume1 / SERIES --allow-other &

But when restarting nothing comes out.
I have also tried creating a cotrab with @reboot sleep 60 and it doesn't work.
Could you give me a hand please.
Thank you very much and sorry for my English, I use google translator.

A greeting from Spain.
User avatar
father.mande
Posts: 1810
Joined: Sat Sep 12, 2015 2:55 am
Location: La Rochelle (France)

Re: [HOW TO] Run your own script on system startup

Post by father.mande »

Hi,

I think @reboot can't work in A.D.M. ... because crontab is applet of busybox ... so incomplete.
... an alternative is to used cron from Entware APKG (the configuration PATH is different so you must be sure that you call the good crontab (for ex.))
... as I remember (98%) the two (ADM one and Entware one) can run simultaneously ... so nothing change for A.D.M. application or system cron list. (NB I never test @reboot ... so ... )

For the second point (start_up script), this script is executed with the init shell, so with a reduced binary PATH variable.
SO always used full path name ex.
if you use rclone from Entware APKG ... the full path is : /opt/bin/rclone (adapt to who provide rclone for you)
BUT this can be not enough if Entware (or your APKG provider) start AFTER the shell start_up ... in this case a wait can be performed up to binary is available and sometimes a Nohup is need to don't stop the init process (I have to check if start_up script are forked (so no problems) or directly executed.
An alternative is to add (in the Entware case) the start_up script in Entware star-up (/opt/etc/init.d/SNNmyrclone.sh (NN is number order to start in Entware) or what ever) so you are sure that Entware is up when you start your script. (Entware used it's own start_up script for launching server or user script) ... Entware program are native but used their own libraries to be independent of A.D.M. environment.
also a good idea is to add logging for the script ... so you can for ex. echo date to an output file, then result of each command, then date for end ...

Philippe.
AS6602T / AS5202T /AS5002T / AS1002T / FS6706T
User avatar
orion
Posts: 3482
Joined: Wed May 29, 2013 11:09 am

Re: [HOW TO] Run your own script on system startup

Post by orion »

Most-likely, there are something wrong in your script. You may print certain debug messages in your scripts (for example, output messages to /var/log/test.log).
SpeedSnnop
Posts: 5
Joined: Fri Nov 05, 2021 7:01 am

Re: [HOW TO] Run your own script on system startup

Post by SpeedSnnop »

Hello, good afternoon, thanks for answering.
How should I modify the script to get the said log.
The case is also that if I run the sh directly from the terminal if it mounts the two drive units.

Thank you very much and greetings.
ndl101
Posts: 57
Joined: Sun Jul 11, 2021 4:32 pm

Re: [HOW TO] Run your own script on system startup

Post by ndl101 »

SpeedSnnop wrote:Good afternoon, my name is Saul, I currently own an Asustor Lockerstor 6604T.
I have rclone configured with 2 drive units mounted, but when I restart nas or it turns off these disappear, having to execute the rclone mount command again.
I have tried to create a script as you comment here in init.d, but the address that comes out to me in /volume1/.@plugins/etc/init.d/, there I create a file S86mount.sh and inside I put the following commands:
#! / bin / sh
### script mount drive Adrisamina in plex

rclone mount PLEX: / volume1 / PELIS --allow-other &
rclone mount SERIES: / volume1 / SERIES --allow-other &

But when restarting nothing comes out.
I have also tried creating a cotrab with @reboot sleep 60 and it doesn't work.
Could you give me a hand please.
Thank you very much and sorry for my English, I use google translator.

A greeting from Spain.
When posing code on the forum, please wrap it on "code tags" (the button labeled "code"). It makes it easier to read for us.

Did you execute a `chmod +x` on the script as instructed? Please paste the output of

Code: Select all

ls -l ls -l /volume1/.@plugins/etc/init.d/S86mount.sh
For me

Code: Select all

admin@asnas:/volume1/home/admin $ ls -l /volume1/.@plugins/etc/init.d/S21bind-mount-video-share 
-rwxr-xr-x    1 admin    administ      3157 Jul 29 13:57 /volume1/.@plugins/etc/init.d/S21bind-mount-video-share*
If the first bit "-rwxr-xr-x" contains no x's, this could be part of the cause. You can read up on Linux file permissions here
SpeedSnnop wrote: The case is also that if I run the sh directly from the terminal if it mounts the two drive units.
How are you executing it from the command line? Are you doing a "sh S86mount.sh" or just a "./S86mount.sh"?

As father.mande suggested, try calling the rclone binary with the full path. So, if rclone is located in "/opt/bin/rclone" do a

Code: Select all

/opt/bin/rclone mount PLEX: / volume1 / PELIS --allow-other &
from the script instead.

I made it long as I lacked the time to make it short.

---
Help to self-help:
How to ask (good) questions in a forum
---
General information
Location: Denmark
OS: Ubuntu 20.04
NAS: Lockerstor 4 (AS6604T)
Post Reply

Return to “Tips & Tricks”