Run python script at startup

Questions and mods regarding system management may go here
Forum rules
Please note the disclaimer before modifying your Synology Product.
User avatar
Freeco
Experienced
Experienced
Posts: 111
Joined: Wed Dec 24, 2008 12:11 pm
Location: Belgium
Contact:

Run python script at startup

Postby Freeco » Mon Jun 22, 2015 3:59 pm

hi guys,
I have this python script that I'd like to run in the background on my DS414, but I'm not sure how to do this without potentially hanging the NAS at startup. My linux skills are quite basic...

So I have this script which I can already start manually from Windows and from my DS414.
It's very simple: python /path/to/script.py
It just sits there listening on a specific port for clients to request info.

As I don't want to run this manually when I need it and my NAS is on all the time anyway, I thought to just start this at startup automatically...
One of our linux admins at work pointed me towards the init.d folder. I found something similar here on the forum.
Thing is... when I try it manually the script does not accept the -d or --daemon parameter. I'm reluctant to create such a script without that specific parameter, cause won't that hang the startup process?? I can't afford locking myself out of my NAS, as it's the only one at hand :wink:

Any tips? :)

Model: DS414 (1.33GHz dualcore - 1GB RAM)
Firmware: DSM 6.1
RAID1: 3 x WD Red 4TB (WD40EFRX)
Network: Netgear GS724T-300
Backup: Sharkoon SATA QuickDeck Pro USB3 + Seagate Barracuda 7200.14 3TB (ST3000DM001)

Sold: DS212 & DS209+
Maurice118
Sharp
Sharp
Posts: 165
Joined: Wed Jul 24, 2013 9:02 pm
Contact:

Re: Run python script at startup

Postby Maurice118 » Mon Aug 10, 2015 9:42 am

Just put it in /etc/rc.local on startup every command in there will be run as root.

Code: Select all

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# run my python script
python /path/to/script.py

exit 0


PS don't forget "chmod +x /etc/rc.local" to give it execution rights if it doesn't already have those.
aka divx118 #synology at freenode.net
rt1900ac packages https://synology.timara.nl
User avatar
Freeco
Experienced
Experienced
Posts: 111
Joined: Wed Dec 24, 2008 12:11 pm
Location: Belgium
Contact:

Re: Run python script at startup

Postby Freeco » Wed Aug 12, 2015 5:08 pm

I figured it out in the meantime, and it works like a charm :D

Copy source files to:
/usr/share/myscript/

Copy startup script in location:
/usr/syno/etc.defaults/rc.d/S99myscript.sh

Code: Select all

#!/bin/sh
# Synology DSM bootup script
# Configured Variables:
PYTHON_EXEC="/volume2/@appstore/python/bin/python2.7"
SCRIPT_EXEC="/usr/share/myscript/script.py"

# Begin script
case "$1" in
start)
  PATH=$PATH:/volume2/@appstore/python/bin
  printf "%-30s" "Starting script"
  ${PYTHON_EXEC} ${SCRIPT_EXEC} &
  printf "[%4s]\n" "done"
 ;;
stop)
  printf "%-30s" "Stopping script"
  printf "[%4s]\n" "done"
  ;;
*)
  echo "Usage: $0 {start|stop}"
  exit 1
esac

exit 0


Set permissions
chmod 755 /usr/syno/etc.defaults/rc.d/S99myscript.sh

Model: DS414 (1.33GHz dualcore - 1GB RAM)
Firmware: DSM 6.1
RAID1: 3 x WD Red 4TB (WD40EFRX)
Network: Netgear GS724T-300
Backup: Sharkoon SATA QuickDeck Pro USB3 + Seagate Barracuda 7200.14 3TB (ST3000DM001)

Sold: DS212 & DS209+
Maurice118
Sharp
Sharp
Posts: 165
Joined: Wed Jul 24, 2013 9:02 pm
Contact:

Re: Run python script at startup

Postby Maurice118 » Sat Aug 15, 2015 8:26 am

Yep, that is also possible.
aka divx118 #synology at freenode.net
rt1900ac packages https://synology.timara.nl
Haldi
I'm New!
I'm New!
Posts: 6
Joined: Tue Dec 16, 2014 8:01 pm

Re: Run python script at startup

Postby Haldi » Mon Oct 19, 2015 1:12 pm

Freeco wrote:I figured it out in the meantime, and it works like a charm :D

