24

I have a python based server which i start from the terminal. This particular instance of the terminal then gives control to the program, and the program uses it as a kind of logging window, until its closed. Is this normal, or should i somehow try to start the program some other way in which it will simply show as an active process? If i close the terminal from which i started the program, the program dies with it.

Thank you

10

Even old bash is using & for sending processes to background, but there is few other ways too .. but basic two are these :

1.)$~ your_command > outputfile_for_stdout &
        # runs your command in background, giving you only PID so you can exit that process by `kill -9 PID_of_process`
        # & goes at the end of row      


2.)$~ your_command > outputfile_for_stdout 
        # this will run your program normally
        # press  Ctrl + Z then program will pause
   $~ bg
        # now your program is running in background
   $~ fg
        # now your program came back to foreground
3.)you can run terminal window under screen command so it will live until you either kill it or you reboot your machine
   $~ screen
   $~ run_all_your_commands
       # Ctrl + A + D will then detach this screen
   $~ screen -r will reattach it

Some other useful commands :

   $~ jobs
        # will show you all processes running right now, but without PID
   $~ ps
        # will show you all processes for actual terminal window
| improve this answer | |
  • Hmmm, i was using command 'top' to view processes till now – U2ros Sep 23 '12 at 18:19
  • Great solution. Is there way to modify the 1. option so that pid is saved to a file. So a log file and a pid file are generated. – Oguz Bilgic Dec 15 '18 at 21:35
29

Turn it to a daemon (service)
daemon --name="yourservicename" --output=log.txt sh yourscript.sh

| improve this answer | |
7
$ servicename &

Using & causes the program to run in the background, instead of blocking the shell until the program ends.

| improve this answer | |
1

You can also use:

start-stop-daemon -SbCv -x your_command

here is init.d script to start and stop a program in the background.

| improve this answer | |
0

From the terminal you could also run screen or follow up you command with &. Easy way to run continuous processes.

| improve this answer | |
  • just using & to background the process in this case is to little, in my opinion. OP states that his server logs to stdout so, with & his terminal will be cluttered with output. Also since OP mentions closing the terminal, OP will not be able to foreground the process again and all the log-output will be lost. Better, at least, to redirect it to a log file or stick with the screen setup - but then please explain the basics of screento OP (detach/attach/etc) – Robert Riedl Feb 9 '18 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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