3

I have a very simple problem. A python script starting with the following:

import sys
sys.stdout = open("server.out", 'w')

If I enter this command directly to the terminal

python start_server.py &

It writes the server.out file. If I enter service tdserver start, all the init.d script does is this:

python start_server.py &

The same command, nothing else. Yet, in this case the stdout doesnt get written to server.out

Why? No processes have lock on the file, confirmed that the script is stopped with ps -aux

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Rápli András
  • 3,869
  • 1
  • 35
  • 55
  • 1
    please try using full-path for the filename, e.g. open("/tmp/server.out", 'w') and check if you find the file in /tmp – Yaron Jan 24 '17 at 12:05
  • It fixed my problem. I wonder what the default exec dir would be. Please add it as an answer so I can accept. – Rápli András Jan 24 '17 at 12:09

2 Answers2

2

By default, the working directory for init.d services is /etc/init.d, see How do I execute an arbitrary script with a working directory of the directory its in?.

Solutions:

  1. Use absolute paths in your Python code, like mentioned in the comments.

  2. Before starting the Python script by python start_server.py &, change the working directory by a simple cd /your/path/.

Hope this helps!

Community
  • 1
  • 1
linusg
  • 6,289
  • 4
  • 28
  • 78
2

As discussed in the comment:

You were writing the to server.out in the current directory.

The current directory of init.d is /etc/init.d (as mentioned by linusg).

Using absolute path for the output file (to a write-able location) will solve the problem.

Yaron
  • 10,166
  • 9
  • 45
  • 65