Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to run a python program (a infinite while loop as listener inside) when my instance (Amazon Linux AMI) boots up. I have created a simple shell script like this:

#!/bin/sh
python bootstrap.py > bootstrap.log

Then I added the name of this script run_service.sh in my /etc/rc.local to run each time the instance boots up. But this simple approach seems not to be working, nothing happens. Both my shell script and python file are in /home/ec2-user folder. Do I need to specify full path in both rc.local and shell script for executing?

I found Amazon Linux AMI is CentOS/Fedora based, not Ubuntu. The cloud-init option seems not so clear, as I need to make this instance bundled into a customized AMI.

So, how can I do this by avoiding complex settings like puppet/chef, etc in a very simple way? For example, where to put my python file and the run_service.sh script? Please give me a simple example.

share|improve this question

1 Answer 1

You probably need to provide full paths to the script and the files it's using

#!/bin/sh
python /home/ec2-user/bootstrap.py &>/home/ec2-user/bootstrap.log

(you may need /usr/bin/python)

and in rc.local

/home/ec2-user/run_service.sh 2>/tmp/myservicestart.log

Also ensure that the run_service.sh is executable.

share|improve this answer
    
I tried both options, like giving full path to file and python but now an empty log file gets created, that means python command is not working. Do I need to add 'sudo'? But I can execute the same shellscript from bash using the deafult 'ec2-user'. –  TJ974 Nov 6 '12 at 9:45
    
@TJ974: It means that the python command is executing. Try capturing the stderr - change >/home/ec2-user/bootstrap.log` to &>/home/ec2-user/bootstrap.log and see what messages you get. –  Iain Nov 6 '12 at 9:50
    
if I run this run_service.sh from bash, it loads some python program and there is a infinite loop in it(like a service). But how can I put it into background so that whenever I logged in it will be still running? –  TJ974 Nov 6 '12 at 10:26
    
@TJ974: I have updated my answer so you can capture the stderr of both your python and the sh script. One of the log files will likely tell you why your daemon isn't running as you expect. –  Iain Nov 6 '12 at 15:52
    
Thanks a lot, seems running script is OK now, my problem is something different. I found that 'rc.local' is executed by the root, so when my python service tries to download something to the 'ec2-user' home directory, it is not successful. But if I run the same script as ec2-user, everything is OK. How can I execute that shell script from 'rc.local' as 'ec2-user' ? –  TJ974 Nov 6 '12 at 22:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.