1

I have a python script i'd like to start on startup on an ubuntu ec2 instance but im running into troubles.

The script runs in a loop and takes care or exiting when its ready so i shouldn't need to start or stop it after its running.

I've read and tried a lot of approaches with various degrees of success and honestly im confused about whats the best approach. I've tried putting a shell script that starts the python script in /etc/init.d, making it executable and doing update-rc.d to try to get it to run but its failed at every stage.

here's the contents of the script ive tried:

#!/bin/bash

cd ~/Dropbox/Render\ Farm\ 1/appleseed/bin
while :
do
    python ./watchfolder18.py -t ./appleseed.cli -u ec2 ../../data/
done

i then did

sudo chmod +x /etc/init.d/script_name
sudo sudo update-rc.d /etc/init.d/script_name defaults

This doesn't seem to run on startup and i cant see why, if i run the command manually it works as expected.

I also tried adding a line to rc.local to start the script but that doesn't seem to work either

Can anybody share what they have found is the simplest way to run a python script in the background with arguments on startup of an ec2 instance.

UPDATE: ----------------------

I've since moved this code to a file called /home/ubuntu/bin/watch_folder_start

#!/bin/bash

cd /home/ubuntu/Dropbox/Render\ Farm\ 1/appleseed/bin
while :
do
    python ./watchfolder18.py -t ./appleseed.cli -u ec2 ../../data/
done

and changed my rc.local file to this:

nohup /home/ubuntu/bin/watch_folder_start &

exit 0

Which works when i manually run rc.local but wont fire on startup, i did chmod +x rc.local but that didn't change anything,

jonathan topf
  • 7,897
  • 17
  • 55
  • 85
  • Only a guess: does your script print to terminal? In that case, it will die. Redirect you output to logfiles or something similar. – Matthias Jun 29 '13 at 04:17
  • check out this tutorial(http://aameer.github.io/cloud-computing-101/) – Aameer Apr 01 '16 at 06:46

2 Answers2

0

Your /etc/init.d/script_name is missing the plumbing that update-rc.d and so on use, and won't properly handle stop, start, and other init-variety commands, so...

For initial experimentation, take advantage of the /etc/init.d/rc.local script (which should be linked to by default from /etc/rc2/S99rc.local). The gets you out of having to worry about the init.d conventions and just add things to /etc/rc.local before the exit 0 at its end.

Additionally, that ~ isn't going to be defined, you'll need to use a full pathname - and furthermore the script will run as root. We'll address how to avoid this if desired in a bit. In any of these, you'll need to replace "whoeveryouare" with something more useful. Also be warned that you may need to prefix the python command with a su command and some arguments to get the process to run with the user id you might need.

You might try (in /etc/rc.local):

( if cd '/home/whoeveryouare/Dropbox/Render Farm 1/appleseed/bin' ; then
      while : ; do
           # This loop should respawn watchfolder18.py if it dies, but
           # ideally one should fix watchfolder18.py and remove this loop.
           python ./watchfolder18.py -t ./appleseed.cli -u ec2 ../../data/
      done
  else
      echo warning: could not find watchfolder 1>&2
  fi
) &

You could also put all that in a script and just call it from /etc/rc.local.

The first pass is roughly what you had, but if we assume that watchfolder18.py will arrange to avoid dying we can cut it down to:

( cd '/home/whoeveryouare/Dropbox/Render Farm 1/appleseed/bin' \
     && exec python ./watchfolder18.py -t ./appleseed.cli -u ec2 ../../data/ ) &

These aren't all that pretty, but it should let you get your daemon sorted out so you can debug it and so on, then come back to making a proper /etc/init.d or /etc/init script later. Something like this might work in /etc/init/watchfolder.conf, but I'm not yet facile enough to claim this is anything other than a rough stab at it:

# watchfolder - spawner for watchfolder18.py
description     "watchfolder program"

start on runlevel [2345]
stop on runlevel [!2345]

script
   if cd '/home/whoeveryouare/Dropbox/Render Farm 1/appleseed/bin' ; then
     exec python ./watchfolder18.py -t ./appleseed.cli -u ec2 ../../data/0
   fi
end script
Alex North-Keys
  • 4,200
  • 1
  • 20
  • 22
  • interesting, thanks for the reply. I've made a new script that contains the while loop that in put in `/home/ubuntu/bin/watch_folder_start` and have prepended this line to my rc.local file `nohup /home/ubuntu/bin/watch_folder_start &`. When i run rc.local manually the script works as expected and the daemon runs happily but it still wont fire at startup, i tried to `chmod + rc.local` but that didnt seem to work either, any further thoughst – jonathan topf Jun 29 '13 at 09:47
  • Make sure you set `/etc/rc.local` back to normal perms with `chmod 755 /etc/rc.local`. Test from inside `sudo su - root`, in which one runs `/etc/rc2.d/S99rc.local start` (assuming your host defaults to runlevel 2 - check `who -r` to be sure). I ended up adding `touch /tmp/rc.local-ran` in `/etc/rc.local` to prove it was executing, since the "Running local boot scripts (/etc/rc.local)" only shows up if VERBOSE is enabled (not the default). `nohup` isn't normal here, and the `( cd ... && exec ... ) &` works fine for me here. It took ~1 min on my box to see the rc.local start up. – Alex North-Keys Jun 29 '13 at 12:27
0

I found that the best solution in the end was to use 'upstart' and create a file in etc/init called myfile.conf that contained the following

description "watch folder service"
author      "Jonathan Topf"

start on startup

stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

script
    HOST=`hostname`
    chdir /home/ubuntu/Dropbox/Render\ Farm\ 1/appleseed/bin
    exec /usr/bin/python ./watchfolder.py -t ./appleseed.cli -u $HOST ../../data/  >> /home/ubuntu/bin/ec2_server.log 2>&1
    echo "watch_folder started"
end script

More info on using the upstart system here

http://upstart.ubuntu.com/

https://help.ubuntu.com/community/UbuntuBootupHowto

http://blog.joshsoftware.com/2012/02/14/upstart-scripts-in-ubuntu/

jonathan topf
  • 7,897
  • 17
  • 55
  • 85