[How To] Set up an autorun.sh file

Discussion on setting up QNAP NAS products.
Locked
CasaP
Know my way around
Posts: 210
Joined: Sat Feb 14, 2015 3:13 am

[How To] Set up an autorun.sh file

Post by CasaP »

Patrick came up with a brilliant solution for everyone using the VPN client, using his method it should be possible for you to set up an autorun.sh file that will run on every restart/reboot. My one copies the custom settings for my VPN setup, but as you can see, it could literally contain anything.

NOTE: From Patrick: Please be aware that the "autorun.sh" creation commands you quoted only work with newer "HAL"-enabled" QNAP NAS models. Those same instructions will not work with older QNAP models, especially the Marvel/ARM based ones. For older units follow this guide: http://wiki.qnap.com/wiki/Running_Your_ ... at_Startup

1. Open a shh session
Copy the following code into your session:

Code: Select all

mount $(/sbin/hal_app --get_boot_pd port_id=0)6 /tmp/config
touch /tmp/config/autorun.sh
chmod +x /tmp/config/autorun.sh
$EDITOR /tmp/config/autorun.sh
umount /tmp/config
#done
 
2. Into VI
The Vi editor will open up, hit the i key to enter INSERT mode and enter any shell commands you need.. I am copying a custom VPN setting from my VPN folder to the /etc/init.d/ folder at start up.
VI Session.png

Code: Select all

#!/bin/sh
cp /share/CACHEDEV1_DATA/Download/VPN/vpn_openvpn_client.sh /etc/init.d/vpn_openvpn_client.sh
3. Save and DONE
Press Esc on the keyboard and type :wq What you have done here is to make sure that on startup your custom vpn_openvpn_client.sh is copied into the relevant directory.
You do not have the required permissions to view the files attached to this post.
Last edited by CasaP on Thu Aug 13, 2015 5:30 pm, edited 2 times in total.
QNAP TS-453Pro w/ 4 * WD RED 4Tb (RAID5) - FW: QTS Build Latest (stable)
QNAP TS-853A w/ 8 * WD RED 6Tb (RAID5) - FW: QTS Build Latest (stable)

OSX 11.2.+
User avatar
pwilson
Guru
Posts: 22533
Joined: Fri Mar 06, 2009 11:20 am
Location: Victoria, BC, Canada (UTC-08:00)

Re: [How To] Set up an autorun.sh file

Post by pwilson »

CasaP wrote:Patrick came up with a brilliant solution for everyone using the VPN client, using his method it should be possible for you to set up an autorun.sh file that will run on every restart/reboot. My one copies the custom settings for my VPN setup, but as you can see, it could literally contain anything.

1. Open a shh session
Copy the following code into your session:

Code: Select all

mount $(/sbin/hal_app --get_boot_pd port_id=0)6 /tmp/config
touch /tmp/config/autorun.sh
chmod +x /tmp/config/autorun.sh
$EDITOR /tmp/config/autorun.sh
umount /tmp/config
#done
 
2. Into VI
The Vi editor will open up, hit the i key to enter INSERT mode and enter any shell commands you need.. I am copying a custom VPN setting from my VPN folder to the /etc/init.d/ folder at start up.
VI Session.png

Code: Select all

#!/bin/sh
cp /share/CACHEDEV1_DATA/Download/VPN/vpn_openvpn_client.sh /etc/init.d/vpn_openvpn_client.sh
3. Save and DONE
Press Esc on the keyboard and type :wq What you have done here is to make sure that on startup your custom vpn_openvpn_client.sh is copied into the relevant directory.
If you don't like "Vi", you can install a different editor on the NAS, and change the $EDITOR variable accordingly.

I personally detest "Vi" and will do almost anything to avoid it. For my NAS here, I simply installed "Joe" on it. (Hint: "Joe" is available from both Optware and QNAPware). The popular "nano" editor is also available in both.

Simply install your favourite text editor on the NAS, and then adjust the "$EDITOR" variable accordingly. For example I use the following command here:

Code: Select all

export EDITOR='/opt/bin/joe'


Test this with:

Code: Select all

echo $EDITOR
From my NAS:

Code: Select all

