Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to write a startup script that executes an application (MapProxy) after a server reboot. My server runs Ubuntu 11.04 Natty 64 bit.

In the script, I execute a python script exec python /$path/"config.py" & which successfully creates a fcgi.socket that the web server can communicate with. However, I need to make fcgi.socket writeable or MapProxy will not work i.e. I need to action a chmod on it.

It seems that after I action the config.py, I lose control within the startup script - it does not go on to make the fcgi.socket writable. Any ideas?

#! /bin/bash
:
exec python /$path/"config.py" &
exec chmod o+w /$path/"fcgi.socket" &
exit 0
share|improve this question
    
On Ubuntu, you may want to look at stop-start-daemon to deal with your Python script, probably with the -b option. –  cjc May 29 '12 at 16:44
add comment

3 Answers

The important bit you seem to be missing is what the exec part is doing. From bash's help exec: "Replace the shell with the given command.". When you say exec python /$path/"config.py" your script essentially says "I'm done here, let's let python take it from here, execute this python script, and whatever it returns is what I return". So nothing later in the script will execute. It doesn't make sense to use exit unless it is the last thing your script does.

You have "exit 0" at the end of this script which says "my script should terminate as if everything succeeded, no matter what else might have failed". if that is really what you want, then you don't want to use exec, since your scripts exit code will be the exit code of whatever process you exec. Instead you might want to return an actual failure result from python or chmod if you get one.

share|improve this answer
add comment

Remove the execs and & symbols. That defers processing to the background so that the next command can run immediately. In effect you are changing the permissions on a file that doesn't exist yet, since it takes some time for your first command to run and create the socket.

share|improve this answer
    
Did you miss exec? –  Oliver May 29 '12 at 15:59
add comment

New script:

#! /bin/bash
python /$path/"config.py" &&
chmod o+w /$path/"fcgi.socket"

& sends a process to the background and executes the next process immediately. && waits for the return value of the last process and if it is zero then the next process is started.

Removing your exit 0 is so that you'll get the error code of whatever process executed last in the script. It should be zero, but this way it will propagate if you have an error.

share|improve this answer
    
Thanks for the replies but I'm afraid that doesn't work for me. "python config.py" is something that I would run manually and it stays "in process" until I type crtl-c. That's why I add & to defer it to the background and follow up by chmod but as you say, it actions too soon. I tried using "sleep 5" but still get the same fcgi.socket created but permissions unchanged. –  Jason May 29 '12 at 16:22
    
Actually, I doubled the sleep time and it now works :- python /$path/"config.py" & sleep 10 chmod o+w /$path/"fcgi.socket" Is there a better solution? I guess the config.py should send a signal to the host script in some way... –  Jason May 29 '12 at 16:40
    
@Jason is your program meant to run as a daemon? Perhaps ideally you would edit config.py to do this task? –  Jeff Ferland May 29 '12 at 16:48
    
Hi Jeff, it runs up a FastCGI server (as described here : mapproxy.org/docs/1.2.0/deployment.html#FastCGI) –  Jason May 29 '12 at 16:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.