WoDoSc
WoDoSc

Reputation: 2618

Run python script from rc.local does not write file

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.

Upvotes: 2

Views: 2425

Answers (1)

WoDoSc
WoDoSc

Reputation: 2618

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.

Upvotes: 1

Related Questions