2

I want to run a python script on boot of ubuntu 14.04LTS.

My rc.local file is as follows:

sudo /home/hduser/morey/zookeeper-3.3.6/bin/zkServer.sh start

echo "test" > /home/hduser/test3

sudo /home/hduser/morey/kafka/bin/kafka-server-start.sh /home/hduser/morey/kafka/config/server.properties &

echo "test" > /home/hduser/test1

/usr/bin/python /home/hduser/morey/kafka/automate.py &

echo "test" > /home/hduser/test2

exit 0

everything except my python script is working fine even the echo statement after running the python script, but the python script doesnt seem to run. My python script is as follows

import sys
from subprocess import Popen, PIPE, STDOUT

cmd = ["sudo", "./sbt", "project java-examples", "run"]
proc = Popen(cmd, shell=False, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
proc.communicate(input='1\n')
proc.stdin.close()

which works perfectly fine if executed individually.

I went through the following questions , link

I did a lot of research but couldn't find a solution

Edit : echo statements are for testing purpose only, and the second actual command (not considering the echo statements) is starting a server which keeps on running, and even the python script starts a listener which runs on an infinite loop, if this is any help

1
  • That Python script would be so more naturally implemented as /path/sbt "project java-examples" > /dev/null 2>&1 in rc.local. Python is only a distraction there. Commented Feb 23, 2016 at 10:39

4 Answers 4

6

The Python script tries to launch ./sbt. Are you sure of what if the current directory when rc.local runs? The rule is always use absolute paths in system scripts

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. Not using absolute path was the issue.
1

Do not run the Python script in background, run it in foreground. Do not exit from its parent script. Better call another script from "rc.local" that does all the job of "echo" and script launching.

Run that script from "rc.local"; not in background (no &).

You do not need "sudo" as "rc.local" is run as root.

1 Comment

I tried it without echo statements and ran the script not in background and still it doesnt work
1

If you want to run python script at system boot there is an alternate solution which i have used.

1:Create sh file like sample.sh and copy paste following content

#!/bin/bash
clear


python yourscript.py

2:Now add a cron job at reboot.If you are using linux you can use as following

a:Run crontab -e(Install sudo apt-get install cron)
b:@reboot /full path to sh file > /home/path/error.log 2>&1

And restart your device

1 Comment

since the edit queue is full, I will make the answer clear here : run this command sudo crontab -e then add @reboot /full/path/to/file/sample.sh > /home/error.log 2>&1 to the end of crontab!
0

I had the same problem, and the cause was that the root user was not able to run the module I wanted to use. The python was run, but exited immediately. The solution is to run the python under the same normal user with sudo -u [user]. That solved the problem and it runs even from the rc.local.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.