2

I want to run a python script at boot of Lubuntu 15.04. This python script writes some string into a text file placed in /home/aUser/aFile.txt

My /etc/rc.local file is:

#!/bin/sh -e
python /home/aUser/theScript.py &
exit 0

And the script /home/aUser/theScript.py is:

#!/usr/bin/python

f = open('/home/aUser/aFile.txt','w');
f.write("Some string...");
f.close();

Actually the python script does more, and run an infinite loop, this is why I run the script in background with &. Of course I have python installed:

~$ python --version
   Python 2.7.9

I checked if /etc/rc.local is called at boot, and it is, proof of that: I added a test into the /etc/rc.local in this way:

#!/bin/sh -e
python /home/aUser/theScript.py &
exit 0
echo "Test" >> /home/aUser/aTest.txt

and the file /home/aUser/aTest.txt is written/created at boot.

So everything looks correct, proof of that:

if I run manually ~$ /etc/rc.local the file aFile.txt is correctly written.

Instead if I start (or reboot) the OS, the file is not written at boot.

I suspect that could be a problem of permissions/user: I know that /etc/rc.local is run as root, but even if I set root or aUser as owner of the file, the situation is the same. Also run the python script in the /etc/rc.local as user aUser (with su command) does not solve the problem.

3
  • Maybe python is not in the PATH when rc.local is run. Try redirecting stderr of the python call to a file python /home/aUser/theScript.py 2>/home/aUser/stderr.log Commented Jul 31, 2015 at 13:08
  • What are those ; ? Commented Jul 31, 2015 at 13:25
  • This problem is no longer reproducible for me. It works fine for me. Commented Jul 31, 2015 at 13:41

1 Answer 1

1

Ok I found the problem and fix it, thanks to the @Zac comment.

Actually the python script try to open a network connection before writing the file: at boot time, when the python script is run from /etc/rc.local (so, it is run), the network is still not ready (probably because it is a wireless network) and therefore an exception is raised and the entire script stops. Capturing the exception solves the problem.

So at the end it was my fault, (not) helped by the rc.local that does not provide an easy way to debug.

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

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.