echo $EDITOR
/opt/bin/joe
Also please be aware that the "autorun.sh" creation commands you quoted only work with newer "HAL"-enabled" QNAP NAS models. Those same instructions will not work with older QNAP models, especially the Marvel/ARM based ones.

Patrick M. Wilson
Victoria, BC Canada
QNAP TS-470 Pro w/ 4 * Western Digital WD30EFRX WD Reds (RAID5) - - Single 8.1TB Storage Pool FW: QTS 4.2.0 Build 20151023 - Kali Linux v1.06 (64bit)
Forums: View My Profile - Search My Posts - View My Photo - View My Location - Top Community Posters
QNAP: Turbo NAS User Manual - QNAP Wiki - QNAP Tutorials - QNAP FAQs

Please review: When you're asking a question, please include the following.
CasaP
Know my way around
Posts: 210
Joined: Sat Feb 14, 2015 3:13 am

Re: [How To] Set up an autorun.sh file

Post by CasaP »

Thanks Patrick, I have been looking for that bit of info everywhere. The funny thing is I "almost" know how to use Vi at this stage.

I will install Joe when I get a chance over the weekend.
QNAP TS-453Pro w/ 4 * WD RED 4Tb (RAID5) - FW: QTS Build Latest (stable)
QNAP TS-853A w/ 8 * WD RED 6Tb (RAID5) - FW: QTS Build Latest (stable)

OSX 11.2.+
CasaP
Know my way around
Posts: 210
Joined: Sat Feb 14, 2015 3:13 am

Re: [How To] Set up an autorun.sh file

Post by CasaP »

This is an even better version from the late great Patrick, RIP.

I had to go back to the drawing board after Entrware-ng refused to work with the previous method.

All you need to do is cut & paste into a text editor, make any changes you need, then cut and paste into a ssh session, it will then run, make the autorun.sh script for you. Simple.

Code: Select all

mount -t ext2 $(/sbin/hal_app --get_boot_pd port_id=0)6 /tmp/config
cp -f /tmp/config/autorun.sh /tmp/config/autorun.sh.OLD
rm -f /tmp/config/autorun.sh
touch /tmp/config/autorun.sh
chmod +x /tmp/config/autorun.sh
cat <<EOF >>/tmp/config/autorun.sh
#!/bin/sh
# AUTORUN.SH - "Boot strap"
# This script forces the NAS to run "/etc/config/autorun.sh" at every startup (if this file exists).
#
/etc/config/autorun.sh
touch /tmp/autorun.ran
# done
EOF
echo "unmounting /tmp/config..."
umount /tmp/config
touch /etc/config/autorun.sh
chmod +x /etc/config/autorun.sh
cat <<EOF1 >>/etc/config/autorun.sh
#!/bin/sh
#
# /etc/config/autorun.sh - These commands run at every NAS Startup
#
# Mount /opt at startup... 
/bin/ln -sf $(getcfg -f /etc/config/qpkg.conf Entware-ng install_path) /opt
#
###  Add your commands here

export PATH=/opt/bin:$PATH

cp /share/CACHEDEV1_DATA/Download/VPN/vpn_openvpn_client.sh /etc/init.d/vpn_openvpn_client.sh
cp /share/CACHEDEV1_DATA/Download/VPN/torg.ovpn /etc/openvpn/client.conf
cp /share/CACHEDEV1_DATA/Download/VPN/torgpass.txt /share/Download/VPN/openvpn_client_login.txt

#done
EOF1
echo "Edit /etc/config/autorun.sh for all further changes to the autostart procedure of the NAS."
#done
QNAP TS-453Pro w/ 4 * WD RED 4Tb (RAID5) - FW: QTS Build Latest (stable)
QNAP TS-853A w/ 8 * WD RED 6Tb (RAID5) - FW: QTS Build Latest (stable)

OSX 11.2.+
CasaP
Know my way around
Posts: 210
Joined: Sat Feb 14, 2015 3:13 am

Re: [How To] Set up an autorun.sh file

Post by CasaP »

Just a word of advice. If you are thinking of installing Entware-ng - DONT.
It seems to have broken more than it has fixed, the Autorun.sh script above being an example. I am trying to get to the bottom of it, and will update when I can.
Entware-ng has so far ruined my setup, be warned.
Ignore that all up and running now, but definitely experienced a lot of weirdness with Entware-ng
QNAP TS-453Pro w/ 4 * WD RED 4Tb (RAID5) - FW: QTS Build Latest (stable)
QNAP TS-853A w/ 8 * WD RED 6Tb (RAID5) - FW: QTS Build Latest (stable)

