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

Community
  • 1
  • 1
aladeen
  • 297
  • 7
  • 18
  • 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 Feb 23 '16 at 10:39

4 Answers4

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

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
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.

nvd
  • 2,995
  • 28
  • 16
  • I tried it without echo statements and ran the script not in background and still it doesnt work – aladeen Feb 23 '16 at 10:34
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

Vishnu
  • 324
  • 1
  • 3
  • 17
  • 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! – Sadra Mar 19 '21 at 07:23
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.

Fedor
  • 17,146
  • 13
  • 40
  • 131
drjpt
  • 1