READYNAS 6.x Start Your Own Service – May Void Warranty
The Views Expressed Below Do not in any way reflect Internal Doctorine or Official Statements of Netgear Inc. These are just my notes – Use at your own Risk.
Enabling SSH may void your warranty. So dont blame me.
What this does:
IF you need to start a program or script or a service on boot, like a script of some sort. This will be for you.
Whats new in Readynas 6:
Systemd is used instead of sysvinit, so instead of using the /etc/init.d directory for start scripts. you will use the systemd service files (Which point to the scripts you want to launch). Personally I think its much easier.
What about Readynas 4 and 5:
They used sysvinit, and scripts started from /etc/init.d, but there was a script there already that would scrape the /var/spool/frontview/boot folder for any script to boot upon boot. Here is a full article on that topic: http://www.infotinks.com/netgear-readynas-4-x-5-x-start-service-may-void-warranty/
Again do that at your own risk (as it may void warranty and I wont be responsible for it)
SUMMARY
########
(STEP1)
Make a service file like this (only edit the Description and the ExecStart):
cat /lib/systemd/system/custom1.service [Unit] Description=CUSTOM SCRIPT 1 [Service] ExecStart=/root/custom1.sh [Install] WantedBy=multi-user.target
(STEP2)
Enable it like so (so it starts on boot) :
systemctl enable custom1
Disable it like so (so it wont start on boot):
systemctl disable custom1
(STEP3)
Make the script/program that ExecStart points at (make sure its executable by root)
cat /root/custom1.sh #!/bin/bash echo "whatever"
(STEP4)
Reboot your system thru the GUI or thru the CLI to test your service file
reboot
or:
shutdown -r now
DETAILED VERSION
###################
Make a service file that looks like this:
(STEP1)
Make a service file in /lib/systemd/system
[Unit] Description=CUSTOM SCRIPT 1 [Service] ExecStart=/root/custom1.sh [Install] WantedBy=multi-user.target[Unit] Description, just tell systemd what the script/service does. (its like the title of the service, the name of the app etc, as you can see it can have spaces) [Service] ExecStart, this points at the executable script that should be started [Install] WantedBy, this tells systemd where to put the service (at what runlevel to launch it). We always put “multi-user.target” here (thats the equivalent of the default runlevel). Note without this to stop and start the service upon boot you would need to run ln -s commands.
Can make it with this copy pastable Here-Document (a here-document is a cat command in bash that makes a file using an EOF word like EOF that tells the cat command where the text output ends – and thus to save it to the file mentioned in standard out which in this case will be a file in /lib/systemd/system)
cat > /lib/systemd/system/custom1.service << EOF [Unit] Description=CUSTOM SCRIPT 1 [Service] ExecStart=/root/custom1.sh [Install] WantedBy=multi-user.target EOF
(STEP2)
Make the script/ensure the program that is started by ExecStart actually exists and has +x (Executable permissions for root)
touch /root/custom1.sh chmod +x /root/custom1.sh vi /root/custom1.sh
Example contents:
#!/bin/bash logger "CUSTOM SCRIPT 1 started" echo "CUSTOM SCRIPT 1 starter" > /tmp/custom1.out
(STEP3)
Tell the device to launch that file upon start up.
NOTE: upon boot systemd scrapes the /etc/systemd/system folder for service files (via the symlinks that are there) to see what services to start (And at what order). We just specified default order (so I assume our script starts towards the end of the systemd service launchings)
Enable the service (make it start on boot):
systemctl enable custom1.service systemctl enable custom1
Disable the service (make it not start on boot):
systemctl disable custom1.service systemctl disable custom1
NOTE: the above command shows you what ln -s or rm commands it ran to enable/disable the script from launching on boot – these are the same commands that you would need to put in
NOTE: “systemctl start <service>” and “systemctl stop <service>” can be programmed in using more complex service files. However our script just needs to start upon boot and thats it.
If you didnt have the [Install] Section (and only had the [Unit] and the [Service] section) then you can use these symlinks to start the service on boot (or to not start the service on boot)
To start service upon boot (just make a symlink that points /etc/systemd/system/multi-user.target.wants/custom1.service to the original /lib/systemd/system/custom1.service – notice the syntax “ln -s <original> <symlink folder or symlink new filename>” – we used symlink folder, so the new symlink will have the same name so the new symlink will be /etc/systemd/system/multi-user.target.wants/custom1.service):
ln -s /lib/systemd/system/custom1.service /etc/systemd/system/multi-user.target.wants/
or:
ln -s '/lib/systemd/system/custom1.service' '/etc/systemd/system/multi-user.target.wants/custom1.service'
To not start service upon boot (just kill the service):
rm /etc/systemd/system/multi-user.target.wants/custom1.service
or:
rm '/etc/systemd/system/multi-user.target.wants/custom1.service'
NOTE: Back in sysvinit days we would tell a script to launch from the runlevel folder (/etc/rc#.d/ where # is the runlevel), the files there were symlinks to the starter/stopper/restart scripts that sat in init.d (the symlink would be prefixed with S for start or K for kill or stop – there was a switch case, an if type of statement, that looked for the start,stop,restart arguments).
IN READYNAS 4 and 5 (which used sysvinit): However you didnt have to mess with init.d scripts, as there was a folder that was looked into upon boot (any script there would launch upon boot). So in the READYNAS4(and Readynas 5) units an exectuable script in here /var/spool/frontview/boot/ would start upon boot up. For more info: http://www.infotinks.com/netgear-readynas-4-x-5-x-start-service-may-void-warranty/ Again do that at your own risk (as it may void warranty and I wont be responsible for it)
How it looks like when your service is enabled (will launch on boot – it will say its loaded)
# systemctl status custom1 custom1.service - CUSTOM SCRIPT 1 Loaded: loaded (/lib/systemd/system/custom1.service; enabled) Active: inactive (dead) CGroup: name=systemd:/system/custom1.service
How it looks like when your service is disabled
# systemctl status custom1.service custom1.service - CUSTOM SCRIPT 1 Loaded: loaded (/lib/systemd/system/custom1.service; disabled) Active: inactive (dead) CGroup: name=systemd:/system/custom1.service
NOTE: you can refer to your service as custom1 or custom1.service
(STEP4)
Reboot your system thru the GUI or thru the CLI to test your service file
reboot
or:
shutdown -r now
EASY SCRIPTS TO COPY PASTE TO MAKE A SERVICE FILE READY TO GO
##################################################
2 Different copy paste scripts that do the same thing (one with a here-doc way to make a service file – another with an echo way to make a service file)
Just edit the first 3 variables
SERVICENAME
SERVICEDESCRIPTION
SCRIPTLOCATION
And copy paste the following scripts to
1) make the service file
2) make the script, and make it executable
3) tell you how to enable and disable the service file
MAKE EVERYTHING WITH 1 STEP – USING HERE-DOCUMENT
SERVICENAME="custom1" SERVICEDESCRIPTION="CUSTOM SCRIPT 1" SCRIPTLOCATION="/root/custom1.sh" cat > /lib/systemd/system/${SERVICENAME}.service << EOF [Unit] Description=${SERVICEDESCRIPTION} [Service] ExecStart=${SCRIPTLOCATION} [Install] WantedBy=multi-user.target EOF echo "Made the service file: /lib/systemd/system/${SERVICENAME}.service" touch ${SCRIPTLOCATION} chmod +x ${SCRIPTLOCATION} echo "Made the executable script file (should be empty now): ${SCRIPTLOCATION}" echo "TODO 4 YOU: Edit the above file to make it do what you want" echo "To enable it on boot:" echo "systemctl enable ${SERVICENAME}" echo "To disable it on boot:" echo "systemctl disable ${SERVICENAME}"
MAKE EVERYTHING WITH 1 STEP – USING ECHO
SERVICENAME="custom1" SERVICEDESCRIPTION="CUSTOM SCRIPT 1" SCRIPTLOCATION="/root/custom1.sh" echo -e "[Unit]\nDescription=${SERVICEDESCRIPTION}\n\n[Service]\nExecStart=${SCRIPTLOCATION}\n\n[Install]\nWantedBy=multi-user.target" > /lib/systemd/system/${SERVICENAME}.service echo "Made the service file: /lib/systemd/system/${SERVICENAME}.service" touch ${SCRIPTLOCATION} chmod +x ${SCRIPTLOCATION} echo "Made the executable script file (should be empty now): ${SCRIPTLOCATION}" echo "TODO 4 YOU: Edit the above file to make it do what you want" echo "To enable it on boot:" echo "systemctl enable ${SERVICENAME}" echo "To disable it on boot:" echo "systemctl disable ${SERVICENAME}"