Running python app in the background on linux
Solution 1
You can use nohup python ChatServer_Listen.py &
nohup
will log your program output to nohup.out
file.
To stop your program you have to use kill your_pid
command.
Solution 2
Like explained here :
https://stackoverflow.com/questions/625409/how-do-i-put-an-already-running-process-under-nohup
"
Using the Job Control of bash to send the process into the background:
[crtl]+z
bg
And as Sam/Jan mentioned you have to execute disown to avoid killing the process after you close the terminal.
disown -h
"
Solution 3
The right way to do this is to change the code to daemonize the process correctly. One way to do this is to use the daeminize
module.
The expedient way to do this, which also works with software that you cannot modify, is to start it like this
nohup python ChatServer_Listen.py >logfile.txt 2>&1 </dev/null &
Note that I didn't use sudo
. That is because you should put the above line in a shell script and run the shell script with sudo.
Solution 4
You want to use Supervisor. It's made exactly for this purpose, plus it will do things like restart the process if it dies, provide a web-based GUI to control it, etc.
Solution 5
An easy way to keep a process alive after you log off is to use screen.
Related videos on Youtube
Muco
Updated on September 18, 2022Comments
-
Muco over 1 year
so I have an Action, which fetches some data from rest. In a component, i need this data from the fetch, which is set in store. I use this data then like
someFunc(this.props.data)
incomponentDidMount()
. Afterwards i give the return value ofsomeFunc
an another Action, which does something with this data and also sets it in store.My only idea is, to give
someFunc
to the Action as a callback and call this right after the first data is fetched. So i have 2 dispatched in one action.I hope you guys understand what i mean. Is there a better way to do this?
Edit
In Component1 i dispatch an Action, which loads data i need to the store. In Component2 i dispatch another Action, which gets his value from
someFunc
function. ButsomeFunc
gets the data from the first Action, so the data is undefined when i callsomeFunc
, because the data isn't set in the store yet.More clear yet?
Component1
componentDidMount(){ this.props.dataLoadAction() //action for the fetch i told } const mapDispatchToProps = (dispatch) => { return { dataLoadAction: () => { dispatch(dataLoadAction()) } } }; export default connect(null, mapDispatchToProps)(Component1);
Component2
componentDidMount(){ this.props.changeDataAction(this.someFunc(this.props.data)) } render(){ return ( <div> {this.props.otherData} </div> ) } const mapStateToProps = (state) => { return { otherData: state.someReducer.otherData, data: state.someReducer.data, } }; const mapDispatchToProps = (dispatch) => { return { changeDataAction: (changedData) => { dispatch(changeDataAction(changedData)) } } }; export default connect(mapStateToProps, mapDispatchToProps)(Component2);
Actions
export function dataLoadAction(){ return dispatch => { HttpRequest.getJson("some url", json => { dispatch( { type: 'DATA_LOADED', payload: { data: json } } ) }); }); };
}
export function changeDataAction(changedData){ return dispatch => { dispatch( { type: 'LOAD_CHANGEDDATA', payload: mappedVis } ) }; }
-
Manula Waidyanatha almost 12 yearsDid you try
nohup python ChatServer_Listen.py &
? It should work. -
SilverbackNet almost 12 yearsSame as any other *nix: stackoverflow.com/questions/4525188/… The & is the most important part.
-
EEAA almost 12 yearsThis really has nothing to do with EC2.
-
Daddy almost 12 yearsIt worked, gents, someone please make an official answer =) Follow up, with the process detached, how do I kill/stop or restart?
-
-
Daddy almost 12 yearsWhen I try this, sudo screen python Chatserver_Listen.py, the next line says "[screen is terminating]". The python app begins as normal, but using another terminal instance and "sudo screen -r python" returns "There is no screen to be resumed matching python."
-
Daddy almost 12 yearsThis is what works for me. Can you explain the disown -h command and what it actually does? My flowchart was like this: 1) run python app, 2) press ctrl+z [app is suspended?],3) type bg [console prints something regarding python], 4) type disown -h [console prints nothing]. It seems to be working though, I closed the terminal window and the server is still listening and accepting clients
-
Daddy almost 12 yearsThis works for me, I thought it didn't but then I realized that I wasn't including the & character. Will this keep the program running indefinitely (until a reboot, crash or explicit kill of course)
-
Manula Waidyanatha almost 12 years@JustinXXVII Yes of course