Copy source files to:
/usr/share/myscript/

Copy startup script in location:
/usr/syno/etc.defaults/rc.d/S99myscript.sh

Code: Select all

#!/bin/sh
# Synology DSM bootup script
# Configured Variables:
PYTHON_EXEC="/volume2/@appstore/python/bin/python2.7"
SCRIPT_EXEC="/usr/share/myscript/script.py"

# Begin script
case "$1" in
start)
  PATH=$PATH:/volume2/@appstore/python/bin
  printf "%-30s" "Starting script"
  ${PYTHON_EXEC} ${SCRIPT_EXEC} &
  printf "[%4s]\n" "done"
 ;;
stop)
  printf "%-30s" "Stopping script"
  printf "[%4s]\n" "done"
  ;;
*)
  echo "Usage: $0 {start|stop}"
  exit 1
esac

exit 0


Set permissions
chmod 755 /usr/syno/etc.defaults/rc.d/S99myscript.sh


Hi, i have a similar problem. Tried to modify this script for my own purposes:

Code: Select all

#!/bin/sh
# Synology DSM bootup script
# Configured Variables:
PYTHON_EXEC="/volume1/@appstore/python/bin/python2.7"
SCRIPT_EXEC="/usr/local/airnef/airnefcmd.py"
# Begin script
case "$1" in
start)
  PATH=$PATH:/volume1/@appstore/python/bin
  printf "%-30s" "Starting script"
  ${PYTHON_EXEC} ${SCRIPT_EXEC} &
  printf "[%4s]\n" "done"
 ;;
stop)
  printf "%-30s" "Stopping script"
  printf "[%4s]\n" "done"
  ;;
*)
  echo "Usage: $0 {start|stop}"
  exit 1
esac

exit 0

Yet when i try to test this out i get an error:
root@NAS:/usr/syno/etc.defaults/rc.d# sh S99Airnef.sh
S99Airnef.sh: line 7: syntax error near unexpected token `$'in\r''
'99Airnef.sh: line 7: `case "$1" in


Also i do not have a /etc/rc.local folder on root on my DS413j with DSM6.0 Beta

Any leads on how to solve this dilema?
Maurice118
Sharp
Sharp
Posts: 165
Joined: Wed Jul 24, 2013 9:02 pm
Contact:

Re: Run python script at startup

Postby Maurice118 » Mon Nov 30, 2015 7:33 pm

You probably edited the script in windows. Now there are windows line endings in the script notice "\r" If you edit a script on windows, then notepad++ is a good choice. The line endings in notepad++ can be set to unix some where in the settings. This way you won't get the \r added which will brake any bash script.
aka divx118 #synology at freenode.net
rt1900ac packages https://synology.timara.nl
Ltek
Enlightened
Enlightened
Posts: 418
Joined: Sat Dec 10, 2011 9:21 pm

Re: Run python script at startup

Postby Ltek » Sun Jan 03, 2016 5:28 am

I want to run a script at startup but cannot figure out why it is not launching on reboot - but works fine when I run the script from SSH

script location and working folder (where config database is written/stored):

/volume1/@appstore/HAbridge


startup script in locations tried:

/usr/syno/etc.defaults/rc.d
/usr/local/etc/rd.d


script contents:

#!/bin/sh
nohup java -jar -Dvera.address=192.168.2.71 -Dserver.port=8099 -Dupnp.response.port=50099 -Dupnp.device.db=/volume1/@appstore/HAbridge/devices.db /volume1/@appstore/HAbridge/HAbridge.jar 2>&1 | tee /volume1/@appstore/HAbridge/HAbridge.log &
exit 0

All files/folders perms: all users Read & Execute, owner = root

Your help is highly appreciated!
User avatar
Freeco
Experienced
Experienced
Posts: 111
Joined: Wed Dec 24, 2008 12:11 pm
Location: Belgium
Contact:

Re: Run python script at startup

Postby Freeco » Mon Mar 28, 2016 6:57 pm

Can someone put me back on track? Since I've upgraded to DSM 6.0 my script doesn't run at startup anymore.
My script does still work if I run it manually through Putty though.

1st small thing: you can't logon with the root account anymore. Well, now I just logon with my own admin account, and switch to root with 'sudo -i'

