2
votes

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

3
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. - msw

3 Answers

5
votes

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

1
votes

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
votes

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