OSX 11.2.+
neothematrix
Starting out
Posts: 27
Joined: Wed Feb 17, 2016 9:03 pm

Re: [How To] Set up an autorun.sh file

Post by neothematrix »

Hello, sorry for bringing to life this old topic, but I thought if anyone's watching this, you should be aware that the latest version of "malware remover" from QNap (if you installed it) automatically removes any /tmp/config/autorun.sh after mounting the config partition, it does so because there's a nas malware that uses that file to restart on reboot.
So you may end up discovering that after a reboot, all your carefully created startup scripts, are just not working, this may be the reason :)
If you have the Malware Remover installed, you can check in the System Log if it removed your autorun.sh, it clearly states it, the Malware Remover doesn't really check the content of the file, so it removes it even if it is not malicious (I think this is a bit over-reacting :-) )
nickj
Starting out
Posts: 49
Joined: Wed Feb 23, 2011 9:23 am

Re: [How To] Set up an autorun.sh file

Post by nickj »

Yes neothematrix, I found out the hard way! That makes it totally useless for me - I've removed the Remover.
- Nick

TS-219P; TS-259 Pro; TS-269 Pro; TS-459U; TS-453U; TS-253B
TR-004U; TS-470 Pro 16; NMP-1000
dvoisin
Starting out
Posts: 13
Joined: Sat May 05, 2012 10:03 pm

Re: [How To] Set up an autorun.sh file

Post by dvoisin »

I am running a TS-670 Pro with firmware 4.3.3.0262 and attempting to use the new "execute an autorun script upon NAS startup" feature. I've followed the instructions in the first post however when I go to System > Hardware > General > "View autorun.sh" the edits to the autorun.sh file are missing. I've also noticed that after writing the file, if I exit vi and then edit the file again, the edits are missing.

When I execute "ls /tmp/config" I see an "autorun.sh@" but I do not see "autorun.sh". Also, when I execute "vi /tmp/config/autorun.sh", vi displays a message indicating '"tmp/config/autorun.sh" is not a file'.

What am I doing wrong? Any help is appreciated.
User avatar
schumaku
Guru
Posts: 43579
Joined: Mon Jan 21, 2008 4:41 pm
Location: Kloten (Zurich), Switzerland -- Skype: schumaku
Contact:

Re: [How To] Set up an autorun.sh file

Post by schumaku »

The official information on how to add an autorun.sh file is here -> https://wiki.qnap.com/wiki/Running_Your ... at_Startup

The TS-670 is a HAL based NAS, to the process posted must work:

[~] # mount $(/sbin/hal_app --get_boot_pd port_id=0)6 /tmp/config
[~] # mount | grep config
/dev/sdc6 on /tmp/config type ext2 (rw)
[~] # touch /tmp/config/autorun.sh
[~] # chmod +x /tmp/config/autorun.sh
[~] # vi /tmp/config/autorun.sh
[~] # umount /tmp/config

...check it again...

[~] # mount $(/sbin/hal_app --get_boot_pd port_id=0)6 /tmp/config
[~] # mount | grep config
/dev/sdc6 on /tmp/config type ext2 (rw)
[~] # cat /tmp/config/autorun.sh
#!/bin/sh
# AUTORUN.SH - "Boot strap"
# This script forces the NAS to run "/etc/config/autorun.sh" at every startup (if this file exists).
#
/etc/config/autorun.sh
touch /tmp/autorun.ran
# done
[~] # umount /tmp/config

This autorun.sh does also show up in the Web UI...
dvoisin
Starting out
Posts: 13
Joined: Sat May 05, 2012 10:03 pm

Re: [How To] Set up an autorun.sh file

Post by dvoisin »

schumaku wrote:The official information on how to add an autorun.sh file is here -> https://wiki.qnap.com/wiki/Running_Your ... at_Startup

The TS-670 is a HAL based NAS, to the process posted must work:

[~] # mount $(/sbin/hal_app --get_boot_pd port_id=0)6 /tmp/config
[~] # mount | grep config
/dev/sdc6 on /tmp/config type ext2 (rw)
[~] # touch /tmp/config/autorun.sh
[~] # chmod +x /tmp/config/autorun.sh
[~] # vi /tmp/config/autorun.sh
[~] # umount /tmp/config

...check it again...

[~] # mount $(/sbin/hal_app --get_boot_pd port_id=0)6 /tmp/config
[~] # mount | grep config
/dev/sdc6 on /tmp/config type ext2 (rw)
[~] # cat /tmp/config/autorun.sh
#!/bin/sh
# AUTORUN.SH - "Boot strap"
# This script forces the NAS to run "/etc/config/autorun.sh" at every startup (if this file exists).
#
/etc/config/autorun.sh
touch /tmp/autorun.ran
# done
[~] # umount /tmp/config

This autorun.sh does also show up in the Web UI...
I tried again and it does not work. When I go back to check autorun.sh it is empty. After I mount /tmp/config, if I run "ls /tmp/config" I see:

autorun.sh@ lost+found/ smb.conf.cksum uLinux.conf customise.conf smb.conf system.map.key

Is it normal for "autorun.sh@" to appear? When I run "vi /tmp/config/autorun.sh", vi says '"/tmp/config/autorun.sh" is not a file'. Is that normal? Should I be editing the "autorun.sh@" file instead?
User avatar
schumaku
Guru
Posts: 43579
Joined: Mon Jan 21, 2008 4:41 pm
Location: Kloten (Zurich), Switzerland -- Skype: schumaku
Contact:

Re: [How To] Set up an autorun.sh file

Post by schumaku »

dvoisin wrote:Is it normal for "autorun.sh@" to appear?
No, this is a symlink pointing to where ever.

With the Flash partition mounted, what shows:

Code: Select all

ls -als /tmp/config/autorun.sh
You can remove the symlink:

Code: Select all

rm /tmp/config/autorun.sh
Now create the file again...
dvoisin
Starting out
Posts: 13
Joined: Sat May 05, 2012 10:03 pm

Re: [How To] Set up an autorun.sh file

Post by dvoisin »

schumaku wrote:
dvoisin wrote:Is it normal for "autorun.sh@" to appear?
With the Flash partition mounted, what shows:

Code: Select all

ls -als /tmp/config/autorun.sh
[~] # ls -als /tmp/config/autorun.sh
0 lrwxrwxrwx 1 admin administ 9 Feb 19 2015 /tmp/config/autorun.sh -> /dev/null

I removed the symbolic link autorun.sh@ and then recreated the file. It is now working as expected. Thank you for your help! :D
danMP
New here
Posts: 4
Joined: Sun Jul 16, 2017 7:36 am

Re: [How To] Set up an autorun.sh file

Post by danMP »

Hello,

I have a TVS-673, and I try to get autorun.sh working as described by CasaP.
I added to autorun.sh for testing the following command:
echo "I will just append to my custom log file" >> /var/log/customlog

Unfortunately is not working after reboot.
Any suggestions?

Regards,
Dan
User avatar
schumaku
Guru
Posts: 43579
Joined: Mon Jan 21, 2008 4:41 pm
Location: Kloten (Zurich), Switzerland -- Skype: schumaku
Contact:

Re: [How To] Set up an autorun.sh file

Post by schumaku »

Dan,
Please read the newer part of the thread ... viewtopic.php?f=11&t=112970#p630122
CasaP
Know my way around
Posts: 210
Joined: Sat Feb 14, 2015 3:13 am

Re: [How To] Set up an autorun.sh file

Post by CasaP »

Just revisiting this;

A much easier (and graceful solution) viewtopic.php?t=130345&p=588946

It makes an autorun.sh and a separate scripts folder, just drop your scripts in there.

Remember to...
Navigate to Control Panel -> System -> Hardware, then enable the option "Run user defined processes during startup".
QNAP TS-453Pro w/ 4 * WD RED 4Tb (RAID5) - FW: QTS Build Latest (stable)
QNAP TS-853A w/ 8 * WD RED 6Tb (RAID5) - FW: QTS Build Latest (stable)

OSX 11.2.+
Locked

Return to “Turbo Station Installation & Setup”