Next problem: /usr/syno/etc.defaults/rc.d/ doesn't exist anymore. So where to I have to copy my S99myscript.sh so it gets started at boot?
I found other S99xyz.sh files under /usr/syno/etc.defaults/rc.sysv/, but putting it among them doesn't do the trick.
I also found empty dir /usr/local/etc/rc.d/, but that one doesn't do it either.

So... where should I put it?

Model: DS414 (1.33GHz dualcore - 1GB RAM)
Firmware: DSM 6.1
RAID1: 3 x WD Red 4TB (WD40EFRX)
Network: Netgear GS724T-300
Backup: Sharkoon SATA QuickDeck Pro USB3 + Seagate Barracuda 7200.14 3TB (ST3000DM001)

Sold: DS212 & DS209+
dconway
Rookie
Rookie
Posts: 38
Joined: Tue May 27, 2014 3:34 am

Re: Run python script at startup

Postby dconway » Sat Apr 16, 2016 4:07 pm

Freeco wrote:Can someone put me back on track? Since I've upgraded to DSM 6.0 my script doesn't run at startup anymore.
...
So... where should I put it?


I'm having the same problem. Anyone know the answer? Thanks
User avatar
CoolRaoul
Seasoned
Seasoned
Posts: 550
Joined: Tue May 18, 2010 7:08 pm

Re: Run python script at startup

Postby CoolRaoul » Sat Apr 16, 2016 8:22 pm

Should work providing the script is in "/usr/local/etc/rc.d/" and respect the following guidelines:

  1. It must have the suffix “.sh”. For example, “myprog.sh”.
  2. The permission must be 755.
  3. It must have the options “start” and “stop”. When the system boots
    up, it will call “myprog.sh start”; when it shuts down, it will call
    “myprog.sh stop”.

This is and has always been the *only* officially documented method (until DSM6 Task Manager added the "triggered tasks" method with "on boot" option)
Last edited by CoolRaoul on Sun Apr 17, 2016 10:35 am, edited 2 times in total.
CR
User avatar
Freeco
Experienced
Experienced
Posts: 111
Joined: Wed Dec 24, 2008 12:11 pm
Location: Belgium
Contact:

Re: Run python script at startup

Postby Freeco » Sun Apr 17, 2016 10:15 am

Forgot to update the thread.
Indeed, my script does get started at boot if I put the .sh file in /usr/local/etc/rc.d/

Model: DS414 (1.33GHz dualcore - 1GB RAM)
Firmware: DSM 6.1
RAID1: 3 x WD Red 4TB (WD40EFRX)
Network: Netgear GS724T-300
Backup: Sharkoon SATA QuickDeck Pro USB3 + Seagate Barracuda 7200.14 3TB (ST3000DM001)

Sold: DS212 & DS209+
catch-n-release
I'm New!
I'm New!
Posts: 2
Joined: Wed Sep 21, 2016 7:55 pm

Re: Run python script at startup

Postby catch-n-release » Fri Sep 23, 2016 10:03 pm

Forgot to update the thread.
Indeed, my script does get started at boot if I put the .sh file in /usr/local/etc/rc.d/


Wow, after pretty much two days of troubleshooting this problem, you had my answer. Thanks for updating the thread.

For people searching, I upgraded to DSM 6.0 from 5.5 to whatever upgrade is current. Then I went about re-installing pyTivo. But I just couldn't get the pyTivo.sh file to execute on boot. I followed these instructions:

I had it in /usr/syno/etc.defaults/rc.sysv as S99pyTivo.sh and I had used chmod +x. I tried it as a symbolic link

Code: Select all

ln -s /usr/local/pyTivo/pyTivo.sh /usr/syno/etc.defaults/rc.sysv/S99pyTivo.sh


and as a copied file (using cp /usr/local/pyTivo/pyTivo.sh /usr/syno/etc.defaults/rc.sysv/S99pyTivo.sh). No luck. I could run S99pyTivo.sh from the terminal after using SSH as root, and it would run even after I closed the terminal session. Which was at least a functional work-around, I'd only need to run it myself when I rebooted the NAS, which doesn't happen that often.

But this solved the problem, thanks. I just put copied S99pyTivo.sh to /usr/local/etc/rc.d and now it runs on startup.

(While I'm here -- I'm also using the ffmpeg for pyTivo as suggested here: https://forum.synology.com/enu/viewtopic.php?t=118851#p445647.)

Return to “System Managment Mods”

Who is online

Users browsing this forum: No registered users and 